Tuesday 16 February 2021

Ben Eater : 6502 : Using RAM and providing LCD output

 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.

Tuesday 9 February 2021

Ben Eater : 6502 : Writing in Machine Code

 In my  previous 6502 post Ben showed (video #1) how to connect up the microprocessor with a few inputs so that it would run through its boot sequence.  We used an Arduino Mega to monitor address and data lines so that we can see what is happening.

We now need to write a program (video #2).  We know that the 6502 reads the program start address from the reset vector at 0xfffc and 0xfffd so we have to provide some hardware (memory) which is addressable.  By that I mean we attach the address/data lines on the 6502 to address/data lines on a memory chip and when requested the memory chip will look at the address lines and respond with the bits at that address on the data lines.  

For the program memory we use an AT28C256 32KB EEPROM.  This has to be configured with a special programmer, we use a TL866II Plus programmer to write our information on to the EEPROM.  The first step is to define a machine code program which the 6502 will understand.  We then need to create a file containing the program and, using the programmer copy it across to the EEPROM.  Ben uses Python to write a file called rom.bin which is exactly 0x8000=32KB long.  The programmer copies this to the EEPROM which can then be attached to the 6502.

In order to see some results we need the 6502 to produce some output.  Using a cunning scheme Ben arranges for the EEPROM input to use memory addresses 0x8000-FFFF leaving addresses 0x0000-0x7FFF for output.  A multipurpose output chip called a W65C22 Versatile Interface Adapter is attached to address/data lines (lets call them busses from now on).  We will use it to latch information from the data bus to an output port.   We attach LEDs to the output port so that we can see the results.  Another cunning scheme is required so that the W65C22 reacts when the 6502 sends to addresses 0x6000..... 

Now we have hardware attached where we can store our program and we can control an output device.  A short machine code program is written to configure the W65C22 for output, write output patterns to the LEDs and loop around.  Awesome.

This is a packed hour long lesson, at the end of which we should understand programming an EEPROM, ROM addressing, latching output, using an output chip and writing machine code.  Wow.

After the steep learning curve of video #2 we consolidate our position in video #3.  Writing machine code quickly gives you a headache and it is much easier just to write the assembler code and ask an assembly program to convert it.  Video #3 shows you how to install and use vasm to create rom images which can be loaded into the eeprom.  This results in another LED program which is demoed below.


 

  




Monday 8 February 2021

MacBook Air M1

 I wanted to buy a laptop, mainly because I am not happy sitting at my desk all the time whilst typing.  An iPad is fine for armchair computer use but not for typing.

A Windows laptop is the obvious choice but I am not enthusiastic about Windows software at the moment.  In particular a practical alternative to MS-Word for word processing would be welcome.  Previously I borrowed Harry's ancient MacBook Pro and found it quite usable despite being unfamiliar and 10 years old.  In addition macOs is linux-based and I see lots of Macs used in linux-related tutorials these days.  Although miscellaneous software is more likely to run on Windows, I don't expect the laptop to be used for everything and it doesn't need to replace Windows.

The new Apple MacBook Air M1 is based on an ARM chip which is faster than and uses less power than previous Intel-based processors which gives it a very long battery life and means it requires no fan so makes no noise.  It became available in the UK in November 2020 at John Lewis Partnership which is my preferred supplier as they combine competitive prices with excellent service.  The entry level model costs £999 has 8GB RAM, 256GB SSD and 13.1" display.  It looks excellent value compared with older MacBooks or the M1 with larger SSD.  It is also apparently possible to use App Store iPhone/iPad apps, which haven't previously been available for MacBook users.

As you would hope and expect Apple set up is straightforward and it only takes a few minutes to be working on the Internet.  I was particularly impressed with the user guide and newbie guides which are specific to this model.  I shall perhaps blog later on getting used to MacOs.