← hub

8-bit CPUCPU 8-bit

A whole computer small enough to see at once: 16 bytes of memory, two registers, an adder and one shared bus. Write a tiny program, then push the clock one pulse at a time and watch every wire. Satu komputer utuh yang cukup kecil buat dilihat sekaligus: memori 16 byte, dua register, satu penjumlah dan satu bus bersama. Tulis program kecil, lalu dorong clock satu pulsa demi satu pulsa dan lihat setiap kabelnya.

1The machineMesinnya

Every part talks over the bus in the middle. Only one part may put a number on it per clock pulse (amber), any part may take it (blue). The control word at the bottom decides who does what. Semua bagian ngobrol lewat bus di tengah. Tiap pulsa clock cuma satu bagian yang boleh menaruh angka di bus (oranye), bagian mana pun boleh mengambilnya (biru). Control word di bawah yang menentukan siapa ngapain.

clock2 Hz
BUS
—

puts on the busmenaruh ke bus takes from the busmengambil dari bus other controlkontrol lain

2The programProgramnya

Write assembly on the left and load it, or flip the bits in memory by hand like the DIP switches on the real thing. Instructions are one byte: the top 4 bits say what, the bottom 4 bits say which address (or which number, for LDI). Tulis assembly di kiri lalu muat, atau balik bit memorinya pakai tangan seperti saklar DIP di mesin aslinya. Satu instruksi = satu byte: 4 bit atas bilang apa, 4 bit bawah bilang alamat mana (atau angka berapa, untuk LDI).

addralamatbits (tap to flip)bit (ketuk buat balik)decmeaningarti

3Instruction set and microcodeSet instruksi dan microcode

Every instruction starts with the same two steps, T0 and T1: fetch the next byte into the instruction register. What happens after that is the microcode: a fixed table of which control lines switch on at each step. The row running now is lit. Setiap instruksi mulai dengan dua langkah yang sama, T0 dan T1: ambil byte berikutnya ke instruction register. Langkah sesudahnya itu microcode: tabel tetap soal kabel kontrol mana yang nyala di tiap langkah. Baris yang sedang jalan menyala.

4How it worksCara kerjanya

Why a bus? Wiring every part to every other part would need dozens of 8-wire cables. Instead they all share one set of 8 wires, and the control lines take turns: in each clock pulse exactly one part drives the bus and the others either listen or ignore it.

Why only 16 bytes? The address is 4 bits, and 4 bits can count 0 to 15. That's also why an instruction can only point at 16 places: the other 4 bits of the byte are the opcode. Real CPUs use more bits for the same idea.

How does it make decisions? The adder remembers two facts from its last sum: carry (the answer didn't fit in 8 bits) and zero (the answer was 0). JC and JZ only jump when that fact is true. That is all an if or a loop is, down here.

Subtraction is addition. SUB flips every bit of B and adds 1 (two's complement), then uses the same adder. So when A ≥ B the carry comes out as 1, and when A < B it's 0. The multiply example uses exactly that to know when to stop.

Try:

  • Count down from 10 to 0, then halt. (hint: SUB then JZ)
  • Load "Multiply" and change x and y. What happens with 20 × 13, and why?
  • Make the output show only even numbers, 0 to 254, forever.
  • Load "Fibonacci", then flip a bit in the data at the bottom of memory before you press Run.

Kenapa pakai bus? Kalau tiap bagian disambung ke semua bagian lain, butuh puluhan kabel 8 jalur. Jadi semuanya berbagi satu set 8 kabel, dan kabel kontrol yang mengatur giliran: tiap pulsa clock tepat satu bagian yang menaruh angka di bus, sisanya mendengarkan atau cuek.

Kenapa cuma 16 byte? Alamatnya 4 bit, dan 4 bit cuma bisa menghitung 0 sampai 15. Itu juga sebabnya instruksi cuma bisa menunjuk 16 tempat: 4 bit lainnya dipakai buat opcode. CPU beneran pakai bit lebih banyak, idenya sama.

Gimana dia bisa memutuskan? Penjumlahnya mengingat dua fakta dari hasil terakhir: carry (hasilnya nggak muat di 8 bit) dan zero (hasilnya 0). JC dan JZ cuma lompat kalau fakta itu benar. Di level ini, cuma itu isi sebuah if atau loop.

Pengurangan itu penjumlahan. SUB membalik semua bit B lalu menambah 1 (komplemen dua), terus pakai penjumlah yang sama. Jadi kalau A ≥ B carry-nya 1, kalau A < B carry-nya 0. Contoh "Perkalian" memakai persis itu buat tahu kapan berhenti.

Coba:

  • Hitung mundur dari 10 ke 0, lalu berhenti. (petunjuk: SUB lalu JZ)
  • Muat "Perkalian" dan ganti x dan y. Apa yang terjadi dengan 20 × 13, dan kenapa?
  • Bikin output cuma menampilkan bilangan genap, 0 sampai 254, terus-menerus.
  • Muat "Fibonacci", lalu balik satu bit di data paling bawah memori sebelum menekan Jalan.