BLUE
kissa.bsky.social
@kissa.bsky.social
Tomfoolery, nincompoopery, programming
37 followers374 following39 posts
kissa.bsky.social

First we need memory. 64kB will be nice. And the ability to read a word from the memory. Word is 16 bits.

const MEM_SIZE = 256 * 256
const mem = new Uint8Array(MEM_SIZE)

function error(msg: string) {
	throw new Error(msg)
}

function read(addr: number) {
	if (addr < 0 || addr >= MEM_SIZE) {
		error(`read: invalid address ${addr}`)
	}

	return (mem[addr] << 8) + mem[addr + 1]
}

function test() {
	mem[0] = 0x12
	mem[1] = 0x34

	console.log(`0x${read(0).toString(16)}`)
	// => 0x1234
}

test()
1

kissa.bsky.social

There's a bug in the above code. Reading at address 65535 should work, and let's make it so that the memory wraps (in practice rarely happens, but details matter)

index.test.ts:
✓ read word from memory [0.06ms]
12 |
13 | test("read at memory boundary", () => {
14 | 	mem[MEM_SIZE - 1] = 0x98
15 | 	mem[0] = 0x76
16 |
17 | 	expect(read(MEM_SIZE - 1)).toBe(0x9876)
    ^
error: expect(received).toBe(expected)

Expected: 39030
Received: NaN
1
kissa.bsky.social
@kissa.bsky.social
Tomfoolery, nincompoopery, programming
37 followers374 following39 posts