Thursday, October 13, 2016

New Chess Graphics for Chess for Android

Bryan Whitby, who contacted me earlier to tell about very cool USB chess board projects, contacted me recently with a very generous offer to use his awesome chess graphics in Chess for Android. I am very thankful, since these graphics look really good, and combine well with the various board types already supported. So, expect an updated on Android Play and my website really soon! And, thank you Bryan!

Sunday, May 22, 2016

Micro-KIM Tutorial: Brightness of LED Display

A demo is a program that shows off the abilities of a computer or programmer, sometimes even beyond the limits of an original architectural design. For example, a well-known demo theme on the Commodore 64 consists of rendering sprites in the border, i.e. outside the area originally destined for rendering sprites. This tutorial presents demos that use the LED display beyond its (probable) original purpose: adjusting the brightness of characters or even segments.

As shown in the previous tutorial, a refreshing loop is necessary to show all 6 characters on the LED display. Here, the refreshing rate directly defines the brightness of these characters. Simply looping around yields maximum brightness, while lowering the refresh rate dims the screen. This idea can also be used to adjust the brightness of parts of the LED screen (characters or even individual segments within the characters).

To illustrate this effect, let's modify the program of the previous tutorial (the source code of this and the modified program can be found on my Micro-KIM webpage). To focus on the brightness difference, only 'A's are shown. Also, rather than directly looping around, add a few new instructions right before the jump back.

             lda #0
             sta sad
             lda #11
             sta sbd
             lda #$f7  ; change to $40 for segment
             sta sad
             ldx #200
bright_delay dex
             bne bright_delay
             jmp display_loop

This new code glows up the second character for a while before looping around refreshing all other characters. The result of this change is that the second A on the LED display looks a lot brighter, as illustrated below (the effect looks a bit better on the actual kit than in this picture).

Single bright character (second A)

The same idea can be used to change the brightness of individual segments within characters. Changing the value $f7 into $40 as indicated in the comment above makes the center segment of the second A brighter than the rest of the display, as shown below (again, the effect looks a bit better on the actual kit).

Single bright segment (center segment in second A)

Finally, the idea gives rise to a wide variety of demos that change the brightness of the LED display somehow, such as fading the display in and out, sliding a glow effect over characters or segments, or giving emphasis to parts of the display. Give it a try and post your demo here as a comment. For starters, here is one of my sliding glow demos.

Sliding Glow Effect
That's it for this tutorial. As before, please like, share, or comment if you like this series!

Saturday, May 21, 2016

Micro-KIM Tutorial: The LED Display

The following schematic illustrates what is fun about retro computing: the complete schematic of a microcomputer fits on a single page (a higher resolution PDF can be downloaded from the Briel Computers website).

Micro-KIM Schematic. Courtesy Vince Briel - Briel Computers

The schematic shows that the 6 character LED display is controlled through some selection logic by the data ports of the 6532 RIOT. Because the 16 pins of the two 8-bit data ports A and B would not have sufficed to control all characters in the LED display simultaneously, instead a few bits of B select one character (value 9 selects the first, value 11 the second, etc.) while the lower 7 bits in A are used to control the 7 segments of that particular character (bit 0 controls the top segment, bit 1 upper right segment, etc.).

Note that with this scheme, it is possible to set one character and "go on with the program", as I showed in an earlier tutorial by displaying a very bright 8 in the first character, followed by simply looping the program (it could do something else instead). However, it is not possible to set all characters somehow, and "go on". Instead, the program has to loop over all characters and constantly refresh their contents. Due to the refreshing loop, displaying the full display appears a bit less bright than displaying a single character without such a loop.

Now let's write down some code to control the full LED display (as before, you can find the source code on my Micro-KIM webpage). First, some definitions provide symbolic names for the addresses of the data ports of the 6532 RIOT. The data registers contain the actual values, whereas the bits of the data direction registers define whether each pin is used for input (0) or output (1).

sad  = $1740   ; A data register
padd = $1741   ; A data direction register
sbd  = $1742   ; B data register
pbdd = $1743   ; B data direction register

Next some initialization code sets the 6532 RIOT data direction registers for output on the needed pins.

.org $0200
             lda #$7f
             sta padd
             lda #$3f
             sta pbdd

Then, the refreshing loop looks as follows. Here, register x iterates from 0 to 5 to load the proper value for the 7 segments of each character from a data array (with values that define the string "aart b"). Register y iterates from 9 to 19 with increment 2 to select each subsequent character on the LED display through data register B. Note that before changing data register B, the program clears data register A to ensure the old contents do not accidentally "flicker" very briefly in the next character. Furthermore, the program has a short delay when each next character is shown to ensure that character "glows up" a bit before moving on.

display_loop ldx #0
             ldy #9
char_loop    lda #0
             sta sad         ; no flicker
             sty sbd
             lda data, x
             sta sad
             ldx #4
char_delay   dex
             bne char_delay  ; glow up character
             cpx #6
             bne char_loop

             jmp display_loop ; keep refreshing

data .byte   $f7 $f7 $d0 $f8 $00 $fc

Assembling, uploading, and running this program as shown in earlier tutorials shows the following output on the LED display. Of course, feel free to change the values in the data array to your own custom-made characters.

Taking full control of the LED display

That's it for this tutorial. Next tutorials will talk more on controlling the brightness of the LED display, scrolling text, moving graphics, and using interrupts to implement the refreshing loop.

Sunday, April 17, 2016

Micro-KIM Tutorial: The Monitor Program

A simplified memory map of the Micro-KIM is shown below. This tutorial explores the 2K EPROM, leaving a more detailed exploration of the free RAM and 6532 RIOT for later. Address space $1400 to $173f is unused in the standard Micro-KIM kit configuration. 

  | 2K EPROM  |$1fff
  | monitor   |
  | program   |$1800
  | 6532 RIOT |$17ff
  | I/O, timer|
  | and RAM   |$1740
  | optional  |$173f
  | I/O, timer|
  | and RAM   |$1400
  |           |$13ff
  |  5K RAM   |
  |           |$0000

Addresses $1800 through $1fff are taken by the 2K EPROM, which is a read-only memory area that stores the 6530-003 and 6530-002 parts of the monitor program. You can, of course, inspect all  individual bytes in the address mode on the Micro-KIM kit, but I recommend reading the assembly listing of the monitor program in the appendix of the Setup and User's Manual of Briel Computers. The compact coding style found in this monitor program is quite educational, but the monitor program also provides a rich set of subroutines that can be used in your own programs.

When you press the reset key RS on the kit (RST signal), the 6502 processor jumps to the address stored in $1ffc/$1ffd, which has the hard-coded value $1c22 in ROM. This is the entry labeled RST in the monitor program, i.e. the KIM entry via reset. Similarly, the non-maskable interrupt (NMI signal) and interrupt request (IRQ signal) jump to addresses stored in $1ffa/$1ffb and $1ffe/$1fff, respectively, with hard-codes values $1c1c and $1c1f in ROM. These are the entries labeled NMIT and IRQT in the monitor program, which contain indirect jump instructions to the vectors NMIV and IRQV stored in RAM at addresses $17fa/$17fb and $17fe/$17ff. Since these entries in RAM are undefined on power on, the User's Manual instructs you to fill these RAM locations with the value $1c00, so that the ST key or single-step feature (NMI) or BRK instruction (IRQ) jump into the entry labeled SAVE in the monitor program.

The JP2 jumper selects whether the monitor program should handle communication over the RS232 port (jumper on) or keypad/display on the kit (jumper off). Note that both the RS232 port and keypad/display always can be programmed to work regardless of this jumper state. But the jumper is connected with bit PA0 of data port A in the 6532 RIOT, which is tested in the monitor program to decide what action to perform next.

A very useful subroutine in the monitor program labeled SCANDS appears at address $1f1f. Even though these instructions are actually part of a larger subroutine that is used to show the addresses and data during normal operation of the kit, when calling this entry directly, the kit shows the three bytes stored consecutively in zero page addresses $f9, $fa, and $fb in hexadecimal format on the 6 digit LED display.

This routine makes writing a simple three-byte counter very easy, as shown below (you can also find this source file on my Micro-KIM webpage).

scands .equ $1f1f
       .org $0200
; Initialize a 3 byte counter to zero.
       lda #0
       sta $f9
       sta $fa
       sta $fb
; Display and increment the 3 byte counter in a loop.
; Displaying before each increment slows down counting
; quite a bit.
loop   jsr scands
       inc $f9
       bne loop
       inc $fa
       bne loop
       inc $fb
       jmp loop

Using the cross-assembler as shown in the previous tutorial yields the following paper tape output.


Uploading this to the kit and running looks as follows, counting up a 6 digit (three byte) hexadecimal number.

Slow counter on the Micro-KIM
Although the lower digit goes faster than the eye can see, calling the display subroutine before each increment substantially slows down counting. A follow up tutorial looks at using interrupts for display, making the actual computation, counting in this case, much faster.

That's it for now. In the next tutorial, I am going to show how to use the 6532 RIOT to take full control of the LED display, show custom-made characters at any position, control brightness, and avoid flickering. As always, like, share, or comment if you enjoy the tutorial so far.

Saturday, April 16, 2016

Micro-KIM Tutorial: A First Assembly Program

At the lowest level, the 6502 executes numerical machine code. For example, the following bytes in hexadecimal format constitute a simple program that displays a single 8 on the LED display of the Micro-KIM.

  a9 ff 8d 40 17 a9 09 8d 42 17 4c 0a 02

Let's enter this program into the memory of the Micro-KIM. Power on the kit with jumper JP2 off and press the RS key. Then enter 0200 to set the address and press DA to go into data mode. Next, enter the numbers above pressing the + key after each number pair (so, enter A9 + FF + etc.). Before running, I strongly recommend checking the values. Use AD to go back into address mode. Press 0200 again and use + repeatedly to check all entered values. Once satisfied, press 0200 and GO. If all goes well, you will see a very bright 8 as first digit on the LED display (in later tutorials I will explain why).

Displaying a single digit on the Micro-KIM
Obviously constructing and entering programs this way is tedious and error-prone. It is much easier to program in assembly language, where statements still closely relate to machine instructions, but where an assembler takes care of translating instructions and addressing modes into the numeric equivalent as well as resolving symbolic names and evaluating simple expressions into actual values. For the Micro-KIM, one typically wants to use a cross-assembler, i.e., an assembler that runs on a host computer, such as a desktop or laptop, but generates machine code for a different target computer, in this case the Micro-KIM.

While reliving the good old days of my Commodore 64, I implemented a cross-assembler that runs on Windows (win2c64), Linux (lin2c64) and MacOS (mac2c64) and generates machine code for a 65xx-based microcomputer. This assembler supports several common output formats, including the paper tape format that can be directly uploaded to the Micro-KIM. Therefore, in this series, I will use win2c64 for assembly.

The assembly code for the program above looks as follows (note, if you are a bit rusty on the 6502, I recommend reading some online documentation on the instruction set first; more details on the assembler syntax and operation of win2c64 can be found in the online manual; details on the program itself will follow in later tutorials).

sad  .equ $1740
sbd  .equ $1742
     .org $0200         ; start program at $0200
     lda #$ff
     sta sad            ; set all bits of A data
     lda #9 
     sta sbd            ; set value 9 in B data
loop jmp loop           ; loop forever to avoid
                        ; returning to monitor program

To invoke the assembler and generate paper tape format, save this code in a source file, kim.s, and then run the following from the command line.

win2c64 -P kim.s

This generates an output file kim.ptf in textual paper tape format (there is also a binary variant, but that one is less useful for uploading over TTY). The contents of this file are shown below. These can be copied-and-pasted to the Micro-KIM through the terminal, as explained in the first tutorial. Much more convenient then entering a long list of numbers!


The win2c64 binary can also be used as disassembler on the generated output as follows.

win2c64 -dP kim.ptf

Which shows the addresses, numeric encoding and instructions as follows (note how the assembler has resolved and removed all the symbolic information and comments from the original source file).

$0200 a9 ff     lda #$ff
$0202 8d 40 17  sta $1740
$0205 a9 09     lda #$09
$0207 8d 42 17  sta $1742
$020a 4c 0a 02  jmp $020a

Just looking at the encoding, you may recognize the numbers you entered manually at the start of this tutorial.

Now that you have become more familiar with the tools, you are ready for the next tutorial, where I will explore using routines from the monitor program to show more numbers on the LED display in your assembly program. After that, I will explore taking full control of the LED display!

As before, if you like this series, please let me know by liking or sharing this posting, or leave a comment.

Friday, April 15, 2016

Micro-KIM Tutorial: Getting Started

Perhaps reminiscing the past is a sign of getting older, but I cannot help but look back fondly at the times I learned programming machine code on the Commodore 64 in the eighties. Therefore, it is probably no surprise I still occasionally enjoy programming 6502 on the Micro-KIM, which is a modern replica of the seventies KIM1 microcomputer, made available by the well-known retro computer kits provider Briel Computers.

In fact, I am having so much fun with this board, I decided to write a series of tutorials on operating and programming the Micro-KIM. In this series, I assume you have already some experience with the Micro-KIM and 6502 machine code, and have read the basic documentation that is shipped with the kit. Other than that, I hope to give additional information on various topics, such as developing assembly programs, programming the display, using the RS232 port or keypad, setting up timer-based interrupts, using a cross-assembler to generate programs in paper tape format, and uploading these to the kit.

Note that the original KIM1 featured a 6502 microprocessor, 1K of static RAM, two 6530 RRIOT IC's, and a 6 character hexadecimal LED diplay. Even though the Micro-KIM is a surprisingly accurate clone, it features a single 6532 RIOT, 2K EPROM for the monitor program, and 5K RAM. Please keep these differences in mind while reading the tutorial, since not all examples that work on the Micro-KIM will also work on the original KIM1.

Let's get started by uploading a simple demo to the Micro-KIM. In following tutorials, I plan to delve into the details of the demo itself. For now, I simply give the demo in paper tape format.


First, connect the Micro-KIM through a serial cable with your computer, make sure the jumper JP2 is in the ON position to enable RS232 input in the monitor program, and switch the kit on. Next, start a terminal program on your computer, such as HyperTerminal or PuTTY, which I assume you have set up already as described in the basic documentation. Last, press the RS key on the kit and hit ENTER in the terminal program. If all goes well, the Micro-KIM greets you with a prompt that looks something like this.

Micro-KIM Prompt
Then copy the paper tape format code above into the clipboard, press L in the terminal, and then paste from the clipboard. Again, if all goes well, eventually the Micro-KIM answers with the prompt again. Then press 0200 SPACE  and G to start the program. Alternatively, you can remove the jumper, press 0200 on the keypad on the kit and GO to start this program. Both ways will work for this particular demo, which looks as follows.

Wave Demo on the Micro-KIM
In the next tutorial, I will introduce cross-assemblers and start exploring how to program the display.

If you enjoy this series so far, please let me know by liking this posting. Or leave a comment. Suggestions for new topics are also welcome!

Sunday, April 10, 2016

Micro-KIM weekend

A rainy weekend was a perfect excuse to play with my micro-KIM, which had been collecting dust in a drawer for too long. I had fun using my own cross-assembler to develop and generate programs in paper tape format, and upload these to the micro-KIM via the PuTTY client.

I figured out how to use the 6532 RIOT to set up a timer-based interrupt service, which is an important step in separating actual computation from display and keypad handling. The following clip shows the difference between incrementing a three-byte memory counter at roughly 1000 times per second (timer delayed) and 100,000 times per second (full speed with about 10 cycles per iteration at 1MHz). Perhaps a nice illustration of how fast even those early computers were.

Saturday, March 19, 2016

New Buttons for Chess for Android

Not everyone was happy with the "swipe-up" to open the options menus (for devices that lack a menu button, or that broke the legacy options menu altogether), so I decided to simply implement an on-screen button instead. I also improved the graphics in the on-screen buttons for navigation, something that as long overdue.

The result is shown below. The right-most button with the horizontal lines opens the new-style options menu. As before, the other buttons are used for navigating the game, see the manual for details. On devices that still support a physical or virtual menu button (vertical dots in the screen-shot below), that button opens the legacy options menu.

Expect a similar update for Reversi and Checkers for Android soon too.

Friday, March 18, 2016

Chess-playing Robotic Arm

A while back I got an email from Isaiah James D. Puzon, a computer engineering student at the Philippines FEU Institute of Technology, with a minor request for a new feature in Chess for Android that would help with his thesis project: a chess-playing robotic arm. It was very rewarding to receive pictures from his exciting working prototype a few months later. You did a great job building this robot arm, Isaiah. Congrats with your graduation and good luck with your further career!

Friday, March 11, 2016

Opening Books in Chess for Android

I got several questions on how to use the opening book features in Chess for Android, so I hope this blog posting will be useful.

By default, the GUI uses a built-in opening book before it consults any chess engine, either the built-in Java engine, or an imported third-party chess engine. This small built-in opening book (consisting of few opening lines I studied a long time ago as a young member of a chess club, by the way) provides some variety of play, but otherwise is probably not sufficient for the more serious chess player. Therefore, before using an engine's own opening book, one has to disable the GUI opening book, by disabling the "Use Book" choice in the options menu, as shown below (touch to remove the check mark).

It may seem a bit counter-intuitive to disable the "Use Book" feature in order to use an engine's opening book, but without doing this, the GUI will first consult the built-in opening book before consulting the engine, so the engine will only start to consult its own opening book once the GUI runs out of its own. Just think of this feature as "Use GUI Book".

With this being done, there are two ways to enable an engine's book. First, let's assume you have installed an opening book someplace on the SD card, say in /sdcard/book/komodo.bin on the phone. Then, when setting the options of an UCI engine during the first import (clear the engine options first if you imported it earlier), fill out the opening books path accordingly, as shown below. UCI engines that don't support this opening book feature will have this part grayed out.

That's it! After this, the engine (Komodo in this case) will use its own opening book (don't remove the opening book from the SD card though, it is not copied to internal memory).

Alternatively, if an engine does not support this feature, but it does try to read an opening books from the current directory in which the engine is started, the following trick may work. Simply install the opening book as if it were an engine, see below. Note that this only works if the engine expects an opening book with that name in the current directory or if one can somehow define this information through an initialization file ( engine.ini file, which can be copied to the same directory using the trick described above).

Happy opening booking! Please let me know if you have any more questions.

Monday, February 8, 2016

Checkers for Android Animation

I have also improved the graphics and animation in Checkers for Android. You can see the result in the video below. Both the reversi and checkers updates are now available on Google Play.

Friday, February 5, 2016

Reversi for Android Animation

Reversi for Android is getting a graphics overhaul! The "retro stones" have been replaced by stones with a gradient. A new animation on placing and flipping stones makes it more clear what moves just have been played. Except an update on Google Play soon!

Saturday, January 30, 2016

Android Applications Updates

I just released new versions of my Android applications, as usual available on Google Play or as direct download. These versions introduce a new options menu dialog that can be accessed by swiping up. Hopefully this provides a viable alternative on devices that lack or broke the legacy options menu button.
  • Reversi for Android v2.5.5
  • Checkers for Android v2.6.5
  • Chess for Android v5.1.5
Chess for Android also introduces a slightly cleaner position set up window, and added the ability to define an opening book for an imported UCI or XBoard engine. Please let me know if all works well.

Friday, January 29, 2016

New Options Menu

In order to ensure my games remain relevant as the old options menu is phased out by some vendors, I am adding new support for action buttons and feature selection. As shown below for the upcoming Reversi for Android update, in addition to the old options menu entered by pressing the menu button, the user can now swipe up to enter a new dialog with action button, check boxes and a drop down box for the levels.

I am not just excited about making sure that users can enter this menu again, but also about the fact that this probably makes the features much more discoverable for my users.

For Chess for Android, it will look something like this.

Expect updates on all my games soon!

Sunday, January 24, 2016

Working on Chess for Android Again!

I am getting excited about adding new features to Chess for Android again! I am in the process of working around the menu button issue on Samsung devices. Furthermore, I am extending UCI engine support with the ability to set a book opening for each engine, as well as allowing a wider variety of number of threads (six in the example below). If you have any urgent feature requests for engine support, this would be the right time to ask.

Monday, January 11, 2016

Android Applications Update

I just released minor updates for my Android applications, as usual available on Google Play or as direct download. These versions allow opening the options menu with a simple "swipe up" gesture over the screen. Chess for Android also fixes a problem with importing engines.
  • Reversi v2.5.1
  • Checkers v2.6.3
  • Chess v5.1.2
As some background, my applications still use the older options menu, introduced in the days that Android devices still had a physical menu button. Newer models without a physical button sometimes allow accessing the options menu through a virtual button (shown as vertical dots) or through long-pressing the activity switch button, or sometimes don't provide an easy way at all. This release hopefully makes accessing the options menu easier again.

Note that, apparently, a recent Samsung update broke the options menus completely for any application (not just mine) on some models (like the Galaxy S6 edge). This update does not fix that problem. I am still trying to get one of these devices for further debugging.