-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintel8080emulator.go
69 lines (57 loc) · 1.76 KB
/
intel8080emulator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
//go:generate mockgen -destination=alu/mocks/condition_flags_mock.go -package=alu github.com/cbush06/intel8080emulator/alu ConditionFlags
//go:generate mockgen -destination=alu/mocks/alu_mock.go -package=alu github.com/cbush06/intel8080emulator/alu ALU
import (
"fmt"
"github.com/cbush06/intel8080emulator/cpu"
"os"
)
// CPUInterface is a struct comprising multiple go channels to allow IPC between
// this CPU, the machine using it, and any peripherals.
type CPUInterface struct {
Interrupt <-chan uint8
PowerOff <-chan bool
DataBus chan uint8
Memory []uint8
cpu *cpu.CPU
}
// StartCPU begins the CPU fetch-execute cycle and loads the specified program.
func StartCPU(program []byte, memShift uint16) *CPUInterface {
var mainCpu = new(cpu.CPU)
mainCpu.Init()
cpuInt := &CPUInterface{
Interrupt: make(chan uint8),
PowerOff: make(chan bool),
DataBus: make(chan uint8),
Memory: mainCpu.Memory,
cpu: mainCpu,
}
// Set ProgramCounter to execution starting point
mainCpu.ProgramCounter = memShift
// Copy program into working memory
copy(mainCpu.Memory[memShift:], program)
return cpuInt
}
func (cpuInt *CPUInterface) TickCPU() {
// Check for PowerOff command
if powerOff := <-cpuInt.PowerOff; powerOff {
os.Exit(0)
}
// Read DataBus in
if data := <-cpuInt.DataBus; data > 0 {
cpuInt.cpu.DataBus.Write8(data)
}
// Check for Interrupt; if set, execute interrupt instruction cycle
select {
case interruptCommand := <-cpuInt.Interrupt:
fmt.Printf("INTERRUPT 0x%2X\n", interruptCommand)
cpuInt.cpu.DataBus.Write8(interruptCommand)
cpuInt.cpu.InterruptInstructionCycle()
default:
cpuInt.cpu.StandardInstructionCycle()
}
// Write DataBus out
var data uint8
cpuInt.cpu.DataBus.Read8(&data)
cpuInt.DataBus <- data
}