Video from a BBC micro:bit with IchigonQuest

Back in 2017 I played with projects to get composite video from an Arduino – huge fun and very satisfying in a retro way. After trying TinyBASIC on a BBC micro:bit, I fell down a bit of an internet search rabbit hole and discovered the world of IchigonJam (strawberry jam!), a Japanese computer the size of a Raspberry Pi that you program in BASIC. It uses a PS/2 keyboard and produces low-resolution composite video to be viewed on a TV with an AV input. A very cute and intriguing machine.

It had been bugging me for years that it must be possible to get composite video out of a BBC micro:bit the same way as on the Arduino and IchigonJam. And it is, and someone has already done it. Welcome, IchigonQuest! It’s a very quirky, very Japanese way of coding microcontrollers in something a bit like BASIC. There’s a whole ecosystem around it, and as they’re not shipping to the UK at the moment I put it out of my mind.

Then I found there’s a micro:bit port of it. And I got it working. I have no idea what I’m doing, but it works! The micro:bit becomes a self-contained computer with its own keyboard and screen that you can program on its own.

Here’s what I used:

- A V1 BBC micro:bit
- A micro:bit breakout board
- Various jumper wires
- A small breadboard
- A 100 ohm and a 470 ohm resistor
- An RCA / phono plug to get video into the TV
- A TV set. I used a cheap fairly modern Chinese one, bought in the UK but still apparently happy to display NTSC video
- A PS/2 keyboard. (Found on the street with the plug helpfully already chopped off – as used in my previous micro:bit PS/2 keyboard projects).
- Downloaded microDakeOS.20180925.zip from https://p.na-s.jp/, extracted ichigonquest.ubit.dj.en.hex and flashed it to the micro:bit

Here’s how I wired it up:

micro:bit pin connect to
15 100 ohm resistor to video out phono inner PIN
14 470 ohm resistor to video out (same as above)
GND video GND (outer ring of RCA / phono plug)
16 PS/2 keyboard clock wire
8 PS/2 keyboard data wire
3v PS/2 keyboard 5v power in wire
GND PS/2 keyboard ground wire

wiring micro:bit into TV

I very much was not expecting to see anything. I’d bodged together these wires from web pages in Japanese I barely understood even with Google Translate, and filled in some gaps from my own experience connecting PS/2 keyboards to micro:bits. I connected it all up, turned the TV on, switched it to the AV input… and a picture appeared!

I’m still puzzling out how it all works, but I managed to draw my own icons on the micro:bit display and even wrote my first proper program!

IchigonQuest program running on BBC micro:bit

And here’s a bit of a longer walkthrough of how it works:

Sound update

I’ve now figured out that the piezo speaker goes on micro:bit pin 12! Alas it only beeps, you can’t seem to play tunes on it. But it’s a nice addition.

And yes, that’s forbidden pin 12, reserved for accessibility. So if you want to hook up a buzzer, you’ll need a breakout board that allows access to this pin. The Kitronik one I have does, but the Pimoroni pin:bit doesn’t. (Not a criticism of Pimoroni, you’re really not supposed to use pin 12 for anything!)

New video update

Here’s another video talking about how much coding IchigonQuest is like assembly language. And I demonstrate my inability to remember its name:

Posted in computers, microbit | Tagged , , , | Leave a comment

Run BASIC on a BBC micro:bit

It was recently BASIC’s 57th birthday, the programming language many of us first used to do any computer programming on computers from the 1970s and 1980s like the Commodore PET, Sinclair ZX Spectrum or Apple 2. Worth remembering too that part of the team that created BASIC was Sister Mary Kenneth Keller, the first woman and one of the first people in the US to get a PhD in computer science.

I’d known for a while that there was a Japanese port of TinyBASIC for the BBC micro:bit. To be honest I thought it was a bit of a novelty as you need a computer to connect to it that will be more powerful than the micro:bit and able itself to run proper BASIC, but it’s actually very, very clever. It will allow you to access features of the micro:bit itself, like the LED display, the buttons, GPIO pins and so on, meaning you can do real physical computing with the micro:bit in BASIC. You don’t need any special hardware, just a micro:bit and some sort of serial terminal on a computer connected to the micro:bit by USB.

To get it running go to https://github.com/Tamakichi/ttbasic_microbit – I used Google translate to help me navigate the page. Download the ZIP file and unpack the hex file and flash it to a V1 micro:bit. (It looks like it’s not yet been updated to work on the newer micro:bit with sound).

You can use terminal software to communicate with it, but it’s simpler to use Chrome and a web-based alternative. You can use the online micro:bit Python editor, but this is probably better as it handles scrolling well: https://googlechromelabs.github.io/serial-terminal/. The video at the top of this page shows how to set it up.

A very cool feature is that you can save files to flash memory, just as you’d save programs to cassette or disk on an 8-bit computer like the ZX Spectrum. There are 16 slots for programs, accessed with the SAVE and LOAD commands. FILES lists everything in the file system.

The program in slot 0 can be made to run automatically at start up – press button B at reset or, if on batteries, Press and hold Button A and Button B for 5 seconds, then release only Button A

You can play music out of piezo speaker attached to pin 8. It also supports NeoPixel 8×8 matrix displays, for example for displaying Japanese characters.

It also has a real time clock, though it’ll lose its time when you power off. Use
SETDATE Year, month, day, hour, minute, second
eg
SETDATE 2021,5,3,12,0,0

There’s a lot more you can do with TinyBASIC on the micro:bit. There’s a comprehensive PDF manual in Japanese which you can translate using Google Translate to discover more.

Here are some sample programs featured in the video:

Blinking LED

1 'blink
5 MATRIX OFF
10 GPIO 3,OUTPUT
20 OUT 3,LOW
30 GPIO 26,OUTPUT
35 "@loop"
40 OUT 26,HIGH
50 WAIT 300
60 OUT 26,LOW
70 WAIT 300
80 GOTO "@loop"

Button input

10 CLS
20 IF !IN(BTNA) ?"Button A"
30 IF !IN(BTNB) ?"Button B"
40 WAIT 200
50 GOTO 20

Fill all the LEDs

10 CLS 1
20 D=1
30 FOR Y=0 TO 4
40 FOR X=0 TO 4
50 PSET X,Y,D
60 WAIT 100
70 NEXT X
80 NEXT Y
90 IF D D=0 ELSE D=1
100 GOTO 30

LED matrix message display

10 CLS 1
20 MSG LEFT,200,"Hello world! "
30 FOR I=O TO 30
40 MSG DOWN,50,I/10
50 WAIT 50
60 MSG LEFT,100,I%10
70 NEXT I
80 WAIT 500
90 GOTO 20
Posted in computers | Tagged , , | Leave a comment

Drive 4 digit 7-segment LED displays direct from a micro:bit

I bought a bag of random 7-segment LED displays to play around with, originally with the idea of adding them to the address and data busses on my 6502 breadboard computer. That became a bit tricky, but I got thinking about adding them to a BBC micro:bit. You can buy excellent displays for the micro:bit, like this one from Monk Makes, but I decided to to build and code my own from scratch, and like building and coding my own keypads, I learned a lot about an electronic component we take for granted.

It turns out that single-digit displays work pretty much as I expected. Each segment has a pin, and you send some volts to the pins and they light up, so a display that shows one digit and a decimal point will have nine pins, one for each segment and a common anode or cathode.

Multi-digit displays work rather differently, however. A 4 digit display does not use 4 x 8 pins. Instead it uses the same 8 pins to choose which segments to light plus 4 extra pins to chose which digit is going to light up at any given time. This means you have to multiplex the display: set the pins for the first digit, light it up by selecting it, change the segments and select the second digit, and so on. So each number is flashed on in turn. The video at the top of the page shows it working slowly. Normally you flash the digits so quickly, persistence of vision means that to the human eye it looks like they’re all on at the same time. This article on Raspi.TV explains it really well.

My display is a common cathode type. This means that you have to ground each digit selection pin in turn to make that digit light up.

If you want to try this, as well as the 4 digit LED display you’ll need a micro:bit, a breakout board, a breadboard, some jumper wires and around 7 or 8 resistors. I used 330 ohm resistors, but you may be able to use lower values depending on your display. I didn’t wire up the decimal point, just the 7 segments. I connected them like this:

micro:bit pin   Resistor?   LED pin
--------------------------------------------------
0               yes         1
1               yes         2
8               yes         4
9               yes         7
12              yes         11
2		yes         10
13              yes         5
14              no          Digit 1 select (pin 12)
15              no          Digit 2 select (pin 9)
16              no          Digit 3 select (pin 8)
10              no          Digit 4 select (pin 6)

I decided to write the program to drive the display using MakeCode blocks. You can see the project here: https://makecode.microbit.org/_HxbLuUWob6Rs

It’s a simple counter that goes up and down when you press button B or button A. A forever loop keeps scanning the display, grounding each digit pin in turn to show the correct number. Timing is pretty crucial here, so I put the main program loop inside a ‘while true’ block inside the ‘forever’ block to remove the small delay that MakeCode adds to forever loops. It seems to work! I did occasionally see some ghosting artefacts, though I think it’s pretty amazing it works at all.

So, next time you look at a numerical LED display on a clock, cooker, or fridge, spare a thought for the design effort that went in to making it work.

Posted in computers | Tagged , , | Leave a comment

6502 breadboard computer part 8 – next steps

6502 breadboard computer

I recently achieved my goal of making a 1970s-style 6502 breadboard computer that I can use to practice machine code programming. It has a hexadecimal keypad for entering instructions, a 16×2 alphanumeric LCD display and a very basic monitor program that allows me to enter programs up to 256 bytes in length and run them.

So it’s time to take stock and consider what next…

Practice my coding
There’s a lot I can do with this computer to learn more about 6502 machine code. There are stacks of books and websites to mine, from classics like Programming the 6502 by Rodnay Zaks, to helpful blogs like this one and this interactive one.

Improve the monitor
- The Apple 1 computer had an amazingly compact monitor program written by Steve Wozniak, usually called ‘wozmon’ – the source code is available and it’ll be instructional to dig into it and see how it works. Straight away, for example, I saw he’d used logical shifts instead of rotates – if I’d done that I’d never have had the carry bug that was corrupting my display of hexadecimal numbers.
- I could also make improvements to the ROM to make displaying text easier. At the moment you have to load the ASCII value of the character you want to show into the accumulator and call a subroutine. There’s almost certainly a better way of doing this. I could make it easier to write text across two lines, for example.
- I don’t want a full assembler or typewrite keyboard, but I do have a lot of spare shift-keys, so it might be fun to put frequently used opcodes as shortcuts on some spare keys, Sinclair ZX81/Spectrum-style.
- It might be useful to use the second line of the monitor display to show the contents of key registers and status flags.
- Add some way of making it easier to include keypad input in user-entered programs. This needs some thought and maybe a fundamental redesign of my monitor. At the moment the monitor sits in the ROM and pressing a key triggers an IRQ which updates the monitor display. Pressing GO (shift-A) jumps to memory location $0300 and starts executing any user-entered program. You can drop out of the user-entered program and back to the monitor with an RTI instruction. I guess looking at wozmon or even the Kim-1 monitor program might be instructive.
- Allow longer programs perhaps, but will I ever really want to type more than 256 opcodes by hand? (See note below on adding, or not adding, storage).

You know nothing of future time, and yet in my teeming circuitry I can navigate the infinite delta streams of future possibility and see that there must one day come a computer whose merest operational parameters I am not worthy to calculate, but which it will be my destiny eventually to design.
The Deep Thought computer in The Hitch-Hiker’s Guide to the Galaxy by Douglas Adams, Series 1, Fit the Fourth.

Improve the hardware
I could rewire the LCD and redesign the monitor code to drive it in 4-bit mode to free up 4 more I/O pins on the VIA, but I’m not sure what I’d actually use them for. I think I’m going to keep this machine pretty much as it is, so no video output, no storage device, no serial access, no BASIC interpreter… but perhaps design and build a second machine. This will have a full keyboard, BASIC and rather than a video output, a wide LCD display and maybe even a thermal printer in an Aim-65 style. I’d decided this and then discovered this amazing project, which is very close to what I want to build.

I’m keen to keep it broadly sympathetic to 70s/80s tech, so I’d like to avoid any microcontrollers that are more powerful than the 6502 itself… but what do you think I should do next?

Posted in computers | Tagged , | 3 Comments

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

Posted in computers | Tagged , | Leave a comment