6502 Machine Language Monitor for 4 x 20 LCD - HOWTO
Posted: Wed Feb 08, 2023 11:51 pm
Intro
My interest in the 6502 (and in retrocomputing generally) tends towards systems programming.
I know a lot of folks come to retrocomputing because of games. Sure, I like games. I still get out my NES and play Zelda from time to time, and I still love it! But it doesn't make me want to take Zelda apart and find out how it ticks, or inspire me to write my own neo-retro game, the way it does many people. Give me an operating system, though, and look out!
The thing is, though, there's not too much introductory instructional material about how to write this type of software, at least not that is 6502-specific. There's not a lot of "how to port DOS DEBUG to the 6502" or similar. The traditional method seems to boil down to studying code: either WOZMON (amazingly slick hacker code, but obscure) or some variation of Jim Butterfield's SUPERMON (pretty complex).
You can see why when I first came across Ken Skier's "Beyond Games: Systems Software for your 6502 Personal Computer" my interest was immediately piqued. This book can be found on Archive.org as a pretty nice scan. But, I hate reading online, especially if I'm trying to work on a project board at the same time. Unfortunately this is one of those retro books that (like Levinthal's "Assembly Language Subroutines") tends to be Really Expensive (TM) to get in print. I've been stalking the used book scene for a while, and finally found a used copy that was sort of affordable. It's in pretty good shape, but I did end up spending more on it than I really should have. However, I'm glad I did, because it's exactly what I wanted.
The first few chapters are a general beginner's introduction to 6502 assembly language written in a kind of (honestly, annoying) conversational style. After that it dives right into building a machine language monitor called "VISIMON." It has a modular, structured design that makes it easy to comprehend in detail, and it has a generalized I/O interface that makes it easy to port. (It's designed to be run on a variety of period computers, including the PET 2001, the Apple II, and the Atari 800. The author developed it on an Ohio Scientific Challenger 1P.) If, for example, you have been following along with Ben Eater's videos and have a working 6502 with keyboard input and LCD screen output, VISIMON is just about the perfect level of complexity for a next step.
The COVID fairy came to visit our house again this month, so that's kept me off the breadboards for a week or two. Instead, while I've been sitting around, I've been porting VISIMON over to Peanutbutter-1 and modifying it to incorporate some of the features of my own half-finished system monitor, PAGMON. I thought it might be a good idea to start a thread to document the process for other folks who might be out there googling things like "HOW TO WRITE A 6502 MACHINE LANGUAGE MONITOR DEBUGGER" without having much luck.
Part 1 - The Host System
Peanutbutter-1 is a fairly typical homebrew 6502SBC. Currently the MPU is a CMD G65SC02 running at 4MHz, but the board can be jumpered to accept a WDC 65C02. It has 32K of very fast (12ns) SRAM, and 32K of ROM. The ROM socket can be jumpered to support either 27C256 (EPROM) or 28C256 (EEPROM). I'm currently using a 150ns Atmel EEPROM, but I plan to switch it out for something faster at a later date. The I/O is handled by a 65C22 VIA, which has an LCD (in 4-bit mode) connected to PORTA and a PS/2 keyboard connected to PORTB.
The LCD deserves a little more attention. The typical Ben Eater system has one of those 2 x 16 LCD modules driven by an HD44780U LCD controller. Peanutbutter-1 was no different. As you can imagine, it's tough to get much of a user interface for an interactive program out of 32 visible characters. However, fortunately for me (in a way) at some point over the last few months I accidentally smashed my 2 x 16 display. As I was shopping around online to replace it, I noticed that the 4 x 20 LCD modules are not very much more expensive. Even better, they use exactly the same interface. You can unplug your 2 x 16 LCD and replace it with a 4 x 20 LCD and it will immediately just work. (There are a few weird things with the order of display addresses, but they are not too hard to manage!)
80 visible characters is a lot more than 32, but it's still not a lot of space for a user-interface. We're not going to be able to get something like Chad's fullscreen debugger. Luckily, VISIMON is "field" based. Its display looks like this:
The fields are the address field (field 0), the byte field (field 1), the character field (field 2), and the register fields (fields 3 - 6). You use some navigation keys ('<,' and '>' by default) to select fields, which changes which field the "arrow" points to. You can edit the currently selected field by typing into it, so, for example, the current address can changed by selecting it with the "arrow," and simply typing in a new one. The address can also be stepped forward or backwards by another set of navigation keys ('SPACE,' and 'RETURN' by default). The byte field displays the contents of the current address in hex, and the character field displays the same thing as an ASCII character. If the byte field is selected, you can type in hex digits to edit the contents of the current address (unless it's an address in ROM) and if the character field is selected, you can type whatever you want and it will be stored in memory at the current address as an ASCII character. The register fields contain garbage by default, but if you run a program from the monitor (by selecting the address of its first instruction and typing SHIFT+G, capital 'G' for Go) when it returns the register fields will be updated to contain their contents at the point that the called program returned to the monitor. This is useful if you want to pass a result back to the monitor in the .A register, for example.
VISIMON only needs 3 lines of display, which is one less than we have on our LCD. Unfortunately it's designed for 25 - 40 column text displays, which is still too big for our 4 x 20 LCD to hold. So PAGMON's display will have to be slightly altered:
Instead of an "arrow," a colon shows which field is selected. (In the example, the character field is selected.) Line 3 is empty for now, but later after adding some break routines I thought it might be useful to put a PC display there for single stepping.
This is not exactly a powerful development environment, but it does have the minimum requirements. You can look at memory, edit memory, and run code. Although it would be tedious and time consuming, it would be at least *possible* to hand load arbitrarily lengthy and complex programs and to run them.
Well, I had more to say in Part 1, but it's time to go make dinner and I don't want to lose what I've written so far, so I'll leave this here for now. Be back soon for Part 2!
My interest in the 6502 (and in retrocomputing generally) tends towards systems programming.
Typical system programs include the operating system and firmware, programming tools such as compilers, assemblers, I/O routines, interpreters, scheduler, loaders and linkers as well as the runtime libraries of the computer programming languages.
The thing is, though, there's not too much introductory instructional material about how to write this type of software, at least not that is 6502-specific. There's not a lot of "how to port DOS DEBUG to the 6502" or similar. The traditional method seems to boil down to studying code: either WOZMON (amazingly slick hacker code, but obscure) or some variation of Jim Butterfield's SUPERMON (pretty complex).
You can see why when I first came across Ken Skier's "Beyond Games: Systems Software for your 6502 Personal Computer" my interest was immediately piqued. This book can be found on Archive.org as a pretty nice scan. But, I hate reading online, especially if I'm trying to work on a project board at the same time. Unfortunately this is one of those retro books that (like Levinthal's "Assembly Language Subroutines") tends to be Really Expensive (TM) to get in print. I've been stalking the used book scene for a while, and finally found a used copy that was sort of affordable. It's in pretty good shape, but I did end up spending more on it than I really should have. However, I'm glad I did, because it's exactly what I wanted.
The first few chapters are a general beginner's introduction to 6502 assembly language written in a kind of (honestly, annoying) conversational style. After that it dives right into building a machine language monitor called "VISIMON." It has a modular, structured design that makes it easy to comprehend in detail, and it has a generalized I/O interface that makes it easy to port. (It's designed to be run on a variety of period computers, including the PET 2001, the Apple II, and the Atari 800. The author developed it on an Ohio Scientific Challenger 1P.) If, for example, you have been following along with Ben Eater's videos and have a working 6502 with keyboard input and LCD screen output, VISIMON is just about the perfect level of complexity for a next step.
The COVID fairy came to visit our house again this month, so that's kept me off the breadboards for a week or two. Instead, while I've been sitting around, I've been porting VISIMON over to Peanutbutter-1 and modifying it to incorporate some of the features of my own half-finished system monitor, PAGMON. I thought it might be a good idea to start a thread to document the process for other folks who might be out there googling things like "HOW TO WRITE A 6502 MACHINE LANGUAGE MONITOR DEBUGGER" without having much luck.
Part 1 - The Host System
Peanutbutter-1 is a fairly typical homebrew 6502SBC. Currently the MPU is a CMD G65SC02 running at 4MHz, but the board can be jumpered to accept a WDC 65C02. It has 32K of very fast (12ns) SRAM, and 32K of ROM. The ROM socket can be jumpered to support either 27C256 (EPROM) or 28C256 (EEPROM). I'm currently using a 150ns Atmel EEPROM, but I plan to switch it out for something faster at a later date. The I/O is handled by a 65C22 VIA, which has an LCD (in 4-bit mode) connected to PORTA and a PS/2 keyboard connected to PORTB.
The LCD deserves a little more attention. The typical Ben Eater system has one of those 2 x 16 LCD modules driven by an HD44780U LCD controller. Peanutbutter-1 was no different. As you can imagine, it's tough to get much of a user interface for an interactive program out of 32 visible characters. However, fortunately for me (in a way) at some point over the last few months I accidentally smashed my 2 x 16 display. As I was shopping around online to replace it, I noticed that the 4 x 20 LCD modules are not very much more expensive. Even better, they use exactly the same interface. You can unplug your 2 x 16 LCD and replace it with a 4 x 20 LCD and it will immediately just work. (There are a few weird things with the order of display addresses, but they are not too hard to manage!)
80 visible characters is a lot more than 32, but it's still not a lot of space for a user-interface. We're not going to be able to get something like Chad's fullscreen debugger. Luckily, VISIMON is "field" based. Its display looks like this:
Code: Select all
A X Y P
0300 41 A 53 FE 3C 31
†
VISIMON only needs 3 lines of display, which is one less than we have on our LCD. Unfortunately it's designed for 25 - 40 column text displays, which is still too big for our 4 x 20 LCD to hold. So PAGMON's display will have to be slightly altered:
Code: Select all
.A .X .Y .P
53 FE 3C 31
0300 41 :A
This is not exactly a powerful development environment, but it does have the minimum requirements. You can look at memory, edit memory, and run code. Although it would be tedious and time consuming, it would be at least *possible* to hand load arbitrarily lengthy and complex programs and to run them.
Well, I had more to say in Part 1, but it's time to go make dinner and I don't want to lose what I've written so far, so I'll leave this here for now. Be back soon for Part 2!