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

Actually need to implement a bunch of arithmetic instructions and test them somehow efficiently. Here's a start.

List of test instructions that ChatGPT wrote me.

export const opcodes: { [key: number]: string } = {
	0b00000000: "add", // add <op1> <op2> <op3>  - op1 <- op2 + op3
	0b00000001: "sub", // sub <op1> <op2> <op3>  - op1 <- op2 - op3
	0b00000010: "mul", // mul <op1> <op2> <op3>  - op1 <- op2 * op3
	// etc, ending with 0b00001100: "mov"
	
	0b00010000: "adda", // add <op1> <op2>       - op1 <- op1 + op2
	0b00010001: "suba", // sub <op1> <op2>       - op1 <- op1 - op2
	0b00010010: "mula", // mul <op1> <op2>       - op1 <- op1 * op2
	// etc, ending with 0b00011100: "nop"
	
	0b11110000: "hlt", // hlt <?> <?> <?>         - halt program execution
	0b11110001: "hcf", // hcf <?> <?> <?>         - halt and catch fire
}
Implementation of first 3 arithmetic instructions:

	if (instr === "add") {
		writeOperand(op1, op2value + op3value)
	}
	if (instr === "sub") {
		writeOperand(op1, op2value - op3value)
	}
	if (instr === "mul") {
		writeOperand(op1, op2value * op3value)
	}
	if (instr === "hlt") {
		return false
	}
	if (instr === "hcf") {
		error("Machine caught fire")
	}
	return true
test("arithmetic instructions", () => {
	testArithmetic(instr.add, 0x1234, 0x2345, 0x3579)
	testArithmetic(instr.sub, 0x4131, 0x2121, 0x2010)
	testArithmetic(instr.mul, 111, 20, 2220)
	testArithmetic(instr.mul, -111, 20, -2220)
	testArithmetic(instr.mul, -1, -0x7fff, 0x7fff)
	// TODO should overflow probably
	// testArithmetic(instr.mul, -1, -0x8000, -0x8000)
})
1

kissa.bsky.social

Now I need to take a break of few days, weeks, or months, depending on when I next manage to free up some time. This was a miserable week for writing code: I had planned 5 full working days and got some 11+ hours done. But such is life sometimes

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