Public
Edited
Oct 20, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
registers
Insert cell
Insert cell
function makeCpuRegisters() {
const registers = {
SP: 0,
PC: 0
}

const splitRegisters = ['AF', 'BC', 'DE', 'HL']

splitRegisters.forEach(pair => {
const [hi, lo] = pair

registers[hi] = 0
if (lo === 'F') {
Object.defineProperty(registers, 'lo', {
value: 0,
enumerable: false
})
} else {
registers[lo] = 0
}

Object.defineProperty(registers, pair, {
get() {
return this[hi] << 8 | this[lo]
},
set(value) {
this[hi] = (value & 0xFF00) >> 8
this[lo] = value & 0xFF
},
enumerable: true
})
})

const flags = ['z', 'n', 'h', 'c']
flags.forEach((flag, index) => {
const bit = 1 << (7 - index)
Object.defineProperty(registers, flag, {
get() {
console.log(flag, bit.toString(2), this.F & bit, this.F & bit ? true : false)
return this.F & bit ? 1 : 0
},
set(value) {
this.F = value
? this.F | bit
: this.F & ~bit
},
enumerable: true
})
})

return registers
}
Insert cell
CpuFlags = ({
zero: 1 << 7,
subtraction: 1 << 6,
halfCarry: 1 << 5,
carry: 1 << 4
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
style = htl.html`<style>
@import url('https://fonts.googleapis.com/css2?family=Press+Start+2P&display=swap');
</style>`
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more