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

Here's the whole set of immediate instructions. This makes me reconsider the design, not only because of the effort of testing all of that stuff but also because I don't want to type "SHLAB" when I just want to do a 1-bit left shift. Maybe it's time to write some programs first and reconsider this.

Previous picture, plus new stuff (8-bit immediate instructions)

// If bit 6 (immediate mode) is set, bit 7 sets "byte mode"
//
// Instruction names are postfixed with "b" for immediate.
// Arithmetic instructions with 8-bit immediate operand (96-111):
//
// addb op1, op2, imm8 ; op1 <- op2 + imm8     01100000
// subb
// mulb
// divb
// modb
// andb
// orb
// xorb
// shlb
// shrb
// add1b
// sub1b
// movb
//
// // Arithmetic instructions with 8-bit immediate operand, augmented assignment
// (112-127):
//
// addab op1, imm8     ; op1 <- op2 + imm8     01110000
// subab
// mulab
// divab
// modab
// andab
// orab
// xorab
// shlab
// shrab
// incab
// decab
// nopab
1

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
@kissa.bsky.social
Tomfoolery, nincompoopery, programming
37 followers374 following39 posts