internal/vm: update Appleone and all it’s `a` handlers to VM with `vm` handler

This commit is contained in:
Bradford Lamson-Scribner 2020-06-04 20:47:35 -06:00
parent b7a3ec4a6c
commit 67416900b4
4 changed files with 367 additions and 321 deletions

View File

@ -2,538 +2,538 @@ package vm
// interrupt, N Z C I D V // interrupt, N Z C I D V
// push PC+2, push SR - - - 1 - - // push PC+2, push SR - - - 1 - -
func execBRK(a *Appleone, o operation) error { func execBRK(vm *VM, o operation) error {
// set processer status flag to BRK // set processer status flag to BRK
a.cpu.ps = flagBreak vm.cpu.ps = flagBreak
a.pushDWordToStack(a.cpu.pc + 1) vm.pushDWordToStack(vm.cpu.pc + 1)
a.pushWordToStack(a.cpu.ps) vm.pushWordToStack(vm.cpu.ps)
a.cpu.ps |= flagDisableInterrupts vm.setFlag(flagDisableInterrupts)
a.cpu.pc = uint16(a.mem[0xFFFF])<<8 | uint16(a.mem[0xFFFE]) vm.cpu.pc = uint16(vm.mem[0xFFFF])<<8 | uint16(vm.mem[0xFFFE])
return nil return nil
} }
// pull SR, pull PC N Z C I D V // pull SR, pull PC N Z C I D V
// from stack // from stack
func execRTI(a *Appleone, o operation) error { func execRTI(vm *VM, o operation) error {
a.cpu.ps = a.popStackWord() vm.cpu.ps = vm.popStackWord()
a.cpu.pc = a.popStackDWord() vm.cpu.pc = vm.popStackDWord()
return nil return nil
} }
// M - 1 -> M N Z C I D V // M - 1 -> M N Z C I D V
// + + - - - - // + + - - - -
func execDEC(a *Appleone, o operation) error { func execDEC(vm *VM, o operation) error {
addr, err := a.getAddr(o) addr, err := vm.getAddr(o)
if err != nil { if err != nil {
return err return err
} }
b := a.mem[addr] b := vm.mem[addr]
b-- b--
a.mem[addr] = b vm.mem[addr] = b
a.maybeSetFlagZero(b) vm.maybeSetFlagZero(b)
a.maybeSetFlagOverflow(b) vm.maybeSetFlagOverflow(b)
return nil return nil
} }
// M + 1 -> M N Z C I D V // M + 1 -> M N Z C I D V
// + + - - - - // + + - - - -
func execINC(a *Appleone, o operation) error { func execINC(vm *VM, o operation) error {
addr, err := a.getAddr(o) addr, err := vm.getAddr(o)
if err != nil { if err != nil {
return err return err
} }
b := a.mem[addr] b := vm.mem[addr]
b++ b++
a.mem[addr] = b vm.mem[addr] = b
a.maybeSetFlagZero(b) vm.maybeSetFlagZero(b)
a.maybeSetFlagOverflow(b) vm.maybeSetFlagOverflow(b)
return nil return nil
} }
// X + 1 -> X N Z C I D V // X + 1 -> X N Z C I D V
// + + - - - - // + + - - - -
func execINX(a *Appleone, o operation) error { func execINX(vm *VM, o operation) error {
a.cpu.x++ vm.cpu.x++
a.maybeSetFlagZero(a.cpu.x) vm.maybeSetFlagZero(vm.cpu.x)
a.maybeSetFlagOverflow(a.cpu.x) vm.maybeSetFlagOverflow(vm.cpu.x)
return nil return nil
} }
// Y + 1 -> Y N Z C I D V // Y + 1 -> Y N Z C I D V
// + + - - - - // + + - - - -
func execINY(a *Appleone, o operation) error { func execINY(vm *VM, o operation) error {
a.cpu.y++ vm.cpu.y++
a.maybeSetFlagZero(a.cpu.y) vm.maybeSetFlagZero(vm.cpu.y)
a.maybeSetFlagOverflow(a.cpu.y) vm.maybeSetFlagOverflow(vm.cpu.y)
return nil return nil
} }
// A -> X N Z C I D V // A -> X N Z C I D V
// + + - - - - // + + - - - -
func execTAX(a *Appleone, o operation) error { func execTAX(vm *VM, o operation) error {
a.cpu.x = a.cpu.a vm.cpu.x = vm.cpu.a
a.maybeSetFlagZero(a.cpu.x) vm.maybeSetFlagZero(vm.cpu.x)
a.maybeSetFlagOverflow(a.cpu.x) vm.maybeSetFlagOverflow(vm.cpu.x)
return nil return nil
} }
// A -> Y N Z C I D V // A -> Y N Z C I D V
// + + - - - - // + + - - - -
func execTAY(a *Appleone, o operation) error { func execTAY(vm *VM, o operation) error {
a.cpu.y = a.cpu.a vm.cpu.y = vm.cpu.a
a.maybeSetFlagZero(a.cpu.y) vm.maybeSetFlagZero(vm.cpu.y)
a.maybeSetFlagOverflow(a.cpu.y) vm.maybeSetFlagOverflow(vm.cpu.y)
return nil return nil
} }
// X - 1 -> X N Z C I D V // X - 1 -> X N Z C I D V
// + + - - - - // + + - - - -
func execDEX(a *Appleone, o operation) error { func execDEX(vm *VM, o operation) error {
a.cpu.x-- vm.cpu.x--
a.maybeSetFlagZero(a.cpu.x) vm.maybeSetFlagZero(vm.cpu.x)
a.maybeSetFlagOverflow(a.cpu.x) vm.maybeSetFlagOverflow(vm.cpu.x)
return nil return nil
} }
// Y - 1 -> Y N Z C I D V // Y - 1 -> Y N Z C I D V
// + + - - - - // + + - - - -
func execDEY(a *Appleone, o operation) error { func execDEY(vm *VM, o operation) error {
a.cpu.y-- vm.cpu.y--
a.maybeSetFlagZero(a.cpu.y) vm.maybeSetFlagZero(vm.cpu.y)
a.maybeSetFlagOverflow(a.cpu.y) vm.maybeSetFlagOverflow(vm.cpu.y)
return nil return nil
} }
// M -> A N Z C I D V // M -> A N Z C I D V
// + + - - - - // + + - - - -
func execLDA(a *Appleone, o operation) error { func execLDA(vm *VM, o operation) error {
operand, err := a.getOperand(o) operand, err := vm.getOperand(o)
if err != nil { if err != nil {
return err return err
} }
a.cpu.a = operand vm.cpu.a = operand
a.maybeSetFlagZero(a.cpu.a) vm.maybeSetFlagZero(vm.cpu.a)
a.maybeSetFlagOverflow(a.cpu.a) vm.maybeSetFlagOverflow(vm.cpu.a)
return nil return nil
} }
// M -> X N Z C I D V // M -> X N Z C I D V
// + + - - - - // + + - - - -
func execLDX(a *Appleone, o operation) error { func execLDX(vm *VM, o operation) error {
operand, err := a.getOperand(o) operand, err := vm.getOperand(o)
if err != nil { if err != nil {
return err return err
} }
a.cpu.x = operand vm.cpu.x = operand
a.maybeSetFlagZero(a.cpu.x) vm.maybeSetFlagZero(vm.cpu.x)
a.maybeSetFlagOverflow(a.cpu.x) vm.maybeSetFlagOverflow(vm.cpu.x)
return nil return nil
} }
// M -> Y N Z C I D V // M -> Y N Z C I D V
// + + - - - - // + + - - - -
func execLDY(a *Appleone, o operation) error { func execLDY(vm *VM, o operation) error {
operand, err := a.getOperand(o) operand, err := vm.getOperand(o)
if err != nil { if err != nil {
return err return err
} }
a.cpu.y = operand vm.cpu.y = operand
a.maybeSetFlagZero(a.cpu.y) vm.maybeSetFlagZero(vm.cpu.y)
a.maybeSetFlagOverflow(a.cpu.y) vm.maybeSetFlagOverflow(vm.cpu.y)
return nil return nil
} }
// A + M + C -> A, C N Z C I D V // A + M + C -> A, C N Z C I D V
// + + + - - + // + + + - - +
func execADC(a *Appleone, o operation) error { func execADC(vm *VM, o operation) error {
b, err := a.getOperand(o) b, err := vm.getOperand(o)
if err != nil { if err != nil {
return err return err
} }
operand := uint16(b) operand := uint16(b)
regA := uint16(a.cpu.a) regA := uint16(vm.cpu.a)
sum := regA + operand + uint16(a.getFlag(flagCarry)) sum := regA + operand + uint16(vm.getFlag(flagCarry))
a.cpu.a = byte(sum) vm.cpu.a = byte(sum)
a.clearFlag(flagCarry) vm.clearFlag(flagCarry)
if sum > 255 { if sum > 255 {
a.setFlag(flagCarry) vm.setFlag(flagCarry)
} }
// http://www.righto.com/2012/12/the-6502-overflow-flag-explained.html // http://www.righto.com/2012/12/the-6502-overflow-flag-explained.html
a.clearFlag(flagOverflow) vm.clearFlag(flagOverflow)
if (operand^sum)&(regA^sum)&0x80 != 0 { if (operand^sum)&(regA^sum)&0x80 != 0 {
a.setFlag(flagOverflow) vm.setFlag(flagOverflow)
} }
a.maybeSetFlagZero(a.cpu.a) vm.maybeSetFlagZero(vm.cpu.a)
a.maybeSetFlagOverflow(a.cpu.a) vm.maybeSetFlagOverflow(vm.cpu.a)
return nil return nil
} }
// A - M - C -> A N Z C I D V // A - M - C -> A N Z C I D V
// + + + - - + // + + + - - +
func execSBC(a *Appleone, o operation) error { func execSBC(vm *VM, o operation) error {
operand, err := a.getOperand(o) operand, err := vm.getOperand(o)
if err != nil { if err != nil {
return err return err
} }
carry := uint16(1 - a.getFlag(flagCarry)) carry := uint16(1 - vm.getFlag(flagCarry))
regA := a.cpu.a regA := vm.cpu.a
sum := uint16(regA) - carry - uint16(operand) sum := uint16(regA) - carry - uint16(operand)
a.cpu.a = byte(sum) vm.cpu.a = byte(sum)
a.clearFlag(flagOverflow) vm.clearFlag(flagOverflow)
if byte(regA)>>7 != a.cpu.a>>7 { if byte(regA)>>7 != vm.cpu.a>>7 {
a.setFlag(flagOverflow) vm.setFlag(flagOverflow)
} }
if uint16(sum) < 256 { if uint16(sum) < 256 {
a.setFlag(flagCarry) vm.setFlag(flagCarry)
} else { } else {
a.clearFlag(flagCarry) vm.clearFlag(flagCarry)
} }
a.clearFlag(flagOverflow) vm.clearFlag(flagOverflow)
if ((255-operand)^a.cpu.a)&(regA^a.cpu.a)&0x80 != 0 { if ((255-operand)^vm.cpu.a)&(regA^vm.cpu.a)&0x80 != 0 {
a.setFlag(flagOverflow) vm.setFlag(flagOverflow)
} }
a.maybeSetFlagZero(a.cpu.a) vm.maybeSetFlagZero(vm.cpu.a)
a.maybeSetFlagOverflow(a.cpu.a) vm.maybeSetFlagOverflow(vm.cpu.a)
return nil return nil
} }
// X -> M N Z C I D V // X -> M N Z C I D V
// - - - - - - // - - - - - -
func execSTX(a *Appleone, o operation) error { func execSTX(vm *VM, o operation) error {
addr, err := a.getAddr(o) addr, err := vm.getAddr(o)
if err != nil { if err != nil {
return err return err
} }
a.mem[addr] = a.cpu.x vm.mem[addr] = vm.cpu.x
return nil return nil
} }
// Y -> M N Z C I D V // Y -> M N Z C I D V
// - - - - - - // - - - - - -
func execSTY(a *Appleone, o operation) error { func execSTY(vm *VM, o operation) error {
addr, err := a.getAddr(o) addr, err := vm.getAddr(o)
if err != nil { if err != nil {
return err return err
} }
a.mem[addr] = a.cpu.y vm.mem[addr] = vm.cpu.y
return nil return nil
} }
// A -> M N Z C I D V // A -> M N Z C I D V
// - - - - - - // - - - - - -
func execSTA(a *Appleone, o operation) error { func execSTA(vm *VM, o operation) error {
addr, err := a.getAddr(o) addr, err := vm.getAddr(o)
if err != nil { if err != nil {
return err return err
} }
a.mem[addr] = a.cpu.a vm.mem[addr] = vm.cpu.a
return nil return nil
} }
// branch on Z = 1 N Z C I D V // branch on Z = 1 N Z C I D V
// - - - - - - // - - - - - -
func execBEQ(a *Appleone, o operation) error { func execBEQ(vm *VM, o operation) error {
if a.getFlag(flagZero) == flagZero { if vm.getFlag(flagZero) == flagZero {
a.branch(o) vm.branch(o)
} }
return nil return nil
} }
// branch on Z = 0 N Z C I D V // branch on Z = 0 N Z C I D V
// - - - - - - // - - - - - -
func execBNE(a *Appleone, o operation) error { func execBNE(vm *VM, o operation) error {
if a.getFlag(flagZero) != flagZero { if vm.getFlag(flagZero) != flagZero {
a.branch(o) vm.branch(o)
} }
return nil return nil
} }
// branch on V = 0 N Z C I D V // branch on V = 0 N Z C I D V
// - - - - - - // - - - - - -
func execBVC(a *Appleone, o operation) error { func execBVC(vm *VM, o operation) error {
if a.getFlag(flagOverflow) == 0 { if vm.getFlag(flagOverflow) == 0 {
a.branch(o) vm.branch(o)
} }
return nil return nil
} }
// branch on V = 1 N Z C I D V // branch on V = 1 N Z C I D V
// - - - - - - // - - - - - -
func execBVS(a *Appleone, o operation) error { func execBVS(vm *VM, o operation) error {
if a.getFlag(flagOverflow) != 0 { if vm.getFlag(flagOverflow) != 0 {
a.branch(o) vm.branch(o)
} }
return nil return nil
} }
// bits 7 and 6 of operand are transfered to bit 7 and 6 of SR (N,V); // bits 7 and 6 of operand are transfered to bit 7 and 6 of SR (N,V);
// the zeroflag is set to the result of operand AND accumulator. // the zeroflag is set to the result of operand AND accumulator.
func execBIT(a *Appleone, o operation) error { func execBIT(vm *VM, o operation) error {
operand, err := a.getOperand(o) operand, err := vm.getOperand(o)
if err != nil { if err != nil {
return err return err
} }
a.maybeSetFlagZero(a.cpu.a & operand) vm.maybeSetFlagZero(vm.cpu.a & operand)
a.clearFlag(flagOverflow) vm.clearFlag(flagOverflow)
if operand&flagOverflow != 0 { if operand&flagOverflow != 0 {
a.setFlag(flagOverflow) vm.setFlag(flagOverflow)
} }
a.maybeSetFlagOverflow(operand) vm.maybeSetFlagOverflow(operand)
return nil return nil
} }
// branch on C = 0 N Z C I D V // branch on C = 0 N Z C I D V
// - - - - - - // - - - - - -
func execBCC(a *Appleone, o operation) error { func execBCC(vm *VM, o operation) error {
if a.getFlag(flagCarry) == 0 { if vm.getFlag(flagCarry) == 0 {
a.branch(o) vm.branch(o)
} }
return nil return nil
} }
// branch on N = 1 N Z C I D V // branch on N = 1 N Z C I D V
// - - - - - - // - - - - - -
func execBMI(a *Appleone, o operation) error { func execBMI(vm *VM, o operation) error {
if a.getFlag(flagNegative) == flagNegative { if vm.getFlag(flagNegative) == flagNegative {
a.branch(o) vm.branch(o)
} }
return nil return nil
} }
// branch on N = 0 N Z C I D V // branch on N = 0 N Z C I D V
// - - - - - - // - - - - - -
func execBPL(a *Appleone, o operation) error { func execBPL(vm *VM, o operation) error {
if a.getFlag(flagNegative) == 0 { if vm.getFlag(flagNegative) == 0 {
a.branch(o) vm.branch(o)
} }
return nil return nil
} }
// branch on C = 1 N Z C I D V // branch on C = 1 N Z C I D V
// - - - - - - // - - - - - -
func execBCS(a *Appleone, o operation) error { func execBCS(vm *VM, o operation) error {
if a.getFlag(flagCarry) != 0 { if vm.getFlag(flagCarry) != 0 {
a.branch(o) vm.branch(o)
} }
return nil return nil
} }
// X - M N Z C I D V // X - M N Z C I D V
// + + + - - - // + + + - - -
func execCPX(a *Appleone, o operation) error { func execCPX(vm *VM, o operation) error {
operand, err := a.getOperand(o) operand, err := vm.getOperand(o)
if err != nil { if err != nil {
return err return err
} }
a.compare(a.cpu.x, operand) vm.compare(vm.cpu.x, operand)
return nil return nil
} }
// A EOR M -> A N Z C I D V // A EOR M -> A N Z C I D V
// + + - - - - // + + - - - -
func execEOR(a *Appleone, o operation) error { func execEOR(vm *VM, o operation) error {
operand, err := a.getOperand(o) operand, err := vm.getOperand(o)
if err != nil { if err != nil {
return err return err
} }
a.cpu.a ^= operand vm.cpu.a ^= operand
a.maybeSetFlagOverflow(a.cpu.a) vm.maybeSetFlagOverflow(vm.cpu.a)
a.maybeSetFlagZero(a.cpu.a) vm.maybeSetFlagZero(vm.cpu.a)
return nil return nil
} }
// A - M N Z C I D V // A - M N Z C I D V
// + + + - - - // + + + - - -
func execCMP(a *Appleone, o operation) error { func execCMP(vm *VM, o operation) error {
operand, err := a.getOperand(o) operand, err := vm.getOperand(o)
if err != nil { if err != nil {
return err return err
} }
a.compare(a.cpu.a, operand) vm.compare(vm.cpu.a, operand)
return nil return nil
} }
// Y - M N Z C I D V // Y - M N Z C I D V
// + + + - - - // + + + - - -
func execCPY(a *Appleone, o operation) error { func execCPY(vm *VM, o operation) error {
operand, err := a.getOperand(o) operand, err := vm.getOperand(o)
if err != nil { if err != nil {
return err return err
} }
a.compare(a.cpu.y, operand) vm.compare(vm.cpu.y, operand)
return nil return nil
} }
// 0 -> C N Z C I D V // 0 -> C N Z C I D V
// - - 0 - - - // - - 0 - - -
func execCLC(a *Appleone, o operation) error { func execCLC(vm *VM, o operation) error {
a.clearFlag(flagCarry) vm.clearFlag(flagCarry)
return nil return nil
} }
// 0 -> D N Z C I D V // 0 -> D N Z C I D V
// - - - - 0 - // - - - - 0 -
func execCLD(a *Appleone, o operation) error { func execCLD(vm *VM, o operation) error {
a.clearFlag(flagDecimalMode) vm.clearFlag(flagDecimalMode)
return nil return nil
} }
// 0 -> I N Z C I D V // 0 -> I N Z C I D V
// - - - 0 - - // - - - 0 - -
func execCLI(a *Appleone, o operation) error { func execCLI(vm *VM, o operation) error {
a.clearFlag(flagDisableInterrupts) vm.clearFlag(flagDisableInterrupts)
return nil return nil
} }
// 0 -> V N Z C I D V // 0 -> V N Z C I D V
// - - - - - 0 // - - - - - 0
func execCLV(a *Appleone, o operation) error { func execCLV(vm *VM, o operation) error {
a.clearFlag(flagOverflow) vm.clearFlag(flagOverflow)
return nil return nil
} }
// 1 -> C N Z C I D V // 1 -> C N Z C I D V
// - - 1 - - - // - - 1 - - -
func execSEC(a *Appleone, o operation) error { func execSEC(vm *VM, o operation) error {
a.setFlag(flagCarry) vm.setFlag(flagCarry)
return nil return nil
} }
// 1 -> D N Z C I D V // 1 -> D N Z C I D V
// - - - - 1 - // - - - - 1 -
func execSED(a *Appleone, o operation) error { func execSED(vm *VM, o operation) error {
a.setFlag(flagDecimalMode) vm.setFlag(flagDecimalMode)
return nil return nil
} }
// 1 -> I N Z C I D V // 1 -> I N Z C I D V
// - - - 1 - - // - - - 1 - -
func execSEI(a *Appleone, o operation) error { func execSEI(vm *VM, o operation) error {
a.setFlag(flagDisableInterrupts) vm.setFlag(flagDisableInterrupts)
return nil return nil
} }
// --- N Z C I D V // --- N Z C I D V
// - - - - - - // - - - - - -
func execNOP(a *Appleone, o operation) error { func execNOP(vm *VM, o operation) error {
return nil return nil
} }
// (PC+1) -> PCL N Z C I D V // (PC+1) -> PCL N Z C I D V
// (PC+2) -> PCH - - - - - - // (PC+2) -> PCH - - - - - -
func execJMP(a *Appleone, o operation) error { func execJMP(vm *VM, o operation) error {
if o.addrMode == indirect { if o.addrMode == indirect {
addr, err := a.getAddr(o) addr, err := vm.getAddr(o)
if err != nil { if err != nil {
return err return err
} }
a.cpu.pc = a.littleEndianToUint16(a.mem[addr+1], a.mem[addr]) vm.cpu.pc = vm.littleEndianToUint16(vm.mem[addr+1], vm.mem[addr])
return nil return nil
} }
addr, err := a.getAddr(o) addr, err := vm.getAddr(o)
if err != nil { if err != nil {
return err return err
} }
a.cpu.pc = addr vm.cpu.pc = addr
return nil return nil
} }
// push A N Z C I D V // push A N Z C I D V
// - - - - - - // - - - - - -
func execPHA(a *Appleone, o operation) error { func execPHA(vm *VM, o operation) error {
a.pushWordToStack(a.cpu.a) vm.pushWordToStack(vm.cpu.a)
return nil return nil
} }
// X -> A N Z C I D V // X -> A N Z C I D V
// + + - - - - // + + - - - -
func execTXA(a *Appleone, o operation) error { func execTXA(vm *VM, o operation) error {
a.cpu.a = a.cpu.x vm.cpu.a = vm.cpu.x
a.maybeSetFlagOverflow(a.cpu.a) vm.maybeSetFlagOverflow(vm.cpu.a)
a.maybeSetFlagZero(a.cpu.a) vm.maybeSetFlagZero(vm.cpu.a)
return nil return nil
} }
// Y -> A N Z C I D V // Y -> A N Z C I D V
// + + - - - - // + + - - - -
func execTYA(a *Appleone, o operation) error { func execTYA(vm *VM, o operation) error {
a.cpu.a = a.cpu.y vm.cpu.a = vm.cpu.y
a.maybeSetFlagOverflow(a.cpu.a) vm.maybeSetFlagOverflow(vm.cpu.a)
a.maybeSetFlagZero(a.cpu.a) vm.maybeSetFlagZero(vm.cpu.a)
return nil return nil
} }
// SP -> X N Z C I D V // SP -> X N Z C I D V
// + + - - - - // + + - - - -
func execTSX(a *Appleone, o operation) error { func execTSX(vm *VM, o operation) error {
a.cpu.x = a.cpu.sp vm.cpu.x = vm.cpu.sp
a.maybeSetFlagOverflow(a.cpu.x) vm.maybeSetFlagOverflow(vm.cpu.x)
a.maybeSetFlagZero(a.cpu.x) vm.maybeSetFlagZero(vm.cpu.x)
return nil return nil
} }
// pull A N Z C I D V // pull A N Z C I D V
// + + - - - - // + + - - - -
func execPLA(a *Appleone, o operation) error { func execPLA(vm *VM, o operation) error {
a.cpu.a = a.popStackWord() vm.cpu.a = vm.popStackWord()
a.maybeSetFlagOverflow(a.cpu.a) vm.maybeSetFlagOverflow(vm.cpu.a)
a.maybeSetFlagZero(a.cpu.a) vm.maybeSetFlagZero(vm.cpu.a)
return nil return nil
} }
// pull SR from stack N Z C I D V // pull SR from stack N Z C I D V
func execPLP(a *Appleone, o operation) error { func execPLP(vm *VM, o operation) error {
a.cpu.ps = a.popStackWord() | 0B_00110000 vm.cpu.ps = vm.popStackWord() | 0B_00110000
a.maybeSetFlagOverflow(a.cpu.a) vm.maybeSetFlagOverflow(vm.cpu.a)
a.maybeSetFlagZero(a.cpu.a) vm.maybeSetFlagZero(vm.cpu.a)
return nil return nil
} }
// push SR N Z C I D V // push SR N Z C I D V
// - - - - - - // - - - - - -
func execPHP(a *Appleone, o operation) error { func execPHP(vm *VM, o operation) error {
a.pushWordToStack(a.cpu.ps) vm.pushWordToStack(vm.cpu.ps)
return nil return nil
} }
// push (PC+2), N Z C I D V // push (PC+2), N Z C I D V
// (PC+1) -> PCL - - - - - - // (PC+1) -> PCL - - - - - -
// (PC+2) -> PCH // (PC+2) -> PCH
func execJSR(a *Appleone, o operation) error { func execJSR(vm *VM, o operation) error {
a.pushDWordToStack(a.cpu.pc - 1) vm.pushDWordToStack(vm.cpu.pc - 1)
addr, err := a.getAddr(o) addr, err := vm.getAddr(o)
if err != nil { if err != nil {
return err return err
} }
a.cpu.pc = addr vm.cpu.pc = addr
return nil return nil
} }
// pull PC, PC+1 -> PC N Z C I D V // pull PC, PC+1 -> PC N Z C I D V
// - - - - - - // - - - - - -
func execRTS(a *Appleone, o operation) error { func execRTS(vm *VM, o operation) error {
a.cpu.pc = a.popStackDWord() + 1 vm.cpu.pc = vm.popStackDWord() + 1
return nil return nil
} }
// 0 -> [76543210] -> C N Z C I D V // 0 -> [76543210] -> C N Z C I D V
// 0 + + - - - // 0 + + - - -
func execLSR(a *Appleone, o operation) error { func execLSR(vm *VM, o operation) error {
operand, err := a.getOperand(o) operand, err := vm.getOperand(o)
if err != nil { if err != nil {
return err return err
} }
@ -542,20 +542,20 @@ func execLSR(a *Appleone, o operation) error {
operand >>= 1 operand >>= 1
if bit { if bit {
a.setFlag(flagCarry) vm.setFlag(flagCarry)
} else { } else {
a.clearFlag(flagCarry) vm.clearFlag(flagCarry)
} }
a.maybeSetFlagZero(operand) vm.maybeSetFlagZero(operand)
a.maybeSetFlagOverflow(operand) vm.maybeSetFlagOverflow(operand)
if o.addrMode == accumulator { if o.addrMode == accumulator {
a.cpu.a = operand vm.cpu.a = operand
return nil return nil
} }
if err := a.setMem(o, operand); err != nil { if err := vm.setMem(o, operand); err != nil {
return err return err
} }
@ -564,21 +564,21 @@ func execLSR(a *Appleone, o operation) error {
// C <- [76543210] <- C N Z C I D V // C <- [76543210] <- C N Z C I D V
// + + + - - - // + + + - - -
func execROL(a *Appleone, o operation) error { func execROL(vm *VM, o operation) error {
operand, err := a.getOperand(o) operand, err := vm.getOperand(o)
if err != nil { if err != nil {
return err return err
} }
var carry bool var carry bool
if a.getFlag(flagCarry) == flagCarry { if vm.getFlag(flagCarry) == flagCarry {
carry = true carry = true
} }
if operand>>7 != 0 { if operand>>7 != 0 {
a.setFlag(flagCarry) vm.setFlag(flagCarry)
} else { } else {
a.clearFlag(flagCarry) vm.clearFlag(flagCarry)
} }
operand <<= 1 operand <<= 1
@ -587,15 +587,15 @@ func execROL(a *Appleone, o operation) error {
operand |= flagCarry operand |= flagCarry
} }
a.maybeSetFlagZero(operand) vm.maybeSetFlagZero(operand)
a.maybeSetFlagOverflow(operand) vm.maybeSetFlagOverflow(operand)
if o.addrMode == accumulator { if o.addrMode == accumulator {
a.cpu.a = operand vm.cpu.a = operand
return nil return nil
} }
if err := a.setMem(o, operand); err != nil { if err := vm.setMem(o, operand); err != nil {
return err return err
} }
@ -604,31 +604,31 @@ func execROL(a *Appleone, o operation) error {
// X -> SP N Z C I D V // X -> SP N Z C I D V
// - - - - - - // - - - - - -
func execTXS(a *Appleone, o operation) error { func execTXS(vm *VM, o operation) error {
a.cpu.sp = a.cpu.x vm.cpu.sp = vm.cpu.x
// TODO: needed? // TODO: needed?
// a.maybeSetFlagZero(a.cpu.sp) // vm.maybeSetFlagZero(vm.cpu.sp)
// a.maybeSetFlagOverflow(a.cpu.sp) // vm.maybeSetFlagOverflow(vm.cpu.sp)
return nil return nil
} }
// C -> [76543210] -> C N Z C I D V // C -> [76543210] -> C N Z C I D V
// + + + - - - // + + + - - -
func execROR(a *Appleone, o operation) error { func execROR(vm *VM, o operation) error {
operand, err := a.getOperand(o) operand, err := vm.getOperand(o)
if err != nil { if err != nil {
return err return err
} }
var carry bool var carry bool
if a.getFlag(flagCarry) == flagCarry { if vm.getFlag(flagCarry) == flagCarry {
carry = true carry = true
} }
if operand&0x01 != 0 { if operand&0x01 != 0 {
a.setFlag(flagCarry) vm.setFlag(flagCarry)
} else { } else {
a.clearFlag(flagCarry) vm.clearFlag(flagCarry)
} }
operand >>= 1 operand >>= 1
@ -637,15 +637,15 @@ func execROR(a *Appleone, o operation) error {
operand |= flagNegative operand |= flagNegative
} }
a.maybeSetFlagZero(operand) vm.maybeSetFlagZero(operand)
a.maybeSetFlagOverflow(operand) vm.maybeSetFlagOverflow(operand)
if o.addrMode == accumulator { if o.addrMode == accumulator {
a.cpu.a = operand vm.cpu.a = operand
return nil return nil
} }
if err := a.setMem(o, operand); err != nil { if err := vm.setMem(o, operand); err != nil {
return err return err
} }
@ -654,29 +654,29 @@ func execROR(a *Appleone, o operation) error {
// C <- [76543210] <- 0 N Z C I D V // C <- [76543210] <- 0 N Z C I D V
// + + + - - - // + + + - - -
func execASL(a *Appleone, o operation) error { func execASL(vm *VM, o operation) error {
operand, err := a.getOperand(o) operand, err := vm.getOperand(o)
if err != nil { if err != nil {
return err return err
} }
if operand>>7 == 1 { if operand>>7 == 1 {
a.setFlag(flagCarry) vm.setFlag(flagCarry)
} else { } else {
a.clearFlag(flagCarry) vm.clearFlag(flagCarry)
} }
operand <<= 1 operand <<= 1
a.maybeSetFlagZero(operand) vm.maybeSetFlagZero(operand)
a.maybeSetFlagOverflow(operand) vm.maybeSetFlagOverflow(operand)
if o.addrMode == accumulator { if o.addrMode == accumulator {
a.cpu.a = operand vm.cpu.a = operand
return nil return nil
} }
if err := a.setMem(o, operand); err != nil { if err := vm.setMem(o, operand); err != nil {
return err return err
} }
@ -685,26 +685,26 @@ func execASL(a *Appleone, o operation) error {
// A AND M -> A N Z C I D V // A AND M -> A N Z C I D V
// + + - - - - // + + - - - -
func execAND(a *Appleone, o operation) error { func execAND(vm *VM, o operation) error {
operand, err := a.getOperand(o) operand, err := vm.getOperand(o)
if err != nil { if err != nil {
return err return err
} }
a.cpu.a &= operand vm.cpu.a &= operand
a.maybeSetFlagZero(a.cpu.a) vm.maybeSetFlagZero(vm.cpu.a)
a.maybeSetFlagOverflow(a.cpu.a) vm.maybeSetFlagOverflow(vm.cpu.a)
return nil return nil
} }
// A OR M -> A N Z C I D V // A OR M -> A N Z C I D V
// + + - - - - // + + - - - -
func execORA(a *Appleone, o operation) error { func execORA(vm *VM, o operation) error {
operand, err := a.getOperand(o) operand, err := vm.getOperand(o)
if err != nil { if err != nil {
return err return err
} }
a.cpu.a |= operand vm.cpu.a |= operand
a.maybeSetFlagZero(a.cpu.a) vm.maybeSetFlagZero(vm.cpu.a)
a.maybeSetFlagOverflow(a.cpu.a) vm.maybeSetFlagOverflow(vm.cpu.a)
return nil return nil
} }

View File

@ -11,10 +11,10 @@ type operation struct {
opcode byte opcode byte
size byte size byte
addrMode addrMode addrMode addrMode
exec func(a *Appleone, op operation) error exec func(a *VM, op operation) error
} }
func newOp(name string, opcode, size byte, addrMode addrMode, exec func(a *Appleone, op operation) error) operation { func newOp(name string, opcode, size byte, addrMode addrMode, exec func(a *VM, op operation) error) operation {
return operation{ return operation{
name: name, name: name,
opcode: opcode, opcode: opcode,

View File

@ -3,42 +3,69 @@ package vm
import ( import (
"errors" "errors"
"fmt" "fmt"
"time"
) )
// Appleone represents the virtual Apple 1 computer const clockSpeed = 1 // 1 MHz
type Appleone struct {
cpu *Mos6502 // virtual mos6502 cpu // VM represents the Apple 1 virutal machine
mem block // available memory (64kiB) type VM struct {
cpu *Mos6502 // virtual mos6502 cpu
mem block // available memory (64kiB)
clock *time.Ticker // the "cpu" clock
ShutdownC chan struct{} //
} }
// New returns a pointer to an initialized Appleone with a brand spankin new CPU // New returns a pointer to an initialized VM with a brand spankin new CPU
func New() *Appleone { func New() *VM {
return &Appleone{ return &VM{
cpu: newCPU(), cpu: newCPU(),
mem: newBlock(), mem: newBlock(),
clock: time.NewTicker(time.Second / time.Duration(clockSpeed)),
} }
} }
// load puts the provided data into the apple1's memory block starting at the provided address // Run starts the vm and emulates a clock that runs by default at 60MHz
func (a *Appleone) load(addr uint16, data []byte) { // This can be changed with a flag.
a.mem.load(addr, data) func (vm *VM) Run() {
a.cpu.pc = addr for {
select {
case <-vm.clock.C:
vm.emulateCycle()
continue
case <-vm.ShutdownC:
break
}
break
}
vm.sigTerm("gracefully shutting down...")
} }
func (a *Appleone) step() { func (vm *VM) emulateCycle() {
operation, err := operationByCode(a.mem[a.cpu.pc]) operation, err := operationByCode(vm.mem[vm.cpu.pc])
if err != nil { if err != nil {
fmt.Println("TODO") fmt.Println("TODO")
} }
a.cpu.pc += uint16(operation.size) vm.cpu.pc += uint16(operation.size)
if err := operation.exec(a, operation); err != nil { if err := operation.exec(vm, operation); err != nil {
fmt.Println("TODO") fmt.Println("TODO")
} }
} }
func (a *Appleone) getAddr(o operation) (uint16, error) { func (vm *VM) sigTerm(msg string) {
fmt.Println(msg)
vm.ShutdownC <- struct{}{}
}
// load puts the provided data into the apple1's memory block starting at the provided address
func (vm *VM) load(addr uint16, data []byte) {
vm.mem.load(addr, data)
vm.cpu.pc = addr
}
func (vm *VM) getAddr(o operation) (uint16, error) {
switch o.addrMode { switch o.addrMode {
// TODO: will these ever apply here? // TODO: will these ever apply here?
// case accumulator: // case accumulator:
@ -46,153 +73,166 @@ func (a *Appleone) getAddr(o operation) (uint16, error) {
// case implied: // case implied:
// //
case absolute: case absolute:
return a.nextDWord(), nil return vm.nextDWord(), nil
case absoluteXIndexed: case absoluteXIndexed:
return a.nextDWord() + uint16(a.cpu.x), nil return vm.nextDWord() + uint16(vm.cpu.x), nil
case absoluteYIndexed: case absoluteYIndexed:
return a.nextDWord() + uint16(a.cpu.y), nil return vm.nextDWord() + uint16(vm.cpu.y), nil
case immediate: case immediate:
return a.cpu.pc - 1, nil return vm.cpu.pc - 1, nil
case indirect: case indirect:
return uint16(a.nextWord()), nil return uint16(vm.nextWord()), nil
case indirectXIndexed: case indirectXIndexed:
addr := (uint16(a.nextWord()) + uint16(a.cpu.x)) & 0xFF addr := (uint16(vm.nextWord()) + uint16(vm.cpu.x)) & 0xFF
return a.littleEndianToUint16(a.mem[addr+1], a.mem[addr]), nil return vm.littleEndianToUint16(vm.mem[addr+1], vm.mem[addr]), nil
case indirectYIndexed: case indirectYIndexed:
addr := uint16(a.nextWord()) addr := uint16(vm.nextWord())
val := a.littleEndianToUint16(a.mem[addr+1], a.mem[addr]) val := vm.littleEndianToUint16(vm.mem[addr+1], vm.mem[addr])
return val + uint16(a.cpu.y), nil return val + uint16(vm.cpu.y), nil
case relative: case relative:
return a.cpu.pc - 1, nil return vm.cpu.pc - 1, nil
case zeroPage: case zeroPage:
return uint16(a.nextWord()) & 0xFF, nil return uint16(vm.nextWord()) & 0xFF, nil
case zeroPageXIndexed: case zeroPageXIndexed:
return (uint16(a.nextWord()) + uint16(a.cpu.x)) & 0xFF, nil return (uint16(vm.nextWord()) + uint16(vm.cpu.x)) & 0xFF, nil
case zeroPageYIndexed: case zeroPageYIndexed:
return (uint16(a.nextWord()) + uint16(a.cpu.y)) & 0xFF, nil return (uint16(vm.nextWord()) + uint16(vm.cpu.y)) & 0xFF, nil
default: default:
return 0, errors.New("unkown addressing mode") return 0, errors.New("unkown addressing mode")
} }
} }
func (a *Appleone) getOperand(o operation) (byte, error) { func (vm *VM) getOperand(o operation) (byte, error) {
if o.addrMode == accumulator { if o.addrMode == accumulator {
return a.cpu.a, nil return vm.cpu.a, nil
} }
b, err := a.getAddr(o) b, err := vm.getAddr(o)
if err != nil { if err != nil {
return 0, err return 0, err
} }
return a.mem[b], nil return vm.mem[b], nil
} }
func (a *Appleone) littleEndianToUint16(big, little byte) uint16 { func (vm *VM) littleEndianToUint16(big, little byte) uint16 {
return uint16(a.mem[big])<<8 | uint16(a.mem[little]) return uint16(vm.mem[big])<<8 | uint16(vm.mem[little])
} }
// pushWordToStack pushes the given word (byte) into memory and sets the new stack pointer // pushWordToStack pushes the given word (byte) into memory and sets the new stack pointer
func (a *Appleone) pushWordToStack(b byte) { func (vm *VM) pushWordToStack(b byte) {
a.mem[StackBottom+uint16(a.cpu.sp)] = b vm.mem[StackBottom+uint16(vm.cpu.sp)] = b
a.cpu.sp = byte((uint16(a.cpu.sp) - 1) & 0xFF) vm.cpu.sp = byte((uint16(vm.cpu.sp) - 1) & 0xFF)
} }
// pushWordToStack splits the high and low byte of the data passed in, and pushes them to the stack // pushWordToStack splits the high and low byte of the data passed in, and pushes them to the stack
func (a *Appleone) pushDWordToStack(data uint16) { func (vm *VM) pushDWordToStack(data uint16) {
h := byte((data >> 8) & 0xFF) h := byte((data >> 8) & 0xFF)
l := byte(data & 0xFF) l := byte(data & 0xFF)
a.pushWordToStack(h) vm.pushWordToStack(h)
a.pushWordToStack(l) vm.pushWordToStack(l)
} }
// popStackWord sets the new stack pointer and returns the appropriate byte in memory // popStackWord sets the new stack pointer and returns the appropriate byte in memory
func (a *Appleone) popStackWord() byte { func (vm *VM) popStackWord() byte {
a.cpu.sp = byte((uint16(a.cpu.sp) + 1) & 0xFF) vm.cpu.sp = byte((uint16(vm.cpu.sp) + 1) & 0xFF)
return a.mem[StackBottom+uint16(a.cpu.sp)] return vm.mem[StackBottom+uint16(vm.cpu.sp)]
} }
// popStackDWord pops two stack words (a double word - uint16) off the stack // popStackDWord pops two stack words (a double word - uint16) off the stack
func (a *Appleone) popStackDWord() uint16 { func (vm *VM) popStackDWord() uint16 {
l := a.popStackWord() l := vm.popStackWord()
h := a.popStackWord() h := vm.popStackWord()
return (uint16(h) << 8) | uint16(l) return (uint16(h) << 8) | uint16(l)
} }
// nextWord returns the next byte in memory // nextWord returns the next byte in memory
func (a *Appleone) nextWord() byte { func (vm *VM) nextWord() byte {
return a.mem[a.cpu.pc-1] return vm.mem[vm.cpu.pc-1]
} }
// nextDWord returns the next two bytes (double word) // nextDWord returns the next two bytes (double word)
func (a *Appleone) nextDWord() uint16 { func (vm *VM) nextDWord() uint16 {
return a.littleEndianToUint16(a.mem[a.cpu.pc-1], a.mem[a.cpu.pc-2]) return vm.littleEndianToUint16(vm.mem[vm.cpu.pc-1], vm.mem[vm.cpu.pc-2])
} }
// maybeSetFlagZero takes a single word (byte), clears flagZero, and sets flagZero if word is 0 // maybeSetFlagZero takes a single word (byte), clears flagZero, and sets flagZero if word is 0
func (a *Appleone) maybeSetFlagZero(word byte) { func (vm *VM) maybeSetFlagZero(word byte) {
a.clearFlag(flagZero) vm.clearFlag(flagZero)
if word == 0 { if word == 0 {
a.setFlag(flagZero) vm.setFlag(flagZero)
} }
} }
func (a *Appleone) getFlag(flag byte) byte { func (vm *VM) getFlag(flag byte) byte {
return a.cpu.ps & flag return vm.cpu.ps & flag
} }
func (a *Appleone) setFlag(flag byte) { func (vm *VM) setFlag(flag byte) {
a.cpu.ps |= flag vm.cpu.ps |= flag
} }
func (a *Appleone) clearFlag(flag byte) { func (vm *VM) clearFlag(flag byte) {
a.cpu.ps &^= flag vm.cpu.ps &^= flag
} }
func (a *Appleone) maybeSetFlagOverflow(word byte) { func (vm *VM) maybeSetFlagOverflow(word byte) {
a.clearFlag(flagNegative) vm.clearFlag(flagNegative)
if word > 127 { if word > 127 {
a.setFlag(flagNegative) vm.setFlag(flagNegative)
} }
} }
// Branch offsets are signed 8-bit values, -128 ... +127, negative offsets in two's // Branch offsets are signed 8-bit values, -128 ... +127, negative offsets in two's
// complement. Page transitions may occur and add an extra cycle to the exucution // complement. Page transitions may occur and add an extra cycle to the exucution
func (a *Appleone) branch(o operation) error { func (vm *VM) branch(o operation) error {
offset, err := a.getOperand(o) offset, err := vm.getOperand(o)
if err != nil { if err != nil {
return err return err
} }
if offset > 127 { if offset > 127 {
a.cpu.pc -= 256 - uint16(offset) vm.cpu.pc -= 256 - uint16(offset)
} else { } else {
a.cpu.pc += uint16(offset) vm.cpu.pc += uint16(offset)
} }
return nil return nil
} }
// compare clears zero, carry, and negative flags, compares the two bytes, and sets the // compare clears zero, carry, and negative flags, compares the two bytes, and sets the
// appropriate flags based on the comparison between the bytes. // appropriate flags based on the comparison between the bytes.
func (a *Appleone) compare(b1, b2 byte) { func (vm *VM) compare(b1, b2 byte) {
a.clearFlag(flagZero) vm.clearFlag(flagZero)
a.clearFlag(flagCarry) vm.clearFlag(flagCarry)
a.clearFlag(flagNegative) vm.clearFlag(flagNegative)
if b1 == b2 { if b1 == b2 {
a.setFlag(flagZero) vm.setFlag(flagZero)
a.setFlag(flagCarry) vm.setFlag(flagCarry)
} }
if b1 > b2 { if b1 > b2 {
a.setFlag(flagCarry) vm.setFlag(flagCarry)
} }
b := byte(uint16(b1) - uint16(b2)) b := byte(uint16(b1) - uint16(b2))
a.maybeSetFlagOverflow(b) vm.maybeSetFlagOverflow(b)
} }
func (a *Appleone) setMem(o operation, operand byte) error { func (vm *VM) setMem(o operation, operand byte) error {
addr, err := a.getAddr(o) addr, err := vm.getAddr(o)
if err != nil { if err != nil {
return err return err
} }
a.mem[addr] = operand vm.mem[addr] = operand
return nil return nil
} }
// func (vm *VM) execBRK(o operation) error {
// // set processer status flag to BRK
// vm.cpu.ps = flagBreak
// vm.pushDWordToStack(vm.cpu.pc + 1)
// vm.pushWordToStack(vm.cpu.ps)
// vm.setFlag(flagDisableInterrupts)
// vm.cpu.pc = uint16(vm.mem[0xFFFF])<<8 | uint16(vm.mem[0xFFFE])
// return nil
// }

View File

@ -1,3 +1,9 @@
package main package main
func main() {} import "github.com/bradford-hamilton/apple-1/internal/vm"
func main() {
vm := vm.New()
go vm.Run()
<-vm.ShutdownC
}