Sikau

Project: A whole computer · Day 2 · about 40 min

The CPU

Decode machine words with masks and run them: fetch, decode, execute.

Run the words

Your CPU from How computers work read text. A real one reads numbers. Decode each word by shifting and masking its fields, then execute — with registers kept to 8 bits and a SYS instruction that traps to the operating system (you'll need that on Day 4).

Decoding with masks

function decode(word) {
  return { op: word >> 12, r: (word >> 10) & 3, s: (word >> 8) & 3, n: word & 255 };
}

function newMachine() {
  return { pc: 0, regs: [0, 0, 0, 0], out: [], steps: 0, halted: false };
}

// Runs one instruction. Returns { sys: n } for a SYS instruction, otherwise null.
function step(m, words) {
  const { op, r, s, n } = decode(words[m.pc]);
  m.pc++;
  m.steps++;
  const R = m.regs;
  switch (op) {
    case 0: m.halted = true; m.pc--; break;
    case 1: R[r] = n; break;
    case 2: R[r] = (R[r] + R[s]) & 255; break;
    case 3: R[r] = (R[r] - R[s] + 256) & 255; break;
    case 4: m.pc = n; break;
    case 5: if (R[r] === 0) m.pc = n; break;
    case 6: if (R[r] !== 0) m.pc = n; break;
    case 7: m.out.push(R[r]); break;
    case 8: R[r] = (R[r] + 255) & 255; break;
    case 9: R[r] = (R[r] + 1) & 255; break;
    case 10: return { sys: n };
  }
  return null;
}

function execute(words, maxSteps = 10000) {
  const m = newMachine();
  while (!m.halted && m.pc < words.length) {
    if (m.steps >= maxSteps) throw new Error("Too many steps");
    step(m, words);
  }
  return { out: m.out, regs: m.regs, steps: m.steps };
}

The rest of this lesson

  • Executing each operationexplain
  • Guarding the machineexplain
  • Decode 0x2900predict
  • Decode the fieldsmatch
  • Build the CPUwrite code
  • Badge quizquiz
  • What you builtrecap

Continue this lesson free — run real code in your browser

Continue with Google

See all 4 lessons of Project: A whole computer