Programming the Micral N

By Sylvain Glaize

So we have functional hardware, functional emulation. We also have everything we need to communicate with the machine via the serial port. It's time to plan a few programs that could be run.

To do this, we plan to use a floppy drive simulator, but this part will be covered in a future article.

For the moment, let's talk about programming the Micral N.

The boot

When the machine starts up in our example configuration, the ROM on the boot card fetches data from a floppy drive.

This is not compulsory on a Micral N, which could directly contain software to be executed, or read data from another device. Our configuration seems to be designed to accommodate software that can be easily modified.

This boot program reads the first 14 kilobytes of data from the floppy disk and copies them to RAM from the low addresses. The boot card is then in a mode where data is read from ROM, but written to RAM for common addresses.

Once the data has been loaded, the program resets the machine, which is indicated by the boot card that no longer allows access to the ROM. This means that the program coming from the floppy disk is fully available, and has complete control of the machine.

The system

We have no original software for this machine, apart from the contents of the two ROMs. However, it is possible to imagine that this data could either constitute a programme used directly or an operating system.

The 2 kilobytes of the monitor at the top of the memory are not a system in themselves. It is a program that can be entered to operate and debug the machine. But this part is not built as a system from which you can call predefined functions. In fact, this monitor ROM could just as easily be replaced by RAM.

Special features of the boot programme

This very short program (199 useful bytes) uses the two Stack/Channel cards. One of them is used as secondary memory in the form of a stack. The 8008 does not have RAM stack management like the Z80. This processor only has a very small address stack, internal and not accessible, which is used for subroutine calls.

This secondary memory card is used to calculate the checksum of data read from the floppy disk, without the need for RAM, which is being filled with this data.

This tells us that to write to this stack, you need to use the `OUT $16` instruction (send to output port number $16 in hexadecimal). To read from the stack, use the instruction `INP $6` (read from input port number 6).

The other card acts as a read buffer. The program places the expected number of bytes in the buffer, then asks the disk controller to start reading. Once the expected number of bytes has been received by the card, it signals this and the processor can then read and process them.

The passage of time

Another interesting feature of this boot program is the use of the clock generated by the processor card. This function is theoretically optional, as it can be deactivated. However, on our example, it is compulsory.

Each clock stroke generates an interrupt on the 8008, which connects to `Reset 7' at address $38 (Z80 users will recognise this). This address contains a counter management system, which is initialised and then read in a routine that acts as a waiting loop for a specific time.

This routine is needed to generate the signals sent to the floppy disk controller card.

Special features of the monitor

The first particularity of the monitor is that it has to be called explicitly. Either from a program in RAM, or from a sequence entered via the control panel.

This program needs a reserved area of RAM to house some operating variables, between $070e and $07ff. Keep this in mind if you want to use a program in RAM at the same time as the monitor.

These variables store the state of the registers at the time of arrival in the monitor. With one notable exception: it is not possible to access the instruction pointer (PC register) from within a program.

Another of these variables is used as a trampoline for programming an indirect jump. There is no instruction in 8008 to make a jump to the address contained in a register. This sequence therefore has to be generated using self-modifying code.

In the monitor, this is done in two steps: first modify the destination address of a jump instruction (`JMP`) from a register. Then make a jump to the absolute address of this modified instruction.

This technique is used throughout the monitor code, in conjunction with the use of the Stack card to track subroutine call stacks. My analysis is that, at the cost of some slowness, this preserves the 8008's internal call stack. There is never more than one subroutine call depth in the monitor.

Reading and writing

Another very interesting part of the monitor is that the program reads and writes to the serial port. It can also read and write to punched tape hardware. It can even inject data from a punched tape into memory.

But we are primarily interested in the exchanges with the serial input/output card.

And this system is very simple: when writing, a bit is read until it indicates that the card is ready to receive. As soon as it is ready, the byte is sent. For reading, it's almost the same: wait for a bit indicating that the data can be read, then read a byte until it is different from 255.

The protocol, including the data rate, is managed by the serial card. So there's no need to worry about processor time.

Level 1: House track

The first program I wrote for real hardware was a chaser: a program that lights up the LEDs on the control panel one after the other.

It's a standard. And above all, it ensured that the programme was executed without having to connect the serial link.

As it is not possible to access the LEDs directly from the control panel, which only displays what is on the address and data buses, the classic trick is to loop through a program located at particular addresses in order to generate the display of the correct LEDs.

A small timer loop is therefore located at addresses $0100, $0200, $0400, $0800, $1000 and $2000.

The programme also sends an ASCII version of the association's logo to the serial port.

MO5.com "logo" displayed on the serial output.

20230501 MicralN Emulation Panel Chenillard

Track displayed on the control panel, on the Micral N emulator (the LEDs are green on the emulator, not red as on the real hardware)

Level 2: Shooting Stars

Although we were unable to find any software specifically written for Micral N, there is some for other machines built around the 8008. This is the case with the SCELBI, on which a few game programs can be found.

I chose one, called Shooting Stars, because it's quite simple, and I adapted it.

Here it is on the emulator, waiting to see it run on the real machine.

20230501 MicralN Emulation ShootingStars

The Shooting Stars game, originally created for SCELBI, has been ported to Micral N

Level 3: a BASIC

For this third experiment, SCELBI was again used to help us out. In fact, a version of BASIC has been implemented under the name SCELBAL, and the sources are available and documented.

A few adaptations were necessary, but the programme was well thought out enough to be easily modified. There are even plans to be able to plug in program save and load routines, which would be perfectly possible on the Micral N with its floppy disk controller.

However, it's not an exercise I've taken to that extreme.

In the meantime, let's program in BASIC in the Micral N emulator.

20230501 MicralN Emulation Scelbal

SCELBAL, the BASIC of SCELBI, ported to Micral N

Now we're almost there. All that's left is to put these programs on the floppy disk emulator, plug everything in and let the magic happen.

This will be the subject of the next article.

In the meantime, don't forget to take part in our campaign! https://micral.mo5.com