Sunday, 28 March 2021

6502 : Add RAM and VIA

 My 6502 system now has a working screen but as yet no RAM.  I find the idea of a working computer with no RAM strangely intruiging, but it is time to move on.  On the face of it, adding RAM is easy as I have done it before following the Ben Eater tutorial and its setup is very similar to the ROM.

RAM

Indeed 62256 memory connection is mostly straightforward.  The chip is located adjacent to the ROM and address/data pin connections are the same.  I rewired two existing NAND gates so they were in the same configuration as Ben's tutorial and used an extra NAND for RAM address wire setup as Ben did.

All worked well, with the proviso that it took a long time.  I found it vital to update the wiring schematic (now Hardware revison 3) before starting and to list out all the changes to existing wiring before I started.  As the circuit becomes more complex, any faults introduced can be very hard to identify.  Loose wires coming out are quite difficult to diagnose but an incorrect wiring change is even harder.

Initially I just added the RAM and wrote a small program to test reading/writing addresses and using the stack in a subroutine call.  I then moved theACIA serial chip from address $0000 where it conflicted with RAM to $6000, which Bens tutorial had previously made accessible for the VIA chip.

It feels very good indeed to have a computer with RAM, ROM, screen input / output which I can program easily in assembler language.  

VIA

To complete my basic computer I need to connect the Versatile Interface Adapter (VIA) so that I can access other peripherals.  I am hoping that this will be my last device attached to the 6502 directly as I have concerns I could mess it up or break it if I do too much tweaking.

Although Ben's initial LCD connection to VIA is not required, it does provide a useful test and confirm whether VIA is working so I connect / test the LCD as previously.

VIA connections are not complex, 8 data pins, 6 address related and 4 control pins.  I need an extra logic gate, and I connected a HCF4069 hex invertor for this.  I moved the A13 address pin from ACIA to VIA so that it uses address $6000.  I then inverted A13 as input to ACIA which is now at $4000.

I found that updating my schematic was vital before making connections as it is VERY easy to confuse yourself.  It is also sensible to write down each connection you need to make beforehand.  Thirdly, testing regularly that the original functions still work provides confidence that important existing pins have not been dislodged by the work.

On completion of the connections I could check that ACIA worked at its new address and then connect an LCD to the VIA as per Ben's instructions and check it work.  All is good, our computer now has RAM and I/O capabilities :) :) :)



Monday, 22 March 2021

6502 : Serial Terminal

 In my previous Ben Eater: 6502 post I set up a 6502 + EEPROM board with a Mega attached which enabled me to download programs with the ROM in situ.  As a second major enhancement, I need it have keyboard input and display output.  Ben does a diy video controller board but that looks painful.  Much more practical for me is to use a serial port with an emulated terminal.  I guess this isn't a pure solution, however I could make it one if I went on ebay to buy an old terminal.  In fact I did consider buying a real DEC VT100 as I used one for much of my (short) programming career but that would be overkill.

My objective is to provide a serial terminal input and output on my 6502 board using hardware compatible with the 6502 and EEPROM.  The best solution used by 6502 afficionados is an  ACIA (Asynchronous Communication Interface Adapter) W65C51 which interfaces to an FTDI chip and provides serial transmit / receive upto 19200bps.  An excellent example circuit and associated program is provided by Dirk Grappendorf.  I particularly liked that the program doesn't use RAM so I could proceed with the program before adding RAM to my computer (I never thought I would be able to say that).

Hardware

W65C51 chips are available on ebay for about £5, and they also require a 1.8432MHz oscillator (HC49) for another £1.




To check my understanding of the hardware I added the W65C51 to my board and connected up power, address, data and control pins (output-enable, chip-enable, write-enable).  I was then able to set pins in a sketch to control ACIA.  ACIA was setup with RS0=A0, RS1=A1 and /CS1=/A15 so that ACIA registers can be accessed at addresses 0000, 0001, 0002, 0003. 

I could then send a character to the serial port at 19200baud with 3 commands:
  Write 0B to address 0002             (for no parity, no echo, no interrupt)
  Write 1F to address 0003              (1 stop bit, 8 data bits, 19200 baud)
  Write characters to address 0000
 

6502 Connection

Once I knew the hardware was connected properly and working I could write a program.  For the previous test 6502 and EEPROM were turned off, so I turned them on and tried saving the simplest possible program to EEPROM and running it from the 6502.

loop:
  8000 LDA #0B             A9 0B    
  8002 STA $0002          85 02   store COMMAND byte
  8004 LDA #1F              A9 1F   store CONTROL byte
  8006 STA $0003          85 03
  8008 LDA #6A             A9 6A   write letter "j" on terminal
  800a STA $0000          85 00
  800c JMP loop           4C 00 80 start from the beginning

I used the monitor sketch to debug the program.  It was easy to have a bad or incorrect connection so I spent some time producing and checking the schematic above to ensure my hardware was set up as expected.  I used a couple of NAND gates to get the ROM and ACIA addressing signals as I wanted them.  In the listing below you can see the three write commands at lines 59,64 and 69.


Software

With a simple program working I could concentrate on providing a working programming environment. Initially I used VASM to compile a simple program and then manually transcribed the bytes into an array in a sketch for download to the EEPROM.  However this quickly became painful and I wrote a small utility program in C to read the VASM hex output file and format it into a text file which was included in my sketch as progdata.h.

Initially I had lots of manual intervention to stop the clock, download data, start clock and reset the 6502/ACIA.  It was easy to get the mega to sketch control the reset and clock pins during EEPROM download. 


I now have an easy programming environment which only requires two steps (1) compile program and format output into a sketch (2) download the sketch with clock paused, then reset the processor.

In the test program I used screen codes to erase and colour text to give a nicer result.











Sunday, 21 March 2021

C graphics programs

 For some reason I had a sudden urge to work out how to write graphics programs.  The natural place to start is on Windows.  I have Mingw installed so I can run X11 programs, in particular X11 programs written under WSL can be run in a window.  The Xeyes sample is shown in the screen shot below.

I also have code::blocks installed so I had a quick look at whether it could do anything for me.  C::B includes a number of openGL related templates.  I tried the openGL template first, it compiled cleanly and gave me a lovely rotating triangle.  I then tried to use GLUT which is mentioned in a number of Google articles on the subject.  GLUT is obsolete and replaced by freeglut which needs some tweaking to get working under C::B (directions provided by GeekforGeeks). Again it provides a lovely demo, in this case some pretty wire frame demos.

In summary I have some great tools at my finger tips when I get an urge to actually write graphics programs.


Shortly after finishing this little investigation on how to write C graphics programs, I stumbled across HTML5 Canvas which provides you with space in your web page where you can put graphics.  The graphics commands themselves are written in javascript.  This is definitely an easier way of doing simple graphics.  A bit of cutting and pasting and I could do a sample on my website.




Tuesday, 16 March 2021

Uprooting a Galaxy S7 Edge

 Two years ago Annette had paid for her lovely Saumsung Galaxy S7 Edge and wanted to unlock it so she could move to another provider.  At the Sutton high street phone provider they suggested the quickest solution would be to pop in to a local phone shop to unlock it.  The (dodgy?) shop failed to unlock it and needed to keep it for someone to look at it, which in the end took two weeks.  When it came back all was not well, it wouldn't accept software updates or allow phone payments to work.  Annette took it to Samsung who said it had been rooted, would have to be rebuilt and might cost a lot of money with no guarantee of repair.  Eventually Annette purchased a new phone and the old one languished in a drawer.

I opened the drawer recently, saw the phone and thought that we should either sell, use or give it to a worthy cause.  There is also a possibility I could do something "techical" with it - use it as a display, an IOT box or may be run Android / linux natively.  The phone is still worth about £130.   It doesn't have a SIM card but I could run apps, browse the net etc.

I saw that it still has some of Annettes information stored so I decided a factory reset would be a good start.  The phone whinged loudly about doing this wanting repeated confirmation of Google and Samsung account passwords which didn't seem to exist.  Eventually Annette managed to delete her Samsung account from the phone and we were able to complete factory reset.  Unfortunately the phone was now effectively dead, it said "custom binary blocked by FRP lock" and wouldn't boot up.  Oh dear!

I googled solutions and it became clear that we needed to replace the firmware.  It appears that a tool called Odin is used to replace firmware and various firmware versions are available on line.  I tried various sites and in the end found an excellent one, sammobile with up-to-date software and firmware.The firmware download is about 2.3GB and they have a very slow throttled free download available (24 hours).  You have to make sure the firmware is for your specific phone model and country.  I thought it totally fair to pay a small amount (€6.5) for a faster download (10 minutes).  You need to install Samsung drivers and Odin software on your PC and unzip the download files.  You then put the phone into download mode, connect it to the PC which then installs it on the phone in a couple of minutes.


After that it is simply a matter of  restarting and signing on with your Google account (which must have been used to sync with the device previously).  The phone now appears totally fine and useable, altogether a wonderfully uplifting experience.  It is a shame that Samsung were unable to do this.  I believe Odin originated within Samsung and they use it themselves so they should have been able to do what I did in a few minutes.




Maixduino : AI meets Arduino

 I read an article in Elektor about an amazing Maixduino hardware board which provides an AI system in an Arduino compatible package.

The board is packed full of juicy hardware, including an ESP32 for ancilliary processing, a camera, SD card, audio and an LCD screen.  At its heart is a Sipeed Maix 64 bit RISC-V module which processes AI.  It can be programmed through the Arduino IDE which avoids a steep learning curve.

I purchased one from Mouser and started to follow Elektor article to set it up.  If you follow detailed instructions and an installation works first time you have an immensely satisfying experience but you tend to learn a little.  When events dont go as planned and issues need to be investigated by looking around different sites and articles, it can be frustrating but you learn a lot more.  Needless to say my installation experience was a struggle.

After some unsuccessful attempts at installing the Arduino software I tried the PC based micropython environment which is described well at icircuit.net and I was soon able to flash Maixduino using the kflash command line tool.  The documentation provided by sipeed also provides good detailed explanations.  Maixduino uses two COM ports one for the Maix processor and the other for the ESP32.  It was exciting to see them both communicating properly, giving me more confidence with the hardware setup and PC connection.  The test program I ran was great, it showed a camera image on the LCD screen.
Returning to the Arduino environment, with my hardware setup validated, I looked a bit more closely at the software environment.  The Elektor article had described a simple Arduino installation which obviously worked for the author.  I was pleased when I found out how to setup multiple Arduino environments on the same PC. Some of the software included in Maixduino libraries has been used by me on other projects, in particular Adafruit graphics libraries.  I set up a "portable" Arduino IDE environment which uses a separate copy of libraries and configuration files.  I then repeated the installation process.  The portable environment is nice as you can see everything relating to libraries you have installed, sketches you have written etc in sub folders of the portable folder.

Using the new environment I could compile a sketch (with a number of warning messages)  but still not "upload" to the board.  I saw two flags in preferences.txt for compile.debug and upload.debug, both set to False.  I turned them on and could see the detailed commands the IDE uses to create and upload a program to a board.   I could see kflash running and failing so I lowered the upload speed from 1,500,000  to 1,000,000bps.  Rebooting and resetting the board now allowed the upload to proceed. 

I tried a few demo programs to verify the environment. Using the Kendryte K210 AI module you declare the ST7789 screen and use the Adafruit GFX library to draw shapes, lines, text etc.  Using th ESP32 I started with a "Hello World" serial monitor display.

I was now able to return to the Elektor article and try the "selfie" program which shows a camera image on the LCD display.  I still had some minor problems, there were duplicate Adafruit GFX libraries (I deleted the one not provided by Maixduino) and a typo in a Maixduino library(googling provided me with a correction to apply).  Finally I was able to complete the first experiment and take a picture, as shown below it is a picture of a rock.
The second example described by Elektor is to run a demo which is a  real image recognition program.  It isn't practical to "train" the AI system oneself without a lot of effort and thousands of images but the sipeed demo provides you with a pre-prepared "neural net" which is copied to the SD card.  A simple sketch reads in the net then analyses a video image before guessing what it shows.

Bella was the first subject.  The program is incredibly fast, it analyses the picture in about a second then guesses.  Bella isn't a very good subject as she moves around and takes whatever position she wants in front of the camera.  The best guess is shown below, the AI top answer is "Fur coat" - very funny, but actually accurate, after that it suggests she is a German Shepherd or police dog! Wow.


I found this a stunning result.  AI hardware, costing £20 can actually identify a dog and have a good guess at the breed within a second.  It shows how real AI technology is and indicates that practical systems are within our reach.  Another stunning Elektor article which is the start of a short series, so there should be more to blog about soon.

Thursday, 4 March 2021

Ben Eater : Arduino EEPROM Programmer

 Having substantially completed Ben's 6502 Hello World project I can start to think what to progress myself.  My priority is to see if I can avoid the need to remove the EEPROM, burn new code in a programmer, and replace it each time I write, amend or correct a 6502 assembly program.

I have a Mega 2560 attached to the 6502 board connected to the address and data buses and therefore connected to most pins on the AT28C256 EEPROM.  The TLS EEPROM programmer I have been using doesn't require special hardware capabilities so providing I connect the Mega to the three EEPROM control pins (Output Enable, Write Enable, Chip Enable) I should be able to write programs to it.

Prototype EEPROM write sketch

I started with a separate breadboard EEPROM with digital I/O pins on an Arduino Mega 2560 connected to each I/O pin.  I firstly wrote a sketch to read data from the EEPROM and display on the Arduino serial monitor.  Functionally this program was very similar to the 6502 video program to read from memory, so it was easy to implement in a sketch.

Ben has an Arduino EEPROM programmer as a separate video tutorial.  It uses a nano and shift registers instead of a Mega to read/write the EEPROM but it provided useful hints for me to write to the EEPROM.  In fact the write process is very simple: disable the output pins, set address and data values on the bus then pulse Write Enable low for 1 microsecond to write data.  To test the program I took the EEPROM which previously displayed "Goodbye, Cruel W" and changed the letter "l" (0x68" to "t" (0x74).  I replaced the EEPROM in the 6502 board and checked that the message displayed was updated. 


John65-02

Originally it was my intention to retrofit this change to the BenEater circuit but instead I have started a new circuit which I will call John65-02.  Leaving the mega connected to ROM I also connected up a 6502 CPU on the board.  CPU and ROM share the same address and data bus so I simply added chip enable, output enable, write enable logic from 6502 as previously.  I connected Bens clock circuit for a testing clock signal.  With the mega connected to 6502 read-write bus, clock and reset I was able to run the Mega monitor program to watch what was happening on the processor.  Importantly, without changing any connections I can also run a sketch to re-program the ROM.  I thought I might have to turn off the 6502 using its ready or bus enable pins but it turned out that , as long as I stopped the clock I could run the ROM update.

This is great progress, I can avoid removing chips to change a program.  It was becoming a liability inserting/removing the chip on a crowded board.  Eventually wires would be dislodged causing a difficult debugging exercise.

In addition to being able to write to the ROM, I have more work to do.  I need to be able to move the raw data from a PC file into a sketch so that I can download it to the ROM.  I am considering using X/Y/ZMODEM to do this, as I did for the RPI bare metal programs.  Alternatively I have ordered a wide 28 pin ZIF socket, which I could put into the circuit to make ROM extraction / replacement easier.

Ben Eater : 6502 : Clock and Loop

 We now have our hello world program using Ben's home made clock and all looks good.  The clock has been really helpful in allowing us to step through programs and run them slowly but we should really run more quickly using an oscillator.  In previous tutorials we looked at a number of timing considerations relating to our chipset.  Now (video #8) Ben guides us through clock considerations for our breadboard circuit. 

All circuits have inductance and capacitance from wires and physical geometry of the system, breadboard circuits suffer more than PCBs.  The unwanted effects include spikes and sluggish changes and they increase with frequency. Looking at the 6502 data sheet we need our rising or falling pulse to transition within 5ns.  Ben checks the diy clock and 5ns response is fine.

The 1mhz clock chip is then connected to the circuit.  Again 5ns response is fine so the 6502 should be fine. However the circuit doesn’t work.  The LCD is reacting more slowly than it should. In fact if you add 750 NOP instructions to commands the circuit works!

Our problem (video #9) is that we don’t check the LCD busy flag before sending the next command to it.  Ben guides through branching, looping and reading LCD values so that we can check LCD status and send commands in an orderly fashion. Our LCD happily displays text with the 6502 working at 1MHz.

Now we have the ability to loop we can tidy up our assembly program to send a message, we store the message characters as data in a block of memory and loop to read through upto a null byte.

Our Hello World is now fully working and we have learnt a huge amount about building basic 6502 systems along the way.