Skip to content

Instantly share code, notes, and snippets.

@dimiro1
Created November 14, 2015 02:38
Show Gist options
  • Save dimiro1/17c461991c7830fd39a3 to your computer and use it in GitHub Desktop.
Save dimiro1/17c461991c7830fd39a3 to your computer and use it in GitHub Desktop.
A Simple Z80 Machine
package main
import (
"fmt"
"github.com/remogatto/z80"
)
type Memory struct {
memory []byte
}
func NewMemory() *Memory {
return &Memory{
memory: []byte{
0x21, 0x0C, 0x00, // ld hl, 0008
0x06, 0x0F, // ld b,0f
0x7e, // ld a,(hl)
0x23, // inc hl
0xD3, 0x00, // out (00), a
0x10, 0xFA, // djnz
0x76, // halt
0x48, 0x65, 0x6C, 0x6C, 0x6F, // Message ASCII
0x20, 0x77, 0x6F, 0x72, 0x6C, //
0x64, 0x21, 0x20, 0x0D, 0x0A,
},
}
}
func (m *Memory) ReadByte(address uint16) byte {
return m.ReadByteInternal(address)
}
func (m *Memory) ReadByteInternal(address uint16) byte {
return m.memory[address]
}
func (m *Memory) WriteByte(address uint16, value byte) {
m.WriteByteInternal(address, value)
}
func (m *Memory) WriteByteInternal(address uint16, value byte) {
m.memory[address] = value
}
func (m *Memory) Read(address uint16) byte { return 0 }
func (m *Memory) Write(address uint16, value byte, protectROM bool) {}
func (m *Memory) ContendRead(address uint16, time int) {}
func (m *Memory) ContendReadNoMreq(address uint16, time int) {}
func (m *Memory) ContendReadNoMreq_loop(address uint16, time int, count uint) {}
func (m *Memory) ContendWriteNoMreq(address uint16, time int) {}
func (m *Memory) ContendWriteNoMreq_loop(address uint16, time int, count uint) {}
func (m *Memory) Data() []byte {
return m.memory
}
type Ports struct{}
func (p *Ports) ReadPort(address uint16) byte {
return p.ReadPortInternal(address, true)
}
func (p *Ports) ReadPortInternal(address uint16, contend bool) byte {
return 0
}
func (p *Ports) WritePort(address uint16, b byte) {
p.WritePortInternal(address, b, true)
}
func (p *Ports) WritePortInternal(address uint16, b byte, contend bool) {
fmt.Print(string(b))
}
func (p *Ports) ContendPortPreio(address uint16) {}
func (p *Ports) ContendPortPostio(address uint16) {}
func NewPorts() *Ports {
return &Ports{}
}
func main() {
z80 := z80.NewZ80(NewMemory(), NewPorts())
z80.Reset()
for !z80.Halted {
z80.DoOpcode()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment