Saturday, January 14, 2023

Software Sprite

As often discussed, the Motorola 6847 VDG offers little in terms of assistance for game programmers or other software authors. While many contemporary video chips offered assistance in scrolling backgrounds or in moving objects in the foreground of a display, the 6847 hardware merely provides access to a static framebuffer. As we have seen prviously, anything beyond a still image requires work from the CPU. Fortunately for CoCo people, the Motorola 6809 CPU is reasonably capable, and when used properly it can provide at least some of these video effects.

 

Get Things Moving

Of course  the only way to start is to draw something in the framebuffer memory by using the CPU's usual load and store instructions to copy data out of program storage and into the framebuffer. To create the illusion of motion this must be done rapidly and repeatedly. If care is not taken between draws to remove the previous image then "movement" will create a trail across the screen, corrupting the background image. Various methods exist for doing that cleanup (e.g. saving and restoring the background as part of the sprite draw operation), but this takes both program development time and execution time. The latter in particular is at a premium when so much data is already being moved for the background.

Mask Mandate 

In the 6847's RG3 (4 color, 2 bit per pixel) graphics mode, each byte of buffer memory represents 4 pixels. Since memory writes happen one byte at a time, any image that uses less than 4 pixels in each byte it occupies will interfere with those extra pixels unless care is taken. This is typically observable as the movable object appearing a bit like a postage stamp laid on top of the display.  Unless the object being displayed is rectangular with a pixel width that is a multiple of 4 pixels, then part of the draw operation must include masking. This involves reading the background, using a bitwise mask of the object to combine the object's pixels with the background pixels then writing the resulting data. This operation performs the trimming necessary to preserve the background. There is some execution time lost for the masking, and storing the mask data doubles the amount of memory necessary for storing the graphics objects. But there really is no way around this part.

No Background Check

With the scrolling background demonstration as a base, we can now show a foreground item on top. Since both the foreground item and the background image move in relation to each other, this is not a single draw (or mask and draw) operation. With a normal static framebuffer, displaying a moving foreground itme would require some action to restore any background bits eclipsed by the foreground when the foreground moves. This might require regenerating the background algorithmically or saving the background before drawing the foreground and restoring the background after the foreground is moved. If we draw the foreground item in our source or "render" buffer, then all of the above would be true. But by drawing the foreground item directly in the current "display" buffer, we know that the background will be copied from the render buffer to the next display buffer at the next buffer switch. Since the background is already being redrawn for every frame, we don't need to maintain the background at all, saving all that time that would have been used for maintaining the background image!

So while redrawing the entire background initially seems expensive, that operation turns out to be cheaper than tracking damage to the background image and redrawing the background over and over again. It took me a while to wrap my head around this feature, but hopefully the explanation above makes things clear for you? I hope so.

Hopefully I can keep finding time to turn this discussion into an actual CoCo game. If you want to see where this all goes, then be sure to stay tuned!

Tuesday, January 10, 2023

Scrolling Engine

Well, it's been a while!  Despite the boredom and isolation that came along with the COVID years, I didn't do much that was very impressive in terms of retro programming during that time. In a cruel twist of fate, the boredom (and an ongoing spat with another CoCo media figure) seems to have robbed me of inspiration during that time.  Despite that, I did manage to produce a little technology demo to demonstrate scrolling a full graphical background on a CoCo. Some have asked for more information on how this was done. If that interests you, then please continue reading!


So Much Work

Scrolling a background image with the 6847 VDG is not by itself a huge technical problem. All that is required is to move a lot of data from where it starts to a new location. For a CG3 (128x96) screen that means 3 KB or 3072 bytes of data moved for each frame of video. Many video chips roughly contemporary with the 6847 provided hardware support that allow (usually at the expense of some memory usage) for the illusion of movement by changing the source of the video data between frames. The SAM/VDG combination used in the CoCo offers a rudimentary version of this, but the frame-to-frame jumps would be too coarse and the memory demands too great to be useful by itself for smooth animation.

So Little Time

With no useful hardware support available, brute force must be used to copy bytes around in the video buffer. Computers are fast, but not infinitely fast. How much time do we have? And how long does it the 6809 take to copy 3072 bytes? The VDG generates just under 60 video frames per second. With the normal 0.89 MHz clock speed of the CoCo, that leaves us less than 15000 clocks to move the data for each frame. Unless the CPU can move one byte in five clock cycles (which it cannot), then there is just not enough time.  Even at just 30 frames per second, we still wouldn't be very close.

Switch Things Up

The 6847 generates video output at just under 60 frames per second, and the SAM/VDG combination used in the CoCo does offer a "screen flip" capability. Combining those two details allows us to "double buffer", or to draw in one buffer while the other is used as the display source and then to switch between the buffers. Using two buffers effectively halves our video frame rate but doubles the amount of time available for video data movement and gets us back to a manageable 30 frame updates per second.

Blast Away

One unique feature of the 6809 CPU architecture is the User stack. This provides an efficient means for moving data, particularly when combined with the System stack and used for what is colloquially known as "stack blasting". This feature allows for moving up to 8 bytes at a time in as little as 26 cycles for just two instructions. This would potentially allow us to move a frame's worth of data in roughtly 10000 cycles (depending on the actual code implementation). My implementation depends on some looping and is not quite that efficient. But it does suffice to keep the video update well within the 30 frame per second limit while leaving a generous amount of time available for doing something else (e.g. handling I/O or implementing game logic).

Single Source

The two buffers mentioned above are merely tools for keeping the display looking correct and up-to-date. But both buffers are completely ephemeral. A third buffer is kept for rendering the actual video frame. This buffer is the source for the stack blast copies described above. since both of those buffers will be overdrawn almost immediately, any "permanent" change to the display needs to be done in this "render" buffer. Consequently this is the perfect place to draw any static background items (e.g. anything to be scrolled).

I think that mostly covers it. I originally implemented this back in the Summer of 2020, so hopefully I am not overlooking any big details. But I believe the main points are covered. This technique gets the job done, and it has a few advantages that I hope to exploit in the near future...stay tuned!

Thursday, January 30, 2020

Space Assault Port to 6800

Now, on to the bigger project I've been hinting at... :-)



So...Space Assault is a clone of the well known arcade hit Space Invaders, produced for the Tandy Color Computer by The Image Producers, (written by Lou Haehn?) and released in 1981. Some time ago, Darren Atkinson took it upon himself to disassemble the Space Assault ROM image for the CoCo and to do a binary translation of the 6809 code into a version for the 6803-based Tandy MC-10. The MC-10 version plays very much like the CoCo version, with modifications from Darren to accommodate use of the MC-10 keyboard in place of the CoCo joystick.

It occurred to me that this bit of MC-10 magic might be a good candidate for porting to my Micro Chroma 68 (uchroma68), so I sent Darren an email requesting access to his MC-10 Space Assault source. Darren was nice enough to oblige, so I immediately checked his source into a git repository and set about to making the initial superficial changes necessary to reproduce his MC-10 Space Assault binary using my chosen assembler, the old Motorola Freeware BBS assembler.

6803 > 6808

Working MC-10 code seems like a good place to start for a uchroma68 port, but some more preparation work is still required. The problem is that not all code that runs on the 6803 CPU in the MC-10 will also run on the 6808 CPU in the uchroma68. The 6808 (and the related 6802) execute the same instruction set as the earlier 6800, without any additions. But the 6803 (and the related 6801) use a somewhat improved architecture that supports a number of instructions that are not supported by their 6800/6802/6808 cousins. Not only are these extra instructions quite useful, they also make the 6803 a bit more like the 6809 architecturally. It is no surprise to learn that Darren used a number of these new instructions when porting Space Assault from the 6809 to the MC-10's 6803. Porting the MC-10 version of Space Assault to run on the uchroma68's 6808 will involve replacing these 6801/6803 instructions with 6800/6802/6808-compatible sequences.

Macro-like Translations


Several of the new 6801/6803 instructions involve an architectural change that allows the 8-bit A and B accumulators to be grouped together as the 16-bit D accumulator. Instructions like "LDD" and "STD" are easily replaced with "LDAA/LDAB" and "STAA/STAB" combinations, just as "ADDD" is easily replaced with an "ADDB/ADCA" combination. Using a macro processor (either built-in to an assembler or something external like m4) should make these substitutions trivial, requiring no changes to the source code at all. But as I am using the quite primitive assembler from Motorola, I simply made these changes directly in the source code -- the triviality made this more worthwhile than introducing an m4 pass in the Makefile.

Emulation Functions

Some of the other new 6801/6803 instructions are not so easily replaced. Equivalent instruction sequences are lengthy, and require allocation of memory for temporary storage of registers used in those sequences. Instructions such as "PSHX", "PULX", and "ABX" are in this category, and they are so useful that again it is no surprise that Darren makes hearty use of them in his MC-10 port of Space Assault. In this effort, I found that replacing these instructions with equivalent subroutine calls provided good results while only requiring six bytes for temporary storage of the D, X, and S registers and a few bytes for a temporary stack. (The S register storage and the temporary stack were required to avoid data corruption in a couple of places where an emulated "ABX" was required within a routine that was using S as a pointer for data movement.)

EMUSAVS    rmb       2            ; temp storage of S for 6801/6803 emu
EMUSAVX    rmb       2            ; temp storage of X for 6801/6803 emu
EMUSAVD    rmb       2            ; temp storage of D for 6801/6803 emu

EMUSTACK   rmb       3            ; temp stack for 6801/6803 emu calls
EMUSTKTOP rmb       1

emu_abx
          pshb
          psha
          stx        EMUSAVX
          clra
          addb     EMUSAVX+1
          adca     EMUSAVX
          staa      EMUSAVX
          stab      EMUSAVX+1
          ldx        EMUSAVX
          pula
          pulb
          rts

emu_pshx
          stx        EMUSAVX             ; save X
          tsx
          staa      EMUSAVD             ; save A & B
          stab      EMUSAVD+1
          ldaa      ,x                  ; get return address from stack
          ldab      1,x
          pshb                          ; push return address onto stack
          psha                          ; - also adjusts stack pointer
          ldaa      EMUSAVX             ; retrieve X value
          ldab      EMUSAVX+1
          staa      ,x                  ; save X value onto stack
          stab      1,x
          ldaa      EMUSAVD             ; restore A & B
          ldab      EMUSAVD+1
          ldx        EMUSAVX             ; restore X value
          rts

emu_pulx
          tsx
          staa      EMUSAVD             ; save A & B
          stab      EMUSAVD+1
          ldab      3,x                 ; get X from stack
          ldaa      2,x
          staa      EMUSAVX             ; save X
          stab      EMUSAVX+1
          pula                          ; get return address from stack
          pulb                          ; - also adjusts stack pointer
          stab      3,x                 ; save return address onto stack
          staa      2,x
          ldaa      EMUSAVD             ; restore A & B
          ldab      EMUSAVD+1
          ldx        EMUSAVX             ; set new X value
          rts

Simple Rewrite

Perhaps the most complicated new 6801/6803 instruction is "MUL", which of course multiplies the 8-bit accumulators A and B to produce a 16-bit product in the D accumulator. I anticipated coding a lengthy routine to emulate this instruction, but I found that the code was only doing a multiply by a fixed value of 5! Two left shifts and a single add produced equivalent results, so "MUL" was no longer an issue... :-)

More To Come

I think that covers the remaining "prep" work for the uchroma68 port of Space Assault. At this point, I have code for a machine that does not exist: an MC-10 with a 6800 CPU. The result, of course, will run on a normal MC-10 -- but that would be a pointless objective. I would show a demo video here, but the result is virtually indistinguishable from Darren's original code. Hopefully you can just trust me -- it works. ;-)

The next step will be accounting for differences between the board-level architectures of the MC-10 and the uchroma68. This involves differing locations for video memory, and for where the code and data are loaded and stored. It also involves accounting for I/O differences, such as reading inputs from the keyboard and possibly making sounds. It will all be very much "in the weeds", of course...stay tuned!


Monday, January 27, 2020

Micro Chroma 68 Tape Storage

In my previous post, I showed video of me loading and running code on an emulated Micro Chroma 68 (aka uchroma68) machine, and hinted at a bigger project. I intended to be back sooner to describe the code loading process and then move-on to the bigger project. But, I got started on the bigger project and neglected the next blog update -- time to get back on track!

Audio-Encoded Data

Code can be loaded onto the uchroma68 in a variety of ways, including from the keyboard input or over an audio-encoded interface intended for using "compact cassettes" for mass storage. For real hardware, other options include modifications to use an ACIA for serial input or modern interfaces to drive the keyboard interface from a remote computer. But since our objective includes targeting the unmodified MAME-emulated uchroma68, it seems best to pursue only standard options for loading code.

So, how to convert object coded into appropriate audio?

Kansas City Standard

The Kansas City standard (KCS), or Byte standard, is a way of storing digital data on standard Compact Audio Cassettes at data rates of 300 to 2400 bits per second (at 300–2400 baud) that was first defined in 1976. It originated in a symposium sponsored by Byte magazine in November 1975 in Kansas City, Missouri to develop a standard for storage of digital microcomputer data on inexpensive consumer quality cassettes.
The uchroma68 uses the Kansas City Standard for audio data encoding. This old standard defined how to transfer bits and bytes as audio, suitable for storage on consumer audio tape. Fortunately for me, open source tools (e.g. py-kcs) already exist for encoding and decoding such audio. Unfortunately, some of those tools presume to deal only with ASCII-encoded test files (e.g. BASIC programs). Since I needed to deal with binary data, I did have to write a patch to allow for handling binary input to the KCS encoder:
 diff --git a/kcs_encode.py b/kcs_encode.py
index 21bbf080917d..1f078dd0b537 100755
--- a/kcs_encode.py
+++ b/kcs_encode.py
@@ -52,7 +52,7 @@ def kcs_encode_byte(byteval):

 # Write a WAV file with encoded data. leader and trailer specify the
 # number of seconds of carrier signal to encode before and after the data
-def kcs_write_wav(filename,data,leader,trailer):
+def kcs_write_wav(filename,data,leader,trailer,binary):
     w = wave.open(filename,"wb")
     w.setnchannels(1)
     w.setsampwidth(1)
@@ -64,7 +64,7 @@ def kcs_write_wav(filename,data,leader,trailer):
     # Encode the actual data
     for byteval in data:
         w.writeframes(kcs_encode_byte(byteval))
-        if byteval == 0x0d:
+        if not binary and byteval == 0x0d:
             # If CR, emit a short pause (10 NULL bytes)
             w.writeframes(null_pulse)
   
@@ -74,16 +74,29 @@ def kcs_write_wav(filename,data,leader,trailer):

 if __name__ == '__main__':
     import sys
-    if len(sys.argv) != 3:
-        print("Usage : %s infile outfile" % sys.argv[0],file=sys.stderr)
+    import optparse
+
+    p = optparse.OptionParser()
+    p.add_option("-b",action="store_true",dest="binary")
+    p.add_option("--binary",action="store_true",dest="binary")
+    p.set_defaults(binary=False)
+
+    opts, args = p.parse_args()
+    if len(args) != 2:
+        print("Usage : %s [-b] infile outfile" % sys.argv[0],file=sys.stderr)
         raise SystemExit(1)

-    in_filename = sys.argv[1]
-    out_filename = sys.argv[2]
-    data = open(in_filename,"U").read()
-    data = data.replace('\n','\r\n')         # Fix line endings
-    rawdata = bytearray(data.encode('latin-1'))
-    kcs_write_wav(out_filename,rawdata,5,5)
+    in_filename = args[0]
+    out_filename = args[1]
+    if opts.binary:
+        data = open(in_filename, 'rb').read()
+        rawdata = bytearray(data)
+    else:
+        data = open(in_filename, 'r', newline=None).read()
+        data = data.replace('\n','\r\n')         # Fix line endings
+        rawdata = bytearray(data.encode('latin-1'))
+
+    kcs_write_wav(out_filename,rawdata,5,5,opts.binary)
With that, I could encode audio data for the uchroma68 by using the '-b' option which I added to kcs_encode.py. But what about the logical format of that data?

JBUG-Compatible Format

Knowing how to send 0's and 1's is not too helpful until one knows what 0's and 1's to send. Even a program binary still needs to be accompanied by information such as where to write the program in memory and perhaps protocol data used to manage the transfer. The TVBUG firmware on the uchroma68 uses a format for binary data that is compatible with the format used by the JBUG firmware from the MEK6800D2 board which had preceded it, although TVBUG allows for an extension to the format that provides for the program name to be encoded along with the program data. Based on information from the TVBUG and JBUG manuals and some reading of the assembly language listings of those programs, I was able to write a script that converts a binary program file into a hex dump representing the data prepared for audio encoding:
#!/bin/sh

###########################################################
# Convert a binary to hexdump of JBUG logical tape format
#       (used by both JBUG and TVBUG)
###########################################################

usage() {
        cat <<USAGE
usage: $(basename $0) [-l <loadaddr>] [-n <progname>] [-p] <filename>

        -l <loadaddr>   base load address
        -n <progname>   output program name
        -p              pad tape output with 0xff characters
        <filename>      input data file
USAGE
}

while getopts ":l:n:p" opt
do
        case ${opt} in
        l)
                LOADADDR=$OPTARG
                ;;
        n)
                PROGNAME=$OPTARG
                ;;
        p)
                PADOUT=1
                ;;
        \?)
                echo "Invalid option: $OPTARG" 1>&2
                ;;
        :)
                echo "Invalid option: $OPTARG requires an argument" 1>&2
                ;;
        esac
done
shift $((OPTIND-1))

# Set default load address of zero (if not specified on command-line)
LOADADDR=${LOADADDR:-0}

if [ $# -ne 1 ]
then
        usage
        exit 1
fi

INFILE=$1
INSIZE=$(stat -c %s $INFILE)

if [ x$PADOUT = x1 ]
then
        for i in $(seq 1 1024)
        do
                echo -n 'ff'
        done
        echo
fi

if [ -n "$PROGNAME" ]
then
        echo -n $PROGNAME | xxd -p
        echo '0d'
fi

OUTSIZE=0
while [ $OUTSIZE -lt $INSIZE ]
do
        BLOCKSIZE=$(($INSIZE - $OUTSIZE))
        if [ $BLOCKSIZE -gt 256 ]
        then
                BLOCKSIZE=256
        fi

        echo -n 'ff'
        echo -n '42'
        printf '%x' $(($BLOCKSIZE - 1))
        printf '%04x' $(($LOADADDR + $OUTSIZE))

        xxd -p -g 1 -l $BLOCKSIZE -s $OUTSIZE $INFILE

        OUTSIZE=$(($OUTSIZE + $BLOCKSIZE))
done

if [ x$PADOUT = x1 ]
then
        for i in $(seq 1 144)
        do
                echo -n 'ff'
        done
fi

echo -n 'ff'
echo '47'
The hex dump output can be fed to the xxd utility (e.g. 'xxd -p -r'), captured, and then encoded to Kansas City Standard format audio.

Demo

In my earlier post, I mentioned "a text-based animation that I originally ported from a VZ-200 C program to assembly language for the MC-10 which I had always suspected would be easy to get on the uchroma68". Well, I was right -- changing two "equ" statements (one for the load address and one for the screen address) was all that was required...


But wait -- there's more! The bigger project involves more porting from the MC-10 to the Micro Chroma 68, this time porting some code that originated on the Tandy Color Computer before being ported by Darren Atkinson.

Intrigued? Then stay tuned...

Tuesday, December 31, 2019

Micro Chroma 68 Revisited

About six years ago, on this blog I documented the build of an original Motorola Micro Chroma 68 (aka "uchroma68") kit. The "kit" was intended as an evaluation platform for the 6847 VDG and consisted of a PCB and the major ICs like the 6808 CPU, 6847 VDG, etc. (I had to acquire the various other bits, mosty passive components.) I enjoyed the build process and even hoped to target the machine for some retro games development. Nevertheless, the overall lack of available hardware made this platform seem too niche to be a priority for development. After I wrote a simple program to demonstrate the machine working, I essentially set aside the uhroma68 and largely ignored it.

Virtual Reality

Of course, I never forgot about the uchroma68. Recently in my Internet wanderings, I came across the news that MAME had added uchroma68 support:


This bit of news was exciting enough to inspire me to install the proper ROM images and update my MAME installation in order to try out the emulation. Having done so, I decided to pursue how to get my old example program running on the emulator:


Whet Your Appetite

Getting this far was fun and not without some challenges. I'll save those details for the next post! Plus, I have a text-based animation that I originally ported from a VZ-200 C program to assembly language for the MC-10 which I had always suspected would be easy to get on the uchroma68. That might provide a better taste of the uchroma68 platform and how it is similar to the MC-10.

Beyond that, I have another porting project in the works that involves a headstart provided by a very clever partner. I think you will enjoy that one, and it might bring us the best game to come to the uchroma68 so far. Intrigued? Well...stay tuned!

Saturday, January 30, 2016

RC2016/01 Wrap-Up

Here we are at the end of RC2016/01, and what have we got? Well in my case I have a port of Xmas Rush for the TRS-80 MC-10 -- very exciting stuff! At the end of my last post on the topic, there was still some timer stuff to handle and some "odds and ends". So, let's see how that went...


Pause To Experiment

Early on I had thought that precision timing might not really be necessary, that perhaps the video glitches would be spread so thin that it would hardly be noticeable. Later, someone else made a similar suggestion. So I conducted an experiment that changed the game's time base to just a bit more than the time required for a single frame of video. This did have the predicted effect of spreading the video glitches around so as to make them less objectionable, but they were still both noticeable and constantly recurring. While I did not pursue any algorithmic changes to the drawing routines that might have made this even less objectionable, the experiment cemented my belief that stable timing synchronized with the video hardware remained a worthwhile pursuit.


Help From Afar!

I implemented the timing synchronization by enlisting help from the user to position a marker off-screen. This marker represented the time the video hardware was not actively drawing the screen, and moving it off-screen equated to synchronizing the program to do its animation while the hardware was not actively drawing. This solution is quite effective, and I don't think it is objectionable for the user to do this once when starting the game.

The timing I am seeing here at home is the roughly 60Hz timing of NTSC video. Of course, in much of the world the video standards are timed for 50 Hz vertical refresh rates. By adding a user choice between 50Hz and 60Hz and a corresponding variable used for timer setting, I was able to accommodate that situation. However, I don't have an MC-10 that uses PAL video or one of the French MC-10 clones, the Matra Alice. Luckily I was able to reach out for testing help on the Yahoo MC-10 group. There I found confirmation that my 50Hz timer synchronization was working too.

One Last Bug...

Along with that testing came a report that in some cases the snowmen would overlap and disappear! Worse, even in this vanished state they could still capture the player, causing him to lose...what was going on?

The moving objects in Xmas Rush are drawn using an XOR operation against the existing screen data.  The primary reason for this is to highlight the case when a snowman captures the player, but a side effect is that when two snowmen exactly overlap then they cancel each other out on the screen and seemingly disappear. In order to counteract that I have a series of tests in the snowmen movement routines that were intended to prevent an exact overlap. However, those test were clearly not working...

The original 6809 code for the snowman position tests used a CMPD instruction to do a 16-bit comparison against the D register (i.e. the A register and B register together). The 6803 lacks such an instruction, so in the conversion process I compared 8-bits against A and the other 8-bits against B. This changed the flow just enough that I confused myself, and I ended-up checking each snowman's position against the position of only one other snowman, allowing overlaps to occur with the other two! Once I found that problem it was easy to correct, and now snowmen should generally be unable to eclipse one another.

That's A Wrap

So anyway, that's about it for Xmas Rush on the MC-10. I'll get the MC-10 binaries added to the Xmas Rush web page and then I'll call it a month. I am really happy to have finally taken the time to learn more about the MC-10 and to put it to a decent use, and I hope you have enjoyed monitoring my progress along the way. I will probably have Xmas Rush on display at the 25th annual "Last" Chicago CoCoFEST! this April, if you want to stop and give it a go.

Until next time, I hope you will find your own excuse to get out your retro computing gear and make it do something fun, useful, or otherwise enjoyable. And as always, I hope you will stay tuned!

Saturday, January 23, 2016

Checking My List

Another week down and roughly one more left to go in the Retrochallenge RC2016/01 event. I got off to a bit of a slow start this month, but since then things have progressed well. It looks like things are shaping-up nicely for my port of Xmas Rush to the TRS-80 MC-10!


Time Is On My Side

One of my main concerns with this port has been the lack of a vertical sync signal available to the CPU on the MC-10. A direct port of the drawing algorithms from the CoCo version of Xmas Rush resulted in objects sometimes disappearing completely within certain vertical ranges of the screen. I considered altering the basic drawing algorithms in hopes of minimizing the screen problems, but that really seemed like a "band aid" solution at best.

The MC-10's Motorola 6803 CPU does have a hardware timer circuit, so precision timing is possible. The problem remains as to where (actually when) to anchor the timer settings. Since we know how long the screen is actively drawing and how long each frame lasts, we can derive the anchor point by interacting with the user. The timer is used to divide the screen into two periods, representing the active and inactive drawing times. A visual indicator is used to indicate the temporal position of the inactive period, and the user is asked to provide input that is used to relocate the inactive period until it's visual indicator is not visible. Once that temporal anchor is established, all that is required is to never miss a frame timer expiration... :-)


Watch Your Step

Xmas Rush uses a bitwise map to represent the scrubby trees that comprise the playfield terrain. At game time, that map is also used to generate a data structure used to detect potential collisions between that terrain and the player and snowmen. Converting the original 6809 code to 6803 code was a bit tricky, since the 6809 code made use of the extra index register available on that CPU. The conversion made a lot more use of the stack, reminding me of a Towers of Hanoi game gone terribly wrong... Anyway, I eventually managed to sort that out and objects were suitably constrained from moving across the tree-filled terrain.

With the player now moving about, it seemed like a good time to introduce the pitter-patter sound used during player motion. The CoCo version used the horizontal sync signal for timing square wave generation, but the MC-10 is as equally unhelpful for horizontal sync as it is for vertical sync. I had hoped that I could do some tricky hardware timer manipulations to use the timer both for the vertial sync emulation and for sound timing, but I couldn't seem to master that feat. Instead, the sound timing is based around cycle-counted loops representing horizontal sync periods. This proved to be satisfactory for all of the sound generation in Xmas Rush.

BTW, the pitter-patter sound is not a normal tone. Each up-down transition of the square wave is governed by the output of a linear feedback shift register (LFSR) used to generate pseudo-random data. In a very real sense, the pitter-patter sound is more of a noise than an actual tone.


Win Or Lose

It is important to be able to tell when the moving objects collide. This causes the player to lose when he collides with one of the snowmen, and it is a prerequisite for a win that the player must seize the evergreen tree. Xmas Rush does not have any special data structure for detecting sprite collisions. With so few sprites, it was easiest just to check each sprite position to see if it overlaps with the player.

A set of flags is used within the game code to indicate which snowmen are active and whether or not the evergreen tree has been seized by the player. These flags are used in the drawing routines to control what is drawn in a given frame. The "seized" flag is also used in coordination with the player's position to detect a "win" condition.


The last, big missing piece was the movement of the snowmen. Since the game loop erases and redraws every sprite during every frame, movement is nearly trivial -- just change the draw location and the object moves! Each of the four snowmen has its own movement strategy and a corresponding routine to implement that strategy. I won't divulge them here, but each strategy is fairly simple, and the snowmen can be manipulated into trapping themselves behind parts of the terrain as they pursue their personal strategy.

Anyway, with moving snowmen the game is largely complete. All that remains is integration of the timer calibration code into the main program, and a few odds and ends. This week should see a successful end to this project...I hope you will stay tuned!

Friday, January 15, 2016

RC2016/01 Half-time Update

Wow, half-way through January already! After a slow start, I now have my Xmas Rush port to the MC-10 well underway. I recorded a few more progress reports throughout the past week -- let's review!


Graphics Mode

The Christmas tree on the title screen is drawn in a "semi-graphics" mode in which the low-resolution graphical glyphs are really just special characters, no different from numerals, punctuation, and letters of the alphabet. These are handy for a number of things, but they are generally not as flexible as the "high-resolution" graphics used for the main part of the game. Fortunately, switching to graphics mode is fairly simple to do.

The 6847 Video Display Generator (VDG) is configured through a collection of logic signals applied to its external pins. These can be wired a number of different ways, and even on the MC-10 some of the configuration pins are actually driven directly by the incoming video data. The rest are connected to a small register on the MC-10 motherboard. That register can be changed by writing to any address between $9000 and $bfff, although $bfff is the conventional address to use. Writing an appropriate bit pattern to this address is all that is required to change the graphics mode.


Coordinate Translation

With graphics mode engaged, we need some way to specify where graphics are displayed on the screen. For simplicity's sake, I designed Xmas Rush to use a simple 32x32 grid for placing objects on the screen. This alignment naturally matches the number of horizontal bytes for each line, avoiding the need for any mask-and-shift operations when writing data to the screen. The X- and Y-coordinate for each object is stored in a two-bye pair that can be easily manipulated using the double-accumulator operations (e.g. LDD, STD, etc) available on the 6803.

There is a bit of math involved in translating a set of coordinates on the 32x32 grid into an address for accessing the framebuffer. Of course, I already had 6809 code to do this on the CoCo. Blindly translating that to 6803 code for the MC-10 quickly provided satisfactory results.



Timing Again

From there, I quickly conquered the task of drawing the playfield. Xmas Rush uses a bitmap to describe the placement of all the bare trees that make-up the playfield. Translating the relevant 6809 code from the CoCo to 6803 code for the MC-10 again proved an easy task, allowing me to begin work on the fundamentals of constructing the game loop.

The CoCo version of Xmas Rush uses a graphics animation technique known as "double buffering". At any give time one buffer is displayed while the other is redrawn for the next frame of animation. Periodically a vertical sync event occurs.  At that time the program changes which buffer is actually displayed, and work shifts to clearing and redrawing the previous buffer. This technique prevents ever displaying a partially drawn frame, and it provides a natural source of "real time" information for pacing the game.


The MC-10 provides no means for the CPU to observe when a vertical sync event happens, but it does provide an internal timing source that can be used for game pacing. My initial attempt to use this timing source as a substitute for the vertical sync event was disappointing, because I was resetting the timer based upon its current value (a moving target) rather than as an offset from the previous setting. A little fix stabilized the timing so at least the game can be paced correctly.

The current timing technique is still prone to showing some weird video effects, depending on the exact timing between the game and what the VDG is doing. I think that a manual, user-driven timing synchronization is possible. Alternatively, maybe just some random resets will be acceptable -- this would be similar to the "reset until this is blue" that was so common on CoCo games that used NTSC "artifact" colors.

In any case, there is still some work to be done adding player movement, snowman AI, and other missing game bits.  This is a good start, but it needs a good finish. Do you want to see how it ends-up? Then you will just have to stay tuned...

Sunday, January 10, 2016

MC-10 Familiarization

Programming retro computers is not rocket science. After all, it wasn't that long ago that teenagers and grade-schoolers regularly produced commercial-quality programs on these very machines! Nevertheless, a new machine always has some new idiosyncrasies and a few tricks to learn to make them useful. The MC-10 is no exception, and much of my time spent with that machine in the past week has been devoted to learning a few such tips and tricks...


Time Well Spent

I like to make use of "real time" sources to control events in my games. Hand-coded loops and cycle counting can be an effective soure of "real time" information, but they are fragile. Small changes in the game code can produce large timing differences than can be difficult to predict and painful to recode again and again. On the CoCo, I often use the vertical and horizontal sync signals as reliable sources of timing information. Unfortunately, the MC-10 makes no provision for accessing either sync source.

Fortunately, the Motorola 6803 CPU in the MC-10 has a little "secret" -- an internal timer! The timer functionality is based on counting elapsed CPU clock pulses, which amounts to a source of "real time" information. Using this timer I was able to replace the hand-coded timing loop in my screen flashing example program, unlocking the ability to accurately time game events.


Key Exercises

Moving along from timing, it should be obvious that a game needs some form of input to indicate what the player wants to do in the game. A joystick would seem like an obvious choice, but the MC-10 has no joystick ports. It does, however, have a keyboard. The MC-10 keyboard employs a reasonably standard switch matrix that the CPU controls by outputing a value at one location and reading back a value from another location. This is similar to how the keyboard is read on the CoCo and on any number of other systems.

Conversely, a game needs some form of output. Graphics are obvious, but a little audio can be a big plus. The MC-10 has no sound chip, and it does not even have the CoCo's DAC. However, it does have a 1-bit digital output for producing audio, similar to the Apple II or the ZX Spectrum. Producing audio on the MC-10 is reasonably simple, particularly with control of the timer. There is even a ROM call available for producing simple tones. One might be surprised at how much is possible with 1-bit audio, but simple tones will be more than enough for porting Xmas Rush.


Practice Porting

So with a few exercises under my belt, it is time to start porting. Since the MC-10 has the same Motorola 6847 video chip as the CoCo and can support all of the video modes used by Xmas Rush, there is practically no need to generate new graphic data for anything. I started by reproducing the "intro" and "tally" screens using the same data used in the CoCo version. I did change some of the text to reflect the use of the MC-10 keyboard rather than the (non-existent) joystick port. Incidentally, the Christmas tree graphic was produced using Simon Jonassen's web-based sgedit utility -- very handy!

Well, that is my little recap of this week's progress. I am pleased with my results so far! The biggest challenge has been figuring-out how to port 6809 code to it's poorer cousin, the 6803. Most of the Xmas Rush graphics data should remain intact, and most algorithms will be virtually the same (albeit written for the register-poor 6803). The biggest problem I forsee right now will be dealing with the single, fixed video buffer on the MC-10. I don't think that problem will be too tough, but there could be problems.  If you want to see how that goes, then you will just have to stay tuned!

Tuesday, January 5, 2016

A Little Prodding Helps!

More than half-way through the first week of RC2016/01, and not surprisingly I am barely getting started. Slow Retrochallenge starts seem to have become a habit for me! Hopefully this competition will at least end better for me than the last one did...


Desktop MC-10

The biggest reason for my sluggish Retrochallenge start is that I was still finishing-up some stuff I started during my extended end-of-the-year break in December. I generally use this yearly ritual to enjoy some retro projects and similar work, and this time was no exception. Along with a few soldering projects and other fun, I used a big portion of my time to straighten-up some of the disaster that is my office/bonus-room/man-cave. This time I even managed to clear-off the top of my desk and to reduce the biggest pile of junk to a somewhat smaller, mostly orderly pile of boxes!

One of the soldering projects from my December break involved installing a composite video output circuit to my MC-10. (These are available from Ed Snider, and are highly recommended to other MC-10 owners!) The newly composite-capable MC-10 pairs nicely with an 8 inch Night Owl LCD monitor on my newly cleaned-off desk. In fact, I have it situated in such a way that the "cassette" interface cable easily reaches my development laptop, allowing me to load code on the MC-10 by simply playing a WAV file on my laptop.

Wake-Up Calls

We are far enough into the Retrochallenge event that I had started to get "how's it going?" inquiries from a few friends. This includes inquiries from CoCo (and MC-10) "demo coder" Simon Jonassen, fellow Retrochallenge competitor Brett Gordon, and even my old Linux colleague Alan Cox. In particular, the latter inquiry proved to be enough to get me moving.

I had no idea that Alan had been working on a project to enable playing the old Scott Adams adventure games on the MC-10. Anyway, Alan suggested using a slightly modernized version of the old Motorola freeware assembler which is maintained for Unix-like systems. He also pointed me at a tool he had written for making cassette images for loading machine code. I had solutions for these problems, but apparently Alan's code (and his interest in my project) was enough to get me off the couch -- thanks, Alan!

In Motion

Back in March of 2014, there was a thread on the MC-10 Yahoo group about getting an ASCII-based animation running on the MC-10 and other 6847-based machines. I had participated in this effort, and had some MC-10 assembly code as my result. As part of getting started with this project, I got that code out and running on the MC-10 on my desk. I also ported the code I had from my earlier Micro Chroma 68 build I did as a Retrochallenge project. I now feel "in the groove" for writing some new code for the MC-10.


Originally, I had envisioned porting a monitor program for loading code over the serial port. However, the rebuild/reload/run cycle with my current arrangement seems fairly quick and is convenient enough to make me question the need for that porting effort. Also, the MC-10 emulator support on Linux is better than I had realized, including both MAME/MESS support and a surprisingly good Javascript-based emulator at mc-10.com.  The latter even includes the ability to load cassette images from local storage directly into emulator memory -- fast and easy! So, I may just live with what I have and save the serial-port stuff for another day...maybe...

Anyway, I suppose this post marks that I am officially underway with the RC2016/01 competition.  If you want to see where I end-up, then I guess you will just have to stay tuned...

Thursday, December 31, 2015

Xmas Rush for the MC-10

It's been a while since I've posted on this blog. I guess it is time to change that! A little Retrochallenge action should liven things up for a bit...

Christmas Spirit

Late in 2015 (just before the Thanksgiving holiday in the USA), I started thinking about making a Christmas-themed game for the Tandy Color Computer (CoCo). The original idea was to have a collection of 3 or 4 "mini games" with silly holiday references. I got started on that by putting together a simple game engine that used one of the 4-color modes on the CoCo. I tried to save time by taking the easy choice as often as possible, implementing simple blocky animation mechanisms, etc. Nevertheless, the holiday season brought with it some extra demands on my time...

What's the Rush?

By mid-December or so I had only come-up with one mini-game, a scenario where you have to go into the forest, grab the last Christmas tree, and escape from the evil snowmen guarding the tree. It sounds silly, but it turned-out to be rather fun. With the self-imposed Christmas deadline approaching I decided to polish-up what I had and make the best of things by releasing it. The result was Xmas Rush!


Challenge Accepted

Now we are at the turn of the year. With the New Year comes the next Retrochallenge event, RC2016/01! I have been participating in Retrochallenge events for a number of years, although my entry this past summer was a bit lackluster. Nevertheless, Retrochallenge is generally time well spent. The competition aspect makes for great motivation to get things done and the event provides great inspiration as an opportunity to observe what others are doing. I will continue what has become a tradition for me by entering again this year.

But what is the entry? Well, one regret I had about the Xmas Rush development was that I didn't have time to document any of what I was doing. I suppose I could go back and do that as my entry, but that seems a bit lame. However, it occurred to me that the MC-10 should be able to run Xmas Rush reasonably well. Plus, I have wanted to do an MC-10 project for some time. So for RC2016/01 I will port Xmas Rush to the MC-10, documenting the process along the way. Hopefully everyone will enjoy that!

The MC-10 is a different processor than the CoCo, and it has a number of architectural limitations that make it more challenging as a development target that its more powerful cousin. Plus, I will have to put some work into building an MC-10 development environment that is friendly to my style of work. Hopefully none of those challenges in insurmountable...be sure to stay tuned!

Thursday, January 1, 2015

35 Colors On NTSC

Some time ago, I played with a "flicker" technique on the CoCo that alternated screens between a 9-color (8 + black) "Semi-Graphics" mode and a 4-color "Color Graphics" mode on the VDG.  I combined that with a technique for teasing 8 on-screen colors out of the 4-color mode, giving a combined effect of 44 on-screen colors!  I then used this technique as the basis for showing puzzle pictures in Sluzzle, and there was much rejoicing...

Not long after that, I conceived of a similar technique that might solve at least a few of the problems with the 44 color technique at the cost of reducing the palette size a bit.  I let that sit dormant for a while, until I decided to play with it a bit during my 2014 year-end break.  Below I will describe the 35 color technique for those interested in hearing more.

Problems Abound

The main problem with the 44 color "flicker" technique is, of course, the flicker.  The mode toggles between the two base video modes (SG24 and G6C) at the vertical refresh rate (60 Hz on NTSC machines, 50 Hz on PAL machines).  This produces a noticeable flicker effect on the displayed screen, but unfortunately this is inherent to any "flicker" technique -- sorry!

The bigger problem with the 44 color technique is that it uses the aforementioned technique to get 8 on-screen colors from the 4-color modes of the VDG.  This is achieved by switching the colorset select (i.e. CSS) input to the VDG while a line is being drawn on the screen!  This changes between the two 4-color palettes as much as 8 times per line, doubling the amount of available colors on the screen.  (This 8 color technique is also used in the CoCo version of the Imagic game Dragonfire.)  Unfortunately the extra colors come at the cost of eating-up all of the available CPU while the 4- (now 8-)color screen is being drawn.

The other big problem with the 44 color technique is that only one of the base modes offers the (non-)color black.  Since the observed color at a given position is the average of the colors at that position on the two base screens, then no position will ever be observed as fully black.  Although the available dark blue is a workable substitute in many case, the lack of a true black is a big handicap for the 44 color technique.

Another Option

If we accept that we will only get 4 on-screen colors from the 4-color base screen, then we could free-up more CPU time while that screen is displayed.  This would allow more time for implementing a game, playing background music, or whatever else.  But what about black pixels?

It turns out that on NTSC CoCos (and Dragons), there is another option.  Due to how an NTSC video signal encodes color (and how the CoCo video circuit is implemented), the black and white 256x192 VDG mode (G6R) is effectively a 128x192 4-color mode offering black, white, "blue" (cyan?), and "red" (orange?) pixels.  This mode was very popular for commercial games on the Coco, and it is similar to using 4 of the 6 available colors in the high-res video mode of the Apple II.

So now we can use a "flicker" technique to alternate between the G6R (i.e. PMODE4) "artifact color" mode and a Semi-Graphics mode.  Since the G6R mode also offers black and white, both of those colors are still available when using the "flicker" technique!  I will show the available palette below.

Semi-Graphics Palette
G6R "artifact color" Palette
Combined "Flicker" Palette
Issues Remain

As you can see, this technique yields a nice range of usable colors from the VDG.  It does, of course, still have a perceivable "flicker" on the screen.  Also, the technique is limited to being used on NTSC displays.  Of course, the G6R mode can be used as a 2-color mode on PAL machines -- by ignoring the "artifact" colors, this could also be viewed as a 17 color technique...

Sticking with the NTSC world, there is one other problem.  Anyone familiar with the use of "artifact" colors on the CoCo will know that the patterns corresponding to "blue" and "red" switch at random between resets.  That is why so many CoCo games have an introductory screen with a message like "reset until this is blue".  Any program using the 35 color technique described above will need to address the same issue.

Finally, I am seeing some distortion to the colors near the top of the display.  I have not yet determined why that is happening, and it might relate to something else I am doing -- YMMV.  I'm going to downplay this distortion for now, as I believe it is likely caused by something outside the core of the technique described herein.

44 Color Me
35 Color Me
Nevertheless, I am getting decent results in my picture conversions using this 35 color technique.  The availability of a true black makes a huge difference, and that largely compensates for the ~20% smaller color palette available versus the 44 color technique.  With a lot more CPU time available, this technique could be useful for any number of programs on the CoCo including games.

I'm not planning to do anything more with this technique right now, but I hope someone else finds it useful.  I'm happy to consult with anyone that wants to try to make use of it.  In any case, be sure to stay tuned... :-)

Sunday, February 2, 2014

Working Keyboard

The end of the Retrochallenge event freed me to work a bit on spiffing-up my Micro Chroma 68 project.  Still not happy with the keyboard hack, I looked in earnest at how I might fix things...

Keyboard Adapter One-Shot Timer Circuit
One Shot

My earlier experiences with the PS/2 keyboard adapter and my own hack to make it work with the Micro Chroma 68 suggested that either the adapter was not generating a strobe signal at all or that the TVBug software was just missing it.  The former seemed unlikely, so I set about to address the latter issue.

Mike Willegal's documentation for the keyboard adapter tells me that the adapter should be generating an 125 microsecond pulse for every keystroke.  That translates to a little more than 100 CPU cyles during the strobe signal.  But since the strobe signal is not generating an interrupt, those 100 cycles may not be enough for TVBug to see the strobe if it happens to be doing something else.

To address this, I fed the strobe signal from the adapter to a 555 timer circuit configured as a one-shot timer.  This allowed me to send a longer strobe to the Micro Chroma 68 after a keystroke.  Experimenting a bit led me to use a 470 kΩ resistor and 0.1 μF capacitor, resulting in a pulse width of 47 milliseconds -- quite a bit longer than what the adapter was generating!  These values give me very reliable and pleasant keyboard entry, with no need for a magic "repeat the last key" button... :-)

Grounded Out

The Micro Chroma 68 keyboard interface also has provisions for 4 directional keys and a "Home" key.  I guess these allow for moving the cursor around on the screen, but I'm not sure I see the usefulness.  Anyway, letting them float was resulting in some random cursor movement as can be seen in some of my videos posted previously.  While mucking with the keyboard interface, I took the opportunity to ground all of those signals.  This also resulted in a much more pleasant TVBug experience.

Happy Camper

So, that is one less nagging problem with my Micro Chroma 68 build!  As long as I restrain myself from typing more than 20 characters per second (which would be 240 words per minute), the keyboard is quite usable.  With the new modifications I was able to reenter my flashing "Retrochallenge Rulez!" program much more easily than before.

Anyway, it looks like the Retrochallenge event was extended until midnight GMT today.  I wanted to get this posted so that my entry gets full consideration... :-)  But in all seriousness, I do hope and expect to keep poking at this one a bit more for a little while longer.  So, as always...stay tuned!

Friday, January 31, 2014

Final Lap

The end of the month is here, and with it the end of the Retrochallenge Winter Warmup for 2014.  Despite a few setbacks and unresolved issues, this has been a good one for me!  But before I declare my contest entry complete, I should at least get some code running on this thing... :-)

The Micro Chroma 68 Expressing Enthusiasm
Some Assembly Required

The TVBug monitor on the Micro Chroma 68 board only provides for loading machine language programs.  So, the first step is to find an assembler for the Motorola 6800 family of processors that runs under my Linux development environment.  There are a number of options for this, although it seems that not all assemblers are created equalWe must be cautious...

In the days of yore, Motorola operated a "freeware" BBS.  This BBS was originally available by modem, and later over the Internet as an FTP site.  Some of the files there included source code for assemblers targeting the full range of Motorola's CPUs, including the 6800.  Alas, the site is no more and it is now difficult to find even an old archive of it.  If anyone knows of such an archive, please leave that info as a comment on this blog -- that site needs to be preserved!

Fortunately, others found those tools useful as well.  While the unadulterated sources are difficult to find, a variety of slightly modified versions are available from a number of places.  So for now, I will be using the as0 assembler from the "masm" package.  It is a simple tool, but it more then fills the need at hand.

Manual Code Entry In TVBug
Hands-On Programming

The Micro Chroma 68 is already limited in its accessibility for loading programs, providing only an audio cassette interface and the video/keyboard console.  Since I still have not received those magic capacitors for the audio interface, I am limited to just the console for loading programs.  The keyboard adapter I am using has a provision for input through a serial port instead of the PS/2 keyboard.  But with the continuing "repeated characters" problem, manual intervention is still required.

Undaunted, I wrote a small program to put a message on the screen and assembled it with the assembler mentioned above.  The assembler outputs an S-record file by default, but I also had it generate a listing file.  That file had the hexadecimal values for all of the opcodes and data for my program, allowing me to enter them into the TVBug monitor manually.  The process was a bit tedious, but it was manageable for the small program in question.

Flash In The Pan

The original program merely cleared the display and wrote a new message to the screen.  At the end of the program, I simply inserted a busy loop to keep my display on the screen rather than returning to TVBug.  This worked fine, but didn't have quite enough sizzle for the end of the project.  I checked the schematics and found that the video timing signals from the VDG are available for the CPU to read on the Micro Chroma 68.  So, I changed the end of the program to spin in a loop that counted those timing signals and periodically changed the display's color set between the green and orange options -- exciting! ;-)

While changing the end of the program, I was faced with a problem.  I had structured the code such that adding new code at the end would force me to reenter a significant amount of data in memory.  My inherent laziness kicked-in and found another option -- I just jumped over the data and added new code at the end.  Maybe this sheds some light on why "spaghetti code" was so prevalent and accepted back in the early days of computing?  Or, maybe I am just a horrible coder...


Thus ends my entry for the Retrochallenge Winter Warmup 2014 event.  I hope that all of you have enjoyed reading about it as much as I have enjoyed doing it!  A few problems remain, and ultimately I would like to do something a bit more useful (or at least something more interesting) with the Micro Chroma 68 in the future.  I can't help but think that it would look right at home mounted inside of an arcade cabinet...hmmm...

Well, anyway...if you want to find-out what happens in the future then you'll just have to stay tuned!