the move machine - WIP documentation

This is very, very WIP documentation. I have a lot of notes and things in my head, and eventually it will all be here in a somewhat complete and coherent form.

opcodes are boring and take up space ;-)

So, the goal here is to make a processor that has one implicit instruction.This is for the excellent reason that I decided to do it, but there are some interesting side-effects of such a simple decision. We'll get to the deeper why after I've explained some what and how.

What an instruction is

An instruction is two consecutive words in memory:
word 0:  bit 15 unused (must be 0), bits 14–0 = source address
word 1:  bit 15 = guard flag,        bits 14–0 = destination address

To execute the instruction, the processor:

  1. reads word 0 and word 1 at the programme counter (PC)
  2. advances PC by two
  3. reads from the source address (word 0), and writes what it got to the destination address (word 1).

There is no opcode field. Every operand is an address.

(Except for one small case, which is a design choice I agonised over, and we will get to it soon.)

How?

I hear you ask, how on earth can this machine do anything?

Memory isn't a pure cache of values. It is circuits. So while we can create circuits at various addresses which do store and retrieve faithfully, we can also create circuits that do other things. Like add values, and anything else a circuit can do.

I hope you just worked out how this machine can do anything, including be Turing-complete.

Ports

You've probably worked out we are doing memory-mapped IO even to the ALU.

By moving the ALU to dedicated circuits, we can do some pretty fun things. Like run multiple ALUs. Like offboard VERY complicated things to dedicated hardware (like doing crypto inside one write/read instruction pair). We can also link processors up in fun ways, and the simulator you probably came to this page from does this.

What about literal values?

If that's the whole machine, it leaves a question. Every operand is an address. How do you ever get the number 42 into anything?

You have to read it from somewhere, so where does it live?

The naive answer, which works

Put the constant in memory and read that address:

        k42 -> x
k42:    .word 42

This is a perfectly legal program. The assembler stores 42 at some address, say 0x0020, and emits the move `[0x0020] [addr of x]`. Fine for one constant. But breaks something I want to do (or at least makes it hard) so we need a better answer. We want the literals to ride with the moves they are for. Aside from what I'll talk about below, doing this is kind of clumsy. Every literal needs a named word somewhere. There is a subtle cost that constants are stored somewhere far from the instruction that uses them. There are some positive things that this pattern promotes, but reading the program listing can get a bit exhausting going back and forth.

Use the MSb

There's a big juicy bit (mask 0x8000) at the top of the source address we do nothing with. We could just set that for immediate values of up to 0x7fff. But that would imply having to make 0x4000 the mask for negative numbers. I want 16 bits for my data. And I've got another idea for something the top bit can do in v2 of this processor design, so lets not use it here.

Also, it feels too normal to do that. Don't be normal.

The IMM port

IMM is a port at address 0x7F00, and reading from it does this: value = memory[PC] PC = PC + 1 return value

That's all. It hands you the word sitting at the current PC, and bumps PC past it.

The crucial detail is when PC is read. Remember that the processor advanced PC by two when it fetched the instruction, before it reads the source.

So by the time the source read happens, PC already points to the word immediately after the two-word instruction. Reading IMM therefore returns the word that physically follows the instruction, and skips it so the processor doesn't try to execute it.

So a literal load to memory is a three-word instruction: source word, destination word, data word.

Why do this, which looks like burning a word to save a bit (i.e. using the MSb)? Because I have a use for that MSb.

Why not do the clean version (load from an array of constants)? Because then you can't stream relocatable code into another processor over a link...