6502 breadboard computer part 7 – working monitor program!

I love it when things work! Ok, it wasn’t entirely plain sailing, but progress on this has been rapid in the last few months.

A quick recap:

  • As a child in the 1970s I wanted my own self-contained single-board computer like a Kim-1 or an Acorn System 1. I couldn’t afford one then, though I have to say the latter especially would have been a sound investment, dad, but I guess I’d better let that go.
  • Machines like these have simple LED displays and you usually program them with raw machine code opcode hexadecimal numbers.
  • Ben Eater’s guides to making a 6502 breadboard computer have helped me realise that dream, despite not having an oscilloscope or logic probe, little knowledge of electronics and a distant memory of 6502 assembler from my big brother’s Kim-1.
  • I caught up with Ben to the point where he’s started looking at full keyboards, and as I don’t want that, I only want a hex keypad and a few buttons, I designed my own keypad interface using an encoder chip and a shift key (see earlier posts).

I’ve now written my own, very basic monitor program, and it seems to work. I’m pleased with its elegant simplicity.

  • Type hex numbers on the keypad and they get loaded straight into the current memory location.
  • Browse backwards and forwards in memory with shift-C and shift-D.
  • As well as the current address and memory contents, the display also shows an ASCII representation of the memory contents, useful if displaying text.
  • Run a program by pressing shift-A.
  • Stop it by pressing the CPU reset button, which drops you back into the monitor at $0300.
  • Programs can only be 256 bytes long. I may change this but, frankly, I can’t see myself typing in any program longer than 256 bytes.
  • User-entered programs always start at $0300 and always run from $0300 when you press shift-A. I may change this so they run from the current location instead, which would allow you to enter and test multiple small programs.
  • The user can access ROM routines to display text ($80D7) and send control codes ($80C0) to the LCD display. These addresses, of course, may change if you modify the ROM in any way.
  • There’s no easy way to scan the keypad in your user-entered programs yet – that’s something for me to work on.

Here are my first programs I entered and ran. The first just clears the LCD display and sits in a loop doing nothing.

address	mnemonic		op code		description
-------------------------------------------------------------
$0300	LDA $01			A9 01		Clear screen
$0302	JSR [lcd instruction]	20 C0 80
$0305	JMP [here]		4C 05 03        Infinite loop

The next one clears the screen and prints the ‘h’ of ‘hello world’.

address	mnemonic		op code		description
-------------------------------------------------------------
$0300	LDA $01			A9 01		Clear screen
$0302	JSR [lcd instruction]	20 C0 80
$0305	LDA “h”			A9 68           print 'h'
$0307	JSR [print_char]	20 D6 80
$030A	JMP [here]		4C 0A 03	Infinite loop

Here’s another program. Add two numbers, store the result in memory just after the program at $0309. As luck has it, RTI (return from interrupt) drops you back into the monitor so you can start examining your results as stored in memory.

address  mnemonic     hex        notes
--------------------------------------------------------
$0300    CLC          18         clear carry
$0301    LDA #$40     A9 40      load $40 into A
$0303    ADC #$02     69 02      add $2
$0305    STA $0309    8D 09 03   store result at $0309
$0308    RTI          40         jump back to monitor

It’s been a hugely educational experience. I’ve gained huge respect for hardware designers – frankly I think it’s amazing any calculator works let alone a phone or computer. I’ve learned precisely why pull-down or pull-up resistors are needed, by seeing first hand at a logic level what happens when you don’t use them. And I’ve also rediscovered the joy of assembler. It’s really not that hard as you have incredibly few instructions to play with. Every problem truly needs decomposing like you wouldn’t believe.


Source code: https://github.com/blogmywiki/6502

Posts in this series
Part 6
Part 5
Part 4
Part 3
Part 2
Part 1

This entry was posted in computers and tagged , . Bookmark the permalink.

Leave a Reply