By the end of the previous tutorials Ben had arranged for us to write assembler programs which can be stored in an EEPROM so that the 6502 can run the program. We also have a "Virtual Interface Adapter" which gives us the ability to latch output from the data bus and initially we arranged to set LED patterns.
LCD
Displaying output on LEDs becomes very dull very quickly so our next mission is to display a message on an LCD panel (video #4). I am quite familiar with the HD44780 LCD controller from other projects so I found this quite easy.
The LCD has three control signals RS RW EN which we attach to VIA PORTA and 8 data pins which we attach to PORTB. Register select switches between the instruction register and data register in the controller and RW determines whether data is being written to or read from the controller. The Enable signal is pulsed to actually execute the command. We program the LCD by putting a character or instruction in port B, setting RS and RW in Port A then briefly enabling the EN pin. It takes about four command instructions to setup the LCD and we can then send characters in subsequent commands. Each display command takes 8 assembler instructions so our program is about 160 lines long.
Finally we can display our message
Stack
It is a real pain duplicating 8 assembler instructions each time we want to send a character so we use a subroutine mechanism. Define the character to be displayed then call a subroutine to control the LCD. At the end of the subroutine return to define / process the next character. This removes the repeated code and our program is reduced to 75 lines.
Unfortunately this program doesn't work! The 6502 needs to save a return address when it jumps into a subroutine (JSR) so that it can return to execute the next instruction when it is finished (RTS) (video#5). The 6502 uses an area of memory called the stack 0100-01ff where it pushes 2-byte addresses and pops or pulls them back on return. 256 bytes allows for upto 128 nested subroutines.
RAM
Previously we attached an EEPROM to the 6502 bus and read data from it. We attach a 62256 32k x 8 bit RAM chip, which uses the same pin positions as the EEPROM. We set it up so that we can address it at 0x0000 to 0x3FFF (16k). Our life then becomes rather complicated as we delve into the timing diagram (video #6). A lot of patience and study of the 6502 and 62256 datasheets is required to get this right. It turns out that we need to ensure that A15, the first address line is set before the others and we have to add a little bit of circuitry to achieve this.
Finally we can run our subroutine (video #7) and stepping through we see data written to the stack as part of each JSR and retrieved/moved to the PC (program counter) when executing RTS. It works a treat and our subroutines do their job.
No comments:
Post a Comment