Tackling the Logiker 2022 Vintage Computing Christmas Challenge – part 3

See also: part 1, part 2, part 3, part 4, part 5, part 6 and part 7.

So far, we’ve taken a brute force PRINT program and turned it in to a less-brute force program that did the same thing using DATA statements:

0 ' LOGIKER8.BAS
10 CLS
15 CH=32:PRINTTAB(6);
20 READ A:IF A=-1 THEN 220
25 IF A=0 THEN PRINT:GOTO 15
30 PRINT STRING$(A,CH);
35 IF CH=32 THEN CH=42 ELSE CH=32
40 GOTO 20
50 DATA 5,1,7,1,0
60 DATA 5,2,5,2,0
70 DATA 5,3,3,3,0
80 DATA 5,4,1,4,0
90 DATA 1,17,0
100 DATA 2,15,0
110 DATA 3,13,0
120 DATA 4,11,0
130 DATA 5,9,0
140 DATA 4,11,0
150 DATA 3,13,0
160 DATA 2,15,0
170 DATA 1,17,0
180 DATA 5,4,1,4,0
190 DATA 5,3,3,3,0
200 DATA 5,2,5,2,0
210 DATA 5,1,7,1,0
215 DATA -1
220 GOTO 220

All of this in an effort to try to print out this image:

While there are still many BASIC optimizations we could do (removing spaces, combining lines even further, renumbering by 1, etc.), those would apply to any version of the code we create. Instead of doing that, let’s look at some other ways we can represent this data.

Simpsons Atari 2600 did it first.

Let no meme go to waste, I always say.

When the Atari VCS came out in 1977 (you younguns may only know it as the 2600, but it didn’t get that name until 1982 — five years after its release later), it required clever tricks to make games run in only 1K or 2K of ROM and with just 128 bytes of RAM.

The game Adventure was quite the challenge, since it had multiple screens representing different mazes, castles and areas.

Atari’s Greatest Hits on an old iPad

Each screen was represented by only 21 bytes of ROM! If you follow that link, you can read more about my efforts to understand how this worked. Here is an example of how the castle room was represented:

;Castle Definition                                                                                                 
CastleDef:
  .byte $F0,$FE,$15 ;XXXXXXXXXXX X X X      R R R RRRRRRRRRRR                                      
  .byte $30,$03,$1F ;XX        XXXXXXX      RRRRRRR        RR                                      
  .byte $30,$03,$FF ;XX        XXXXXXXXXXRRRRRRRRRR        RR                                      
  .byte $30,$00,$FF ;XX          XXXXXXXXRRRRRRRR          RR                                      
  .byte $30,$00,$3F ;XX          XXXXXX    RRRRRR          RR                                      
  .byte $30,$00,$00 ;XX                                    RR                                      
  .byte $F0,$FF,$0F ;XXXXXXXXXXXXXX            RRRRRRRRRRRRRR   

There are three bytes to represent each line. Three bytes would only be able to represent 24 pixels (8 bits per byte), and the ASCII art shows the screen width is actually 40. Those three bytes cannot represent the entire row of pixels.

In fact, 4-bits of that isn’t used. Each set of three bytes represents halfa row (20 bits out of the 24 the three bytes represent). Look at the first entry:

  .byte $F0,$FE,$15 ;XXXXXXXXXXX X X X      R R R RRRRRRRRRRR   

If you turn those bytes into binary, you get this pattern:

 byte 1  byte 2  byte 3|
--------========--------
111100001111111000010101

The Atari drew the first 8-bits from least significant bit to most. the second 8-bits from most significant to least, then the third from least significant to most. That makes it look like this, matching the ASCII art (skipping the unused 4-bits):

000011111111111010101000
    XXXXXXXXXXX X X X   

To represent a full screen, the Atari had a trick that would mirror or duplicate the other half of the screen. In the case of the castle, the right side was a mirror image. In the case of certain mazes, the data was duplicated.

Looking at our image here, since it is symmetrical, we could certainly use the same trick and only store half of the image.

+----------------+
|           *    |
|           **   |
|           ***  |
|           **** |
|       *********|
|        ********|
|         *******|
|          ******|
|           *****|
|          ******|
|         *******|
|        ********|
|       *********|
|           **** |
|           ***  |
|           **   |
|           *    |
+----------------+

Also, since the top and bottom are also mirror images, we could mirror those, too, and get away with only storing 1/4 of the image:

+----------------+
|           *    |
|           **   |
|           ***  |
|           **** |
|       *********|
|        ********|
|         *******|
|          ******|
|           *****|
+----------------+

Since the image is 17×17 (an odd number, so there is a halfway row and column), we’d actually need to just draw to that halfway row/column, then reverse back through the data.

We should be able to take our existing data and crop it down from this, which represents the full image:

50 DATA 5,1,7,1,0
60 DATA 5,2,5,2,0
70 DATA 5,3,3,3,0
80 DATA 5,4,1,4,0
90 DATA 1,17,0
100 DATA 2,15,0
110 DATA 3,13,0
120 DATA 4,11,0
130 DATA 5,9,0
140 DATA 4,11,0
150 DATA 3,13,0
160 DATA 2,15,0
170 DATA 1,17,0
180 DATA 5,4,1,4,0
190 DATA 5,3,3,3,0
200 DATA 5,2,5,2,0
210 DATA 5,1,7,1,0

…to this, which represents the top left quarter-ish of the image:

50 DATA 5,1,4,0   '     X    '
60 DATA 5,2,3,0   '     XX   '
70 DATA 5,3,2,0   '     XXX  '
80 DATA 5,4,1,0   '     XXXX '
90 DATA 1,9,0     ' XXXXXXXXX'
100 DATA 2,8,0    '  XXXXXXXX'
110 DATA 3,7,0    '   XXXXXXX'
120 DATA 4,6,0    '    XXXXXX'
130 DATA 5,5,0    '     XXXXX'

That represents all the data up to the center row/column, and that seems to be a considerable savings in code space (removing eight lines).

But how do we draw that forward, then in reverse? There is no way to back up when using the READ command, so we’d have to remember what we just did. For a general purpose “compress 1-bit image” routine it would be more complex, but since we know the image we are going to produce, we can make an assumption:

  1. The image never has more than three transitions (space, asterisk, space) in a line.
  2. No line entry has more than 4 numbers total.

Knowing that, we could simply save up to three numbers in variables, so we would print them out A B C and then C B A. We won’t even need the zeros now, since we can read A,B,C and act on them (stopping if C is 0).

Neat!

A quick bit of trial and error gave me this code that will print the top half of the image:

0 ' LOGIKER10.BAS
10 CLS
15 CH=32:PRINTTAB(6);
20 READ A:IF A=-1 THEN 220
21 PRINT STRING$(A,32);
22 READ B,C
25 IF C=0 THEN PRINT STRING$(B*2-1,42);STRING$(A,32):GOTO 15
30 PRINT STRING$(B,42);STRING$(C*2-1,32);STRING$(B,42)
40 GOTO 15
50 DATA 5,1,4
60 DATA 5,2,3
70 DATA 5,3,2
80 DATA 5,4,1
90 DATA 1,9,0
100 DATA 2,8,0
110 DATA 3,7,0
120 DATA 4,6,0
130 DATA 5,5,0
215 DATA -1
220 GOTO 220

It creates this:

I can now say “we are halfway there!”

But now I have another issue to solve. How do I back up? There is no way to READ data in reverse. It looks like I’m going to need to load all those numbers in to memory so I can reverse back through them.

To be continued…

OpenAI writes a BASIC program!

Note: A slightly updated version of this article appeared earlier on Vintage is the New Old.

I asked:

can you write a program in Microsoft BASIC that encodes a message and can decode it later?

– me, to chat.openai.com

It initially showed me this:

REM This is a simple program that uses a substitution cipher to encode and decode a message in Microsoft BASIC

REM Define the alphabet and the substitution key
DIM alphabet(26)
DIM key(26)
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
key = "QWERTYUIOPASDFGHJKLZXCVBNM"

REM Define the message to encode
DIM message(40)
message = "HELLO, WORLD!"

REM Define the encoded message
DIM encoded(40)

REM Define the decoded message
DIM decoded(40)

REM Encode the message
FOR i = 1 TO LEN(message)
  encoded(i) = key(INSTR(alphabet, UCASE$(message(i))))
NEXT

REM Print the encoded message
FOR i = 1 TO LEN(encoded)
  PRINT encoded(i);
NEXT

REM Decode the message
FOR i = 1 TO LEN(encoded)
  decoded(i) = alphabet(INSTR(key, UCASE$(encoded(i))))
NEXT

REM Print the decoded message
FOR i = 1 TO LEN(decoded)
  PRINT decoded(i);
NEXT

Well, that might be some type of BASIC, but it wasn’t what I meant. So I added:

convert that to older interpreted BASIC, which requires line numbers. Make sure no variables are longer than two characters. All variables should be in uppercase.

– me, to chat.openai.com

It took a few back-and-forths as it generated code that wasn’t quite compatible with my CoCo. It initially was using strings similar to how C works, declaring a string array for the number of letters in the string. It would then try to access them as A$(1), A$(2), etc. That seemed silly, and I told it so.

It removed the DIMs and just started using strings normally.

When it was trying to build a string that way, I’d tell it “my BASIC can’t do that. use MID$ instead” and it would switch out.

A few rounds of this, and is had created a program that almost worked. There were some errors, caused by the INSTR command returning 0. I told it “if the string is not found, INSTR returns 0 which cannot be used in that MID$”.

It corrected that.

It worked, but I didn’t like the output, so I told it:

print the original message at the start. you can remove the ; on teh PRINT statements so they print a carraige return

– me to chat.open.ai

And at least, this AI-generated masterpiece was complete. And I didn’t write a line of it — I just told it when something wouldn’t work on my BASIC…

10 REM Simple substitution cipher
20 A$="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
30 K$="QWERTYUIOPASDFGHJKLZXCVBNM"
40 M$="HELLO, WORLD!"
50 PRINT "Original message: "; M$
60 E$=""
70 FOR I=1 TO LEN(M$)
80 C=INSTR(A$,CHR$(ASC(MID$(M$,I,1))))
90 IF C>0 THEN E$=E$+MID$(K$,C,1) ELSE E$=E$+MID$(M$,I,1)
100 NEXT
110 PRINT "Encoded message: "; E$
120 D$=""
130 FOR I=1 TO LEN(E$)
140 C=INSTR(K$,CHR$(ASC(MID$(E$,I,1))))
150 IF C>0 THEN D$=D$+MID$(A$,C,1) ELSE D$=D$+MID$(E$,I,1)
160 NEXT
170 PRINT "Decoded message: "; D$

And, I could have kept giving it more suggestions on what to do. Make them in to subroutines? Sure! Change the encoding method? No problem.

BASIC written by the OpenAI!

We live in wondrous times, for sure!

Tackling the Logiker 2022 Vintage Computing Christmas Challenge – part 2

See also: part 1, part 2, part 3, part 4, part 5, part 6 and part 7.

The design is one row taller than will fit on the CoCo’s 32×16 text screen, but it would easily fit on the 40 or 80 column screen of the CoCo 3. For this article, I am going to stick with the standard text screen and just let it scroll one row off the top of the screen. When I have something figured out, it might only require modifying the centering code to display on the 40/80 column screen.

Let there be code!

At this stage, the design is being centered using the TAB command. Putting a “TAB(7)” at the start of each string takes up 3 bytes of programming space. It seems “TAB(” is tokenized, then there is the 3 character, followed by the “)” character. I had thought using PRINT@ might save some space, but the “@” takes a byte, then the screen position numbers follow it, and a comma is required. “PRINT@7,” takes the same amount of code space as “PRINTTAB(7)” so no savings there.

The biggest savings is going to come from eliminating the repeated use of the “* characters in the strings. Since the entire image is made up of spaces or asterisks, it could be represented by data that says how many spaces then how many asterisks then how many spaces, etc.

Here is what the image looks like centered to 32-columns:

+--------------------------------+
|           *       *            |
|           **     **            |
|           ***   ***            |
|           **** ****            |
|       *****************        |
|        ***************         |
|         *************          |
|          ***********           |
|           *********            |
|          ***********           |
|         *************          |
|        ***************         |
|       *****************        |
|           **** ****            |
|           ***   ***            |
|           **     **            |
|           *       *            |
+--------------------------------+

The first line has 11 spaces, 1 asterisks, 7 spaces, then 1 asterisk.

The second line has 11 spaces, 1 asterisks, 5 spaces, then 2 asterisks.

And so on… The first line could be represented in DATA to tell how many repeating groups of spaces and asterisks there are such as 11,1,7,1,0 (using 0, to indicate the end of the line).

For something this small, I could have just counted up the spaces and asterisks myself, but it’s more fun to spend ten minutes writing a BASIC program to do what would have taken me 60 seconds to do manually.

I converted the pattern in to DATA statements and created a program that would scan each line looking for repeated characters. When the character changes, it prints how many of the previous repeating characters it saw and resets the counter.

0 ' LOGIKER5.BAS 
1 ' LC$=LAST CHAR
 2 ' DC=DUPLICATE CHAR COUNT
 10 READ A$:IF A$="END" THEN END
 11 LC$=LEFT$(A$,1):DC=1
 12 FOR I=2 TO LEN(A$)
 13 IF MID$(A$,I,1)=LC$ THEN DC=DC+1 ELSE PRINT DC",";:DC=1:LC$=MID$(A$,I,1)
 14 NEXT
 15 PRINT DC", 0"
 16 LC$=""
 17 GOTO 10
 50 DATA"    *       *"
 60 DATA"    **     **"
 70 DATA"    ***   ***"
 80 DATA"    **** ****"
 90 DATA"*****************"
100 DATA" ***************"
110 DATA"  *************"
120 DATA"   ***********"
130 DATA"    *********"
140 DATA"   ***********"
150 DATA"  *************"
160 DATA" ***************"
170 DATA"*****************"
180 DATA"    **** ****"
190 DATA"    ***   ***"
200 DATA"    **     **"
210 DATA"    *       *"
220 DATA "END"

Rather than explain how the program works, like I often try to do, I just submitted this code to the OpenAI ChatGPT system and let it do the work for me:

This BASIC program reads in a series of strings, one at a time, and outputs the number of times each character appears in the string.

Here is a brief explanation of the code:

  • Lines 10-16: This is the main loop of the program, where each string is read in, processed, and output.
  • Line 11: This line initializes the LC$ variable to the first character of the string and the DC variable to 1.
  • Lines 12-14: This is a FOR loop that iterates through each character of the string.
  • Line 13: This line compares the current character with the last character (stored in LC$). If they are the same, it increments the DC variable. If they are different, it outputs the value of DC and then resets DC to 1 and LC$ to the current character.
  • Line 15: This line outputs the value of DC after the loop has finished processing the string.
  • Lines 50-220: These are the data lines that contain the strings to be processed.
  • Line 220: This is the sentinel value that indicates the end of the data.

Note that this code uses a number of built-in BASIC functions, such as LEFT$, MID$, and LEN, which may not be available in all versions of BASIC. You may need to modify the code to use equivalent functionality if it is not available in the version of BASIC you are using.

– chat.openai.com

Seriously, folks. You have to go play with ChatGPT. It’s amazingly fun.

OpenAI ChatGPT Digression

After showing this program to ChatGPT, I asked “is there a way to do it better?” It responded with:

There are a few ways that this routine could be improved. Here are a few suggestions:

  1. Use a DO loop instead of a GOTO statement to control the main loop. This would make the code easier to read and maintain.
  2. Use a SELECT CASE statement to handle the different cases in line 13 instead of an IF statement. This would make the code more concise and easier to read.
  3. Use a DICTIONARY or MAP data structure to store the counts of each character instead of using separate variables for each character. This would make the code more efficient and allow it to handle an arbitrary number of characters without having to hard-code variables for each one.
  4. Add error-checking to the code to handle cases where the input data is invalid (e.g. empty strings, strings with invalid characters, etc.). This would make the code more robust and prevent it from crashing or producing incorrect results in these cases.

Here is an example of how the code could be modified to incorporate some of these changes:

– chat.openai.com

The code it provided was not something that would work on ancient Microsoft BASIC, but it was interesting to see:

DO
  READ A$
  IF A$="END" THEN EXIT DO

  ' Initialize the character counts to 0
  CHAR_COUNTS = DICTIONARY()

  ' Loop through each character of the string
  FOR i = 1 TO LEN(A$)
    ch = MID$(A$, i, 1)
    IF ch = " " THEN CONTINUE ' Skip whitespace

    ' Increment the count for this character
    IF CHAR_COUNTS.EXISTS(ch) THEN
      CHAR_COUNTS[ch] = CHAR_COUNTS[ch] + 1
    ELSE
      CHAR_COUNTS[ch] = 1
    END IF
  NEXT

  ' Output the counts for each character
  FOR ch = "A" TO "Z"
    PRINT CHAR_COUNTS[ch]; ",";
  NEXT
  PRINT "0"
LOOP

50 DATA"    *       *"
60 DATA"    **     **"
70 DATA"    ***   ***"
80 DATA"    **** ****"
90 DATA"*****************"
100 DATA" ***************"
110 DATA"  *************"
120 DATA"   ***********"
130 DATA"    *********"
140 DATA"   ***********"
150 DATA"  *************"
160 DATA" ***************"
170 DATA"*****************"
180 DATA"    **** ****"
190 DATA"    ***   ***"
200 DATA"    **     **"
210 DATA"    *       *"
220 DATA "END"

I have no idea what version of BASIC that code would work on, and the program it wrote doesn’t really look like it’s doing the same thing, but still neat. In a future article (provided they continue to give us access to this AI), I plan to share some of the interesting things it can do with BASIC programs (including writing them from scratch).

But I digress…

Driving Miss Data

When I run the program, it starts printing out rows of numbers. Since there are seventeen rows of the graphic, it prints out seventeen lines of numbers. I needed to pause the program mid-printing so I can write down the numbers before they scroll off. (A fancier way would have been to have the program write out an ASCII BASIC program with the line numbers and DATA statements already in it which I could load back later. I think I showed how I do that in an earlier article somewhere on this site.)

Now that we have numbers for our DATA statements, a new version of the program could be written to generate the image using them. I will make use of the STRING$ that creates a string of a specific character of a specific length…

0 ' LOGIKER6.BAS
10 CLS
15 CH=32:PRINTTAB(7);
20 READ A:IF A=-1 THEN 220
25 IF A=0 THEN PRINT:GOTO 15
30 PRINT STRING$(A,CH);
35 IF CH=32 THEN CH=42 ELSE CH=32
40 GOTO 20
50 DATA 4,1,7,1,0
60 DATA 4,2,5,2,0
70 DATA 4,3,3,3,0
80 DATA 4,4,1,4,0
90 DATA 17,0
100 DATA 1,15,0
110 DATA 2,13,0
120 DATA 3,11,0
130 DATA 4,9,0
140 DATA 3,11,0
150 DATA 2,13,0
160 DATA 1,15,0
170 DATA 17,0
180 DATA 4,4,1,4,0
190 DATA 4,3,3,3,0
200 DATA 4,2,5,2,0
210 DATA 4,1,7,1,0
215 DATA -1
220 GOTO 220

Obviously those data statements could be combined in to fewer lines, but for this version I wanted them to match the same line number the original PRINT was on. You can easily compare the results:

50 DATA"    *       *"
50 DATA 4,1,7,1,0
 
60 DATA"    **     **"
60 DATA 4,2,5,2,0

70 DATA"    ***   ***"
70 DATA 4,3,3,3,0

80 DATA"    **** ****"
80 DATA 4,4,1,4,0

Before I show you the results, can you see the flaw in my program?

I’ll give you a hint… Line 170.

Close but no cigar

My program assumes each line starts with a space, so the first value will be printed as spaces, then the next value as asterisks, and so on. This causes a problem when it gets to the row that is entirely the asterisk it reads the first number and prints it as spaces, giving me this incorrect result:

I can think of several ways to solve this:

  1. Use a different value other than 0 for “end-of-line” and make 0 mean “nothing to print, just switch to the astrisk”. That would change line 170 to be “DATA 0,17,X” (where “X” is the new end-of-line marker. This would probably require a new bit of IF logic to handle.
  2. Make each group of data two bytes that specifies the character to print, and how many. Printing 17 asterisks would be “17,42”. Printing four spaces would be “4,32”. This would make the program logic simpler, but would double the size of the data. Depending on how much smaller the logic is, this might be a winner. (And I can think of optimizations to that as well, such as using 0 and 1 for the data to print and just printing “32+X*10” so it prints either 32 (if the value is 0) or 42 (if the value is 1). This is normally how I would have started, but I was trying to make the data as small as possible.
  3. I could just encode the leading spaces at the start of each line rather than using TAB(7). By doing this, every line would start with a space. This would work for this specific challenge, but not be flexible for patterns that don’t start with a space.

For now, let’s make a quick change and try #3 by simply adding 7 to the first number in each DATA statement, and adding a 7 to line 170 which is the row that doesn’t have a space at the start. I think it would look like this:

0 ' LOGIKER7.BAS
10 CLS
15 CH=32
20 READ A:IF A=-1 THEN 220
25 IF A=0 THEN PRINT:GOTO 15
30 PRINT STRING$(A,CH);
35 IF CH=32 THEN CH=42 ELSE CH=32
40 GOTO 20
50 DATA 11,1,7,1,0
60 DATA 11,2,5,2,0
70 DATA 11,3,3,3,0
80 DATA 11,4,1,4,0
90 DATA 7,17,0
100 DATA 8,15,0
110 DATA 9,13,0
120 DATA 10,11,0
130 DATA 11,9,0
140 DATA 10,11,0
150 DATA 9,13,0
160 DATA 8,15,0
170 DATA 7,17,0
180 DATA 11,4,1,4,0
190 DATA 11,3,3,3,0
200 DATA 11,2,5,2,0
210 DATA 11,1,7,1,0
215 DATA -1
220 GOTO 220

Running this program produces the desired results! But, it has a drawback:

The data size grew. Not only did we add “7,” (two bytes) to line 170, but eleven other lines went from a 1-digit value to a 2-digit value. This means our data grew by 13 bytes. If we saved 13 bytes in the decoding routine, this is a win. If we did not, it is not an acceptable fix.

When I load the previous version of the program in to the XRoar emulator and PRINT MEM, it shows 22425 free. When I do the same with this version, I get 22416 — less memory free, so a larger program. This is bad, but the previous version is still missing the code to handle that line 170.

Perhaps, instead of adding 7 to each line to center on the screen, each line could just add 1 (so it doesn’t create two-digit values) and we can use TAB(6). That would look like this:

0 ' LOGIKER8.BAS
10 CLS
15 CH=32:PRINTTAB(6);
20 READ A:IF A=-1 THEN 220
25 IF A=0 THEN PRINT:GOTO 15
30 PRINT STRING$(A,CH);
35 IF CH=32 THEN CH=42 ELSE CH=32
40 GOTO 20
50 DATA 5,1,7,1,0
60 DATA 5,2,5,2,0
70 DATA 5,3,3,3,0
80 DATA 5,4,1,4,0
90 DATA 1,17,0
100 DATA 2,15,0
110 DATA 3,13,0
120 DATA 4,11,0
130 DATA 5,9,0
140 DATA 4,11,0
150 DATA 3,13,0
160 DATA 2,15,0
170 DATA 1,17,0
180 DATA 5,4,1,4,0
190 DATA 5,3,3,3,0
200 DATA 5,2,5,2,0
210 DATA 5,1,7,1,0
215 DATA -1
220 GOTO 220

Doing a PRINT MEM on that one shows 22421, so it is four bytes larger than the original, and still smaller than the “add 7” version. Perhaps that is good enough for now?

Combining all the lines to make a smaller program would look like this:

0 ' LOGIKER9.BAS
10 CLS
15 CH=32:PRINTTAB(6);
20 READ A:IF A=-1 THEN 220 ELSE IF A=0 THEN PRINT:GOTO 15
30 PRINT STRING$(A,CH);:IF CH=32 THEN CH=42 ELSE CH=32
40 GOTO 20
50 DATA 5,1,7,1,0,5,2,5,2,0,5,3,3,3,0,5,4,1,4,0,1,17,0,2,15,0,3,13,0,4,11,0,5,9,0,4,11,0,3,13,0,2,15,0,1,17,0,5,4,1,4,0,5,3,3,3,0,5,2,5,2,0,5,1,7,1,0,-1
220 GOTO 220

Better! But we can make it more better.

In the next installment, we will do something that I learned from studying the Atari 2600’s Adventure program…

To be continued…

I will never write a shell script again.

With my initial success using the CMOC compiler to build a CoCo program, I wanted to automate my steps (build and copy to disk image).

Back in my OS-9 days, I used an OS-9 shell script called “doit” that would manually do the steps to build whatever project it was designed for. This would do the compile, optimize, link, etc. steps for me.

I wanted to make a shell that let me specify the C file then build and copy it to some emulator disk image. I thought it might be like:

doit hello.c

I couldn’t remember how to pass parameters in to a shell script, so instead of doing a websearch, I just asked ChatGPT:

“how can you pass a parameter to a unix shell script?

To pass a parameter to a Unix shell script, you can use the $1, $2, $3, etc. variables, which correspond to the first, second, third, etc. arguments passed to the script. For example, if you want to pass a filename as a parameter to your script, you could use the following syntax:

./myscript.sh myfile.txt

Then, in the script, you can access the parameter using the $1 variable, like this:

#!/bin/bash

# Print the filename passed as a parameter
echo "The filename is: $1"

Alternatively, you can use the shift command to access all of the arguments passed to the script, like this:

#!/bin/bash

# Print all of the arguments passed to the script
echo "The arguments are: $@"

# Shift the arguments to the left, so that $1 now refers to the second argument, $2 now refers to the third argument, etc.
shift

# Print all of the arguments again, after shifting
echo "The arguments are now: $@"

In this example, the shift command is used to move all of the arguments to the left by one position, so that the second argument is now accessible using the $1 variable, the third argument is accessible using the $2 variable, and so on. This can be useful if you want to access all of the arguments passed to the script, but you don’t know in advance how many arguments will be passed.

– chat.openai.com

That was quicker than a Google search! I decided to just tell it what I wanted to do:

Write a shell script that takes one parameter (a name) and then runs “cmoc name” and then “decb copy name.bin C.DSK,NAME.BIN -2r”

Here is an example of a Unix shell script that takes one parameter (a name) and then runs the cmoc and decb commands using that parameter:

#!/bin/bash

# Get the name parameter
name=$1

# Run the cmoc command using the name parameter
cmoc $name

# Run the decb command using the name parameter
decb copy $name.bin C.DSK,$name.BIN -2r

In this script, the first line specifies the shell interpreter to use (in this case, /bin/bash), and the second line uses the $1 variable to retrieve the first argument passed to the script (i.e., the name parameter). The third and fourth lines use the cmoc and decb commands, respectively, to perform the specified operations using the name parameter.

To run this script, you would use the following command:

./myscript.sh myname

Replace myscript.sh with the name of your script file, and myname with the name you want to use as the parameter. The script will then run the cmoc and decb commands using the specified name.

– chat.openai.com

Well, if it can do that, maybe it an make it even better…

update that script so the parameter’s extension is removed and replaced with .bin for the copy

To update the script so that the extension of the parameter is removed and replaced with .bin for the decb copy command, you can use the basename and dirname commands to extract the base name and directory path of the parameter, respectively, and then use the sed command to remove the extension from the base name. Here is an example of how you could do this:

#!/bin/bash

# Get the name parameter name=$1

# Use the basename and dirname commands to extract the base name and directory path of the parameter
dir=$(dirname $name) base=$(basename $name)

# Use the sed command to remove the extension from the base name
name_without_ext=$(echo $base | sed -e "s/\.[^.]*$//")

# Run the cmoc command using the name parameter
cmoc $name

# Run the decb copy command using the modified name
decb copy $name_without_ext.bin $dir/C.DSK $name_without_ext.BIN -2r

In this updated script, the basename and dirname commands are used to extract the base name and directory path of the parameter. The sed command is then used to remove the extension from the base name, and the modified name is used as the argument to the decb copy command.

To run this script, you would use the same command as before:

./myscript.sh myname

This time, however, the decb copy command will use the modified name (i.e., the name with the extension removed and replaced with .bin) instead of the original name. For example, if you pass myfile.txt as the parameter, the decb copy command will use myfile.bin as the destination filename.

– chat.openai.com

And maybe even more better than that!

can you uppercase the second $name_without_ext in the decb line?

To uppercase the second instance of $name_without_ext in the decb line, you can use the tr command to convert the string to uppercase. Here is an example of how you could do this:

#!/bin/bash 

# Get the name parameter
name=$1

# Use the basename and dirname commands to extract the base name and directory path of the parameter
dir=$(dirname $name)
base=$(basename $name) 

# Use the sed command to remove the extension from the base namename_without_ext=$(echo $base | sed -e "s/\.[^.]*$//") 

# Use the tr command to convert the name to uppercase name_upper=$(echo $name_without_ext | tr "[:lower:]" "[:upper:]")

# Run the cmoc command using the name parameter
cmoc $name

# Run the decb copy command using the modified name decb copy $name_without_ext.bin $dir/C.DSK,$name_upper.BIN -2r

In this updated script, the tr command is used to convert the $name_without_ext variable to uppercase, and the resulting string is stored in the $name_upper variable. The $name_upper variable is then used as the second argument to the decb copy command, so the second instance of the name will be uppercase.

To run this script, you would use the same command as before:

./myscript.sh myname

This time, the decb copy command will use the uppercase version of the name as the second argument. For example, if you pass myfile.txt as the parameter, the decb copy command will be run as follows:

decb copy myfile.bin mydir/C.DSK,MYFILE.BIN -2r

Notice that the second instance of the name (i.e., MYFILE.BIN) is now in uppercase.

– chat.openai.com

It worked great! But it didn’t tell me what it was doing, so…

Make it print what is happening at the cmoc and decb steps

To make the script print what is happening at the cmoc and decb steps, you can simply add some echo statements to the script. Here is an example of how you could do this:


#!/bin/bash

# Get the name parameter
name=$1

# Use the basename and dirname commands to extract the base name and directory path of the parameter
dir=$(dirname $name) base=$(basename $name)

# Use the sed command to remove the extension from the base name
name_without_ext=$(echo $base | sed -e "s/\.[^.]*$//")

# Use the tr command to convert the name to uppercase name_upper=$(echo $name_without_ext | tr "[:lower:]" "[:upper:]")

# Print a message before running the cmoc command
echo "Running cmoc on $name..."

# Run the cmoc command using the name parameter
cmoc $name

# Print a message before running the decb copy command
echo "Running decb copy on $name_without_ext.bin..."

# Run the decb copy command using the modified name
decb copy $name_without_ext.bin $dir/C.DSK,$name_upper.BIN -2r

In this updated script, two echo statements have been added to print a message before running the cmoc and decb copy commands, respectively. These messages will be printed to the console and will indicate what is happening at each step of the script.

To run this script, you would use the same command as before:

./myscript.sh myname

When you run the script, you should see the following output:

Running cmoc on myname...
(output from cmoc command)
Running decb copy on myfile.bin...
(output from decb copy command)

The output from the cmoc and decb copy commands will be shown after the messages indicating what is happening at each step.

– chat.openai.com

And that’s how I got this…

I will never write a shell script again…

My first C program for CoCo DISK BASIC.

On this day in history … I built the CMOC compiler and compiled my first C program for non-OS-9 CoCO.

I created this source file:

int main()
{
	char *ptr = 1024;
	while (ptr < 1536) *ptr++ = 128;
	return 0;
}

I compiled it using “cmoc hello.c” and it produces “hello.bin”.

I created a new blank disk image using “decb dskini C.DSK”.

I copied the binary to that disk image using “decb copy hello.bin C.DSK,HELLO.BIN -2”

I booted up the XRoar emulator and mounted that disk image as the first drive.

I did LOADM”HELLO” and then EXEC.

And so it begins…

Tackling the Logiker 2022 Vintage Computing Christmas Challenge – part 1

See also: part 1, part 2, part 3, part 4, part 5, part 6 and part 7.

Here we go again! Over in the Facebook Color Computer group, David M. shared a link to this year’s Vintage Computing Christmas Challenge from Logiker. Although I did not submit an entry, I did play with last year’s challenge on my TRS-80 Color Computer.

Last year, it was this:

This year, the challenge is a bit more challenging. Per the challenge website, here is sample code for Commodore:

 10 print"{clear}"
 20 print""
 30 print""
 40 print""
 50 print"               *       *"
 60 print"               **     **"
 70 print"               ***   ***"
 80 print"               **** ****"
 90 print"           *****************"
100 print"            ***************"
110 print"             *************"
120 print"              ***********"
130 print"               *********"
140 print"              ***********"
150 print"             *************"
160 print"            ***************"
170 print"           *****************"
180 print"               **** ****"
190 print"               ***   ***"
200 print"               **     **"
210 print"               *       *"
220 goto 220

Starting with that un-optimized version, I will change it to work on the CoCo 1/2/3’s 32-column screen by adjusting it to be properly centered on that display.

 10 CLS
 50 PRINT"           *       *"
 60 PRINT"           **     **"
 70 PRINT"           ***   ***"
 80 PRINT"           **** ****"
 90 PRINT"       *****************"
100 PRINT"        ***************"
110 PRINT"         *************"
120 PRINT"          ***********"
130 PRINT"           *********"
140 PRINT"          ***********"
150 PRINT"         *************"
160 PRINT"        ***************"
170 PRINT"       *****************"
180 PRINT"           **** ****"
190 PRINT"           ***   ***"
200 PRINT"           **     **"
210 PRINT"           *       *"
220 GOTO 220

Unfortunately, this design is 17 rows tall, and the CoCo’s standard display is only 16. It won’t fit:

We should still be able to enter the challenge by having the program print this pattern, even if it scrolls off the screen a bit. To get one extra line there, we can get rid of the line feed at the end of the final PRINT statement in line 210 by adding a semi-colon to the end:

210 PRINT"           *       *";

And so it begins…

And so it begins

The goal is to make this as small as possible. There were many ways to approach last year’s Christmas tree challenge, and you can read about the results and a follow-up with suggestions folks gave to save a byte or two.

A simple thing is to remove the spaces at the front and replace them with the TAB() command:

 10 CLS
 50 PRINTTAB(7)"    *       *"
 60 PRINTTAB(7)"    **     **"
 70 PRINTTAB(7)"    ***   ***"
 80 PRINTTAB(7)"    **** ****"
 90 PRINTTAB(7)"*****************"
100 PRINTTAB(7)" ***************"
110 PRINTTAB(7)"  *************"
120 PRINTTAB(7)"   ***********"
130 PRINTTAB(7)"    *********"
140 PRINTTAB(7)"   ***********"
150 PRINTTAB(7)"  *************"
160 PRINTTAB(7)" ***************"
170 PRINTTAB(7)"*****************"
180 PRINTTAB(7)"    **** ****"
190 PRINTTAB(7)"    ***   ***"
200 PRINTTAB(7)"    **     **"
210 PRINTTAB(7)"    *       *";
220 GOTO 220

Although this only looks like it saves a character per line (“TAB(8)” versus “seven spaces”), the code itself will be smaller since the TAB command tokenizes down to one (or maybe two?) bytes.

Also, the ending quote is not needed if it’s the last thing on a line, so they could be removed:

 50 PRINTTAB(7)"    *       *
 60 PRINTTAB(7)"    **     **
 70 PRINTTAB(7)"    ***   ***

That would save one byte per line.

But, each line number consumes 5-bytes on it’s own, so a better way to save space would be to pack the lines together. Each line you eliminate saves five bytes. That would become pretty unreadable though, but let’s do it anyway:

10 CLS:PRINTTAB(7)"    *       *":PRINTTAB(7)"    **     **":PRINTTAB(7)"    ***   ***":PRINTTAB(7)"    **** ****":PRINTTAB(7)"*****************":PRINTTAB(7)" ***************":PRINTTAB(7)"  *************":PRINTTAB(7)"   ***********"
130 PRINTTAB(7)"    *********":PRINTTAB(7)"   ***********":PRINTTAB(7)"  *************":PRINTTAB(7)" ***************":PRINTTAB(7)"*****************":PRINTTAB(7)"    **** ****":PRINTTAB(7)"    ***   ***":PRINTTAB(7)"    **     **"
210 PRINTTAB(7)"    *       *";
220 GOTO 220

That’s quite the unreadable mess!

This could still be made better, since the text lines were kept under the input buffer limitation size, but when you enter that line, BASIC compresses it (tokenizes keywords like PRINT, TAB and GOTO) making it take less space. You can then sometimes EDIT the line, Xtend to the end and type a few more characters.

That may or may not be allowed for the Logiker challenge. And since I want to provide code here you could copy and then load in to an emulator, I’ll keep it to the limit of what you could type in.

In the next installment, I’ll see if my brane can figure out a way to generate this code using program logic rather than brute-force PRINT statements.

Until then…

My old Space Invaders game running on an arcade cabinet!

A few years back, Color Computer community member Rietveld Rietveld took an Arcade1Up Rampage cabinet and converted it so I could run other software — such as emulators. Images have been shown of it running the Color Computer emulator and software.

Now, with the official 1.0 release of the NitrOS9 “Ease of Use”, that has been installed on this arcade machine and, by request, my OS-9 game Invaders09 has been ran on it.

I never imagined that my “arcade” game would eventually be running on a (sorta) arcade machine. Thanks, Rietveld!

Reversing bits in C

In my day job, we have a device that needs data sent to it with the bits reversed. For example, if we were sending an 8-bit value of 128, that bit pattern is 10000000. The device expects the high bit first so we’d send it 00000001.

In one system, we do an 8-bit bit reversal using a lookup table. I suppose that one needed it to be really fast.

In another (using a faster PIC24 chip with more RAM, flash and CPU speed), we do it with a simple C routine that was easy to understand.

I suppose this breaks down to four main approaches to take:

  • Smallest Code Size – for when ROM/flash is at a premium, even if the code is a confusingf mess.
  • Smallest Memory Usage – for when RAM is at a premium, even if the code is a confusing mess.
  • Fastest – for when speed is the most important thing, even if the code is a confusing mess.
  • Clean Code – easiest to understand and maintain, for when you don’t want code to be a confusing mess.

In our system, which is made up of multiple independent boards with their own CPUs and firmware, we do indeed have some places where code size is most important (because we are out of room), and other places where speed is most important.

When I noticed we did it two different ways, I wondered if there might be even more approaches we could consider.

I did a quick search on “fastest way to reverse bits in C” and found a variety of resources, and wanted to point out this fun one:

https://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious

At that section of this lengthy article are a number of methods to reverse bits. Two of them make use of systems that support 64-bit math and do it with just one line of C code (though I honestly have no understanding of how they work).

Just in case you ever need to do this, I hope this pointer is useful to you.

Happy reading!

ChatGPT AI can analyze 6809 assembly language!

I gave it this:

clear
    lda #96
clearA
    ldx #1024
loop
    sta ,x+
    cmpx #1536
    bne loop
    rts

…and ChatGPT gave me this:

The clear subroutine in this code is used to clear the screen and initialize it with a grid of green squares. The subroutine begins by loading the value 96 into the accumulator register A using the LDA instruction. This value represents the color value for green, which will be used to fill the screen with green squares.

Next, the LDX instruction is used to load the value 1024 into the X register. This value is the memory address of the top left corner of the screen, and it will be used as the starting point for the POKE operations that will be used to fill the screen with green squares.

The loop label marks the start of a loop that will be used to fill the screen with green squares. Inside the loop, the STA ,X+ instruction is used to store the value in the accumulator register A at the memory location pointed to by the X register, and then increment the X register by one. This has the effect of setting the value at the current memory location to the color value for green, and then moving to the next memory location on the screen.

The CMPX instruction is then used to compare the value in the X register to the value 1536. If the X register is less than 1536, it means that there are still memory locations on the screen that need to be filled with the color value for green. In this case, the BNE instruction is used to branch to the loop label, causing the loop to be repeated and the next memory location on the screen to be filled with the color value for green.

If the X register is equal to 1536, it means that all of the memory locations on the screen have been filled with the color value for green, and the loop can be exited. In this case, the BNE instruction will not be executed, and the RTS instruction will be executed, causing the subroutine to return to the main program. This completes the execution of the clear subroutine.

– ChatGPT AI

Now, it may have figured some of this out because I previously fed it a longer (and fully commented) bit of 6809 code that contained that routine as one of its several functions. That’s the only way I can see it knew the screen location and a green block.

https://chat.openai.com/chat, ya’ll.