Monday, August 20, 2018

C64Net WiFi Modem Filter State Machine

So, believe it or not, work continues on the C64Net WiFi Modem firmware.  Especially since the same firmware is being used in a traditional RS-232 version of the modem based on the ESP32 module.




Some of the newer features include:
  1. New AT+CONFIG configuration menu
  2. Ability to set the hostname
  3. X-Modem and Z-Modem downloads
  4. AT+SHELL to access an SD-card interface.
  5. NTP client with configurable timezone.
  6. New configurable socket filtering state machine.
That last feature is what I wanted to document here.

The use case was the ability to have the modem filter or transform bytes coming either from a socket connection, or from a web page via the AT&G command.  There were already existing commands to mask out specific bytes, but users needed something more complex.  I searched the web the best I could to find an existing language definition for doing such a thing, and couldn't come up with anything.  I therefore chose to invent a really simple filtering code/language.  

My requirements were that it had to be completely definable in ascii, using only characters available for the AT command set in quotes.  It needed to be as compact as possible for memory constraints, and needed to handle cases like filtering out everything inside html comments <!-- -->, or possibly filtering out everything NOT inside html comments.

Here is what I came up with:

State Machine entry format:
MMcCCNN
MM - byte value to match, in hex. The value 00 matches ALL.
c - Command character: e)at char, p)ush to que, d)isplay char, r)eplace char, q)ue display and empty, x)empty que
CC - if c == 'r', then hex value of replacement byte
C - if c != 'r', then same as 'c', or '-' to do nothing further.
NN - next state, in hex, starting with state 00.

The machine starts with state 00, and, for each character byte, increments the state until a match is made, at which point is executes commands and proceeds to state NN.

Example:
Suppose you wanted to filter out everything in a web page EXCEPT the contents of the comments.

Important chars and their hex values:
< 3c
! 21
- 2d
> 3e

So, to grab only the stuff from <!-- -->, your state machine would look like this:
## -CODE--  COMMENT
00 3Ce--02  <-- if a '<' go to state 02
01 00e--00  <-- anything else, ignore it, go back to state 00
02 21e--04  <-- if '<!', go to state 04
03 00x--00  <-- anything else, ignore it, go back to state 00
04 2de--06  <-- if '<!-', go to state 06
05 00x--00  <-- anything else, ignore it, go back to state 00
06 2de--08  <-- if '<!--', go to state 08
07 00x--00  <-- anything else, ignore it, go back to state 00
08 2dp--0a  <-- now inside the <!--.  If '-', then state 0A
09 00qd-08  <-- anything else, display que & char, go to state 08
0a 2dp--0c  <-- if '--', then que the char, go to state 0C
0b 00qd-08  <-- anything else, display que & char, go to state 08
0c 3ex--00  <-- if '-->', dump the que, ignore char, go to state 00
0d 2dqd-0a  <-- anything else, display que & char, go to state 08


So, to do the AT&Y command, we just combine the codes in order:
AT&Y"3Ce--0200e--0021e--0400x--002de--0600x--002de--0800x--002dp--0a00qd-082dp--0c00qd-083ex--002dqd-0a"

Then any subsequent packets received from an open socket, or from the AT&G command (which dumps a web page to the modem) will use the above filter.

A few extra utility arguments were added for convenience:
AT&Y     with no arguments clears the state machine definition entirely
AT&Yn   where n is a decimal number, will set the state machine state.

All of this will be in 3.4 of Zimodem.

Amiga Recaps, Pt. 1

Lately I've been recapping Amigas with a fellow from the CTCUG group.  That's been going pretty well.  I've been practicing on spare Amiga 600 boards, of which I had 3 non-booting boards.  All 3 began booting after the recap, which is amazing happiness.  Once I've recapped all my spare boards, I'll move on to the computers in my collection.


Some lessons I've learned:
1. You can use hot air to remove surface mount caps with tin-foil to isolate the cap and the damage, but be super-careful not to disturb nearby components for several minutes after removing the hot air.  Easier is to use a wire cutter to cut into the cap from the top, being sure not to pinch along the same line as the solder contacts, so as not to pull a trace.  When the cap is cut away, it is easy and safe to de-solder normally.

2. Remove those tiny-legged through-hold electrolytics with a soldering iron on max heat by applying heat to the bottom pin while carefully pulling that pin up through the board.  Once you've exposed the top-side of the pin, resume pulling it up from the top of the board to make sure you aren't pulling up any traces on the top.  Then repeat for the second pin.

3. Clean the area with alcohol and q-tips afterwards, and carefully clean the through-holes with solder-wick.

4. When applying the surface mounted caps, do one leg at a time after tinning the pads.

5. Check the smd caps for a short afterwards.

Thursday, May 17, 2018

My new friend KIM.


I've had a pair of Commodore KIM-1 computers for at least 15 years now, but never really did anything with them.  That all changed over the last few weeks, and now I feel sorry for how much fun I've been missing out on.



The KIM-1 is a single board, 1mhz, 1 kilobyte computer (the green thing in the picture above) with a 23 key hexadecimal keypad for input, and a 6 digit LED display for output.  It also has two edge connectors on the left-hand-side.  One is called the Expansion port, and the lower one is the Application port.  The computer is powered by applying 5V to a pin on the Application port.  This port also includes a Current-Loop RS232 interface and cassette interface for saving/loading programs.

It may be a very minimal micro-computer, and it is definitely not a Home/Personal Computer, but it is certainly a computer!




The screen is divided into two segments.  The left four hex-digits are the current working Address, and the right two hex-digits are the contents of that address.  The address can represent the program counter when stepping through a program, or just the address currently being read or written to when using the KIM-1s built-in monitor.



The keyboard has the sixteen hex digits for entering addresses or data.  The AD button selects "Address" mode, where any entry on the keyboard will change the current working address.  The DA button selects "Data" mode, where any entry will change the contents of the address shown on the LED screen.  When in either Data or Address mode, the + key will advance to the next address byte.

The switch in the upper left selects between SST mode (on), which allows you to step through an executing program one instruction at a time, and normal mode (off), which allows a program to run freely to completion.  In both modes, the GO button will begin executing the program from the current address shown.  In SST mode, the GO button is then used to execute the next instruction, and then immediately return to the monitor program.

The RS button will reset the CPU and cause the KIM to re-enter the built-in monitor.  It is the first button you must press after powering-on the KIM-1.  Lastly, the PC button does nothing except replace the current address with the program counter address.



The KIM-1 is powered by three principle chips.  One, of course, is the MOS 6502 cpu/processor.  The other two are a pair of MOS 6530 RRIOT chips.  Each has 64 bytes (!) of ram, a pair of 8-bit bi-directional I/O controllers, timer registers, and 1k of ROM.  On one of the 6530s is the ROM code for the ml-monitor and cassette interface, while the other has the paper-tape interface.



I have a couple of Corsham expansion boards hooked up to my KIM, which are awfully convenient, and part of the reason why I finally dug the KIM-1 out for play.  Pictured above is their I/O board, which provides a standard 9-pin RS-232 port, which converts to the KIM's strange current-loop port.  It also has convenient interface for regulated 5V, 12V, and ground (the colored wires you see above), as well as an audio input and output jack for plugging in a standard cassette deck.  The little switch on the bottom lets me pick between TTY input, and using the KIM's keypad.


On the Expansion port is a Corsham 60k ram expansion card, gives the computer ram from $2000 all the way through $FFF7.



Once I got it working, I hooked the KIM-1 up to my VIC-20 through the VIC-1011A TTL interface and used the VIC's keyboard and monitor to program the computer.  The protocol settings are: 2400bps, 8 data bits, no parity, two stop bits, no flow control.

One of my first programs was a memory-page address tester to make sure that the Corsham 60K ram expander was working correctly.  The program simply puts the page number into every byte in every page above $2000, and I then use the monitor to spot-check the results.

The first program I wrote on it was done by writing the assembly on paper for address $0200, and then translating the mnemonics into the machine language op-code numbers, and the branch addresses into one byte 6502 offsets.

Later, I realized that I could simply write the code in assembly, assemble it on the c64, and then key in the generated bytes onto the KIM's keyboard.

I am seriously interested in translating Microsoft BASIC for the KIM, and have discovered numerous resources on the web for doing that.  Check back here in the future for progress on that project.