machine

package
v0.0.0-...-85084b6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 15, 2022 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SdlDeviceId  = 0x0200
	SdlInit      = 1
	SdlPoll      = 2
	SdlPresent   = 3
	SdlClear     = 4
	SdlSetColor  = 5
	SdlDrawLine  = 6
	SdlDrawRect  = 7
	SdlFillRect  = 8
	SdlTicks     = 9
	SdlInitAudio = 0x0a
	SdlLoadWav   = 0x0b
	SdlPlayWav   = 0x0c
)
View Source
const (
	ErrNoErr uint16 = iota
	ErrInvalidHandler
	ErrIOError
)
View Source
const (
	PCAddr     = 0  // Program counter
	SPAddr     = 2  // Stack pointer, points to last byte written
	FPAddr     = 4  // Frame pointer
	IOReqAddr  = 6  // Address of I/O commands are written here to execute
	IOStatAddr = 8  // I/O status of last command, 0 = success, != 0 error
	RandAddr   = 10 // Writes are ignored, reads return random uint8/uint16
)
View Source
const (
	StdoutDeviceId     = 0x0100
	StdoutCommandWrite = 1
)
View Source
const BaseDirEnv = "MPU_BASE_DIR"

BaseDirEnv is the key for an environment variable to use for loading relative files.

Variables

This section is empty.

Functions

func DecodeOp

func DecodeOp(in byte) (OpCode, AddressMode, AddressMode)

func EncodeOp

func EncodeOp(op OpCode, m1, m2 AddressMode) byte

func LogIOError

func LogIOError(format string, a ...interface{})

func RegisterSDLHandlers

func RegisterSDLHandlers(m *IODispatcher)

Types

type AddressMode

type AddressMode byte
const (
	Implied AddressMode = iota
	Absolute
	Immediate
	ImmediateByte
	OffsetByte
	Indirect
	Relative
	RelativeIndirect
)

func (AddressMode) String

func (m AddressMode) String() string

type ByteSliceMemory

type ByteSliceMemory struct {
	// contains filtered or unexported fields
}

ByteSliceMemory contains a set of (continuous) memory-mapped regions and a raw byte slice. The memory mapped area must be contiguous, and start at offset zero, and must consist of all word-sized memory.

func NewByteSliceMemory

func NewByteSliceMemory(registers []Memory, raw []byte) *ByteSliceMemory

func (*ByteSliceMemory) BytesReaderAt

func (m *ByteSliceMemory) BytesReaderAt(addr uint16) *bytes.Reader

func (*ByteSliceMemory) GetByte

func (m *ByteSliceMemory) GetByte(addr uint16) byte

func (*ByteSliceMemory) GetWord

func (m *ByteSliceMemory) GetWord(addr uint16) uint16

func (*ByteSliceMemory) PutByte

func (m *ByteSliceMemory) PutByte(addr uint16, b byte)

func (*ByteSliceMemory) PutWord

func (m *ByteSliceMemory) PutWord(addr uint16, w uint16)

func (*ByteSliceMemory) ReadZString

func (m *ByteSliceMemory) ReadZString(addr uint16) string

type Encoding

type Encoding struct {
	// contains filtered or unexported fields
}

Encoding defines the opcode and two address modes for each instruction. TODO It might be easier to both define and lookup at runtime by using bitfields want an int, rather than fields want a struct. If each operand mode is a flag then the combos can be too ... AbsAbs. Add | AbsAbs, Sub | AbsAbs, Mul | AbsAbs, ... may be worth a microbenchmark to see the runtime performance variations.

type Flags

type Flags struct {
	PC       uint16
	SP       uint16
	FP       uint16
	Negative bool
	Zero     bool
	Carry    bool
	Bytes    bool
}

type IODispatcher

type IODispatcher struct {
	// contains filtered or unexported fields
}

IODispatcher implements Memory, to map to an address, and provides another Memory object to view the status of the last IO request.

func NewDefaultDispatcher

func NewDefaultDispatcher() *IODispatcher

func NewDispatcher

func NewDispatcher() *IODispatcher

func (*IODispatcher) BytesReaderAt

func (d *IODispatcher) BytesReaderAt(addr uint16) *bytes.Reader

func (*IODispatcher) GetByte

func (d *IODispatcher) GetByte(addr uint16) byte

func (*IODispatcher) GetWord

func (d *IODispatcher) GetWord(addr uint16) uint16

func (*IODispatcher) PutByte

func (d *IODispatcher) PutByte(addr uint16, b byte)

func (*IODispatcher) PutWord

func (d *IODispatcher) PutWord(addr uint16, w uint16)

func (*IODispatcher) ReadZString

func (d *IODispatcher) ReadZString(addr uint16) string

func (*IODispatcher) RegisterIOHandler

func (d *IODispatcher) RegisterIOHandler(id int, h IOHandler)

func (*IODispatcher) StatusRegister

func (d *IODispatcher) StatusRegister() Memory

type IOHandler

type IOHandler interface {
	Handle(m Memory, addr uint16) (errCode uint16)
}

IOHandler represents a single command handler within a device. Handlers must be registered via RegisterIOHandler. When invoked, machine will use encoding/binary to unmarshall the data pointed to into the handler and then call its Handle() method.

type Machine

type Machine struct {
	// contains filtered or unexported fields
}

Machine implements MPU ... memory processing unit. It supports 27 instructions and 6 addressing modes.

func NewMachine

func NewMachine(image []byte) *Machine

func NewMachineWithDevices

func NewMachineWithDevices(d *IODispatcher, image []byte) *Machine

func (*Machine) Flags

func (m *Machine) Flags() Flags

Flags returns a snapshot of the current state of the registers and flags.

func (*Machine) Memory

func (m *Machine) Memory() Memory

func (*Machine) ReadInt8

func (m *Machine) ReadInt8(addr uint16) int

ReadInt8 reads the given addr from memory as a byte and casts it to a signed int8 (as an int).

func (*Machine) Run

func (m *Machine) Run()

func (*Machine) RunAt

func (m *Machine) RunAt(pc uint16)

RunAt runs code from the given program counter until a HLT is encountered.

func (*Machine) Step

func (m *Machine) Step(addr uint16) uint16

Step executes a single instruction at the given address, and returns the new PC address.

type Memory

type Memory interface {
	PutByte(addr uint16, b byte)
	GetByte(addr uint16) byte
	PutWord(addr uint16, w uint16)
	GetWord(addr uint16) uint16
	ReadZString(addr uint16) string
	BytesReaderAt(addr uint16) *bytes.Reader
}

Memory defines the basic abstraction of reading/writing words and bytes to random access memory OR to memory-mapped registers.

type OpCode

type OpCode byte
const (
	Hlt OpCode = iota
	Add
	Sub
	Mul
	Div
	And
	Or
	Xor
	Cpy
	Cmp
	Inc
	Dec
	Psh
	Pop
	Jsr
	Jmp
	Jeq
	Jne
	Jge
	Jlt
	Jcc
	Jcs
	Sav
	Seb
	Clb
	Clc
	Sec
	Ret
	Rst
)

func (OpCode) String

func (o OpCode) String() string

type RNG

type RNG struct {
	// contains filtered or unexported fields
}

RNG exposes a random number generator as a Memory unit. Writes are ignored. Both bytes and words can be read.

func NewRNG

func NewRNG(seed int64) *RNG

func (*RNG) BytesReaderAt

func (r *RNG) BytesReaderAt(addr uint16) *bytes.Reader

func (*RNG) GetByte

func (r *RNG) GetByte(addr uint16) byte

func (*RNG) GetWord

func (r *RNG) GetWord(addr uint16) uint16

func (*RNG) PutByte

func (r *RNG) PutByte(addr uint16, b byte)

func (*RNG) PutWord

func (r *RNG) PutWord(addr uint16, w uint16)

func (*RNG) ReadZString

func (r *RNG) ReadZString(addr uint16) string

type Register

type Register struct {
	// contains filtered or unexported fields
}

Register defines uint16 which is mapped to memory and therefore supports the put/get byte/word primitives.

func (Register) BytesReaderAt

func (r Register) BytesReaderAt(addr uint16) *bytes.Reader

func (*Register) Get

func (r *Register) Get() uint16

func (*Register) GetByte

func (r *Register) GetByte(addr uint16) byte

func (*Register) GetWord

func (r *Register) GetWord(addr uint16) uint16

func (*Register) PutByte

func (r *Register) PutByte(addr uint16, b byte)

func (*Register) PutWord

func (r *Register) PutWord(addr uint16, w uint16)

func (Register) ReadZString

func (r Register) ReadZString(addr uint16) string

func (*Register) Set

func (r *Register) Set(w uint16)

type SdlClearHandler

type SdlClearHandler struct {
	Id uint16
}

func (*SdlClearHandler) Handle

func (c *SdlClearHandler) Handle(m Memory, addr uint16) uint16

type SdlDrawLineHandler

type SdlDrawLineHandler struct {
	Id             uint16
	X1, Y1, X2, Y2 uint16
}

func (SdlDrawLineHandler) Handle

func (c SdlDrawLineHandler) Handle(m Memory, addr uint16) uint16

type SdlDrawRectHandler

type SdlDrawRectHandler struct {
	Id         uint16
	X, Y, W, H uint16
}

func (*SdlDrawRectHandler) Handle

func (c *SdlDrawRectHandler) Handle(m Memory, addr uint16) uint16

type SdlFillRectHandler

type SdlFillRectHandler struct {
	Id         uint16
	X, Y, W, H uint16
}

func (SdlFillRectHandler) Handle

func (c SdlFillRectHandler) Handle(m Memory, addr uint16) uint16

type SdlInitAudioHandler

type SdlInitAudioHandler struct {
	Id uint16
}

func (*SdlInitAudioHandler) Handle

func (s *SdlInitAudioHandler) Handle(m Memory, addr uint16) (errCode uint16)

type SdlInitHandler

type SdlInitHandler struct {
	Id     uint16
	Width  uint16
	Height uint16
	Title  uint16 // Pointer to zstring
}

func (*SdlInitHandler) Handle

func (c *SdlInitHandler) Handle(m Memory, addr uint16) uint16

type SdlLoadWavHandler

type SdlLoadWavHandler struct {
	Id   uint16
	Path uint16
}

func (*SdlLoadWavHandler) Handle

func (s *SdlLoadWavHandler) Handle(m Memory, addr uint16) (errCode uint16)

type SdlPlayWavHandler

type SdlPlayWavHandler struct {
	Id   uint16
	Path uint16
}

func (*SdlPlayWavHandler) Handle

func (s *SdlPlayWavHandler) Handle(m Memory, addr uint16) (errCode uint16)

type SdlPollHandler

type SdlPollHandler struct {
	Id        uint16
	EventType uint16 // space for response
	Timestamp uint16 // space for response
}

func (*SdlPollHandler) Handle

func (c *SdlPollHandler) Handle(m Memory, addr uint16) uint16

type SdlPresentHandler

type SdlPresentHandler struct {
	Id      uint16
	DelayMS uint16
}

func (*SdlPresentHandler) Handle

func (c *SdlPresentHandler) Handle(m Memory, addr uint16) uint16

type SdlSetColorHandler

type SdlSetColorHandler struct {
	Id         uint16
	R, G, B, A uint8
}

func (SdlSetColorHandler) Handle

func (c SdlSetColorHandler) Handle(m Memory, addr uint16) uint16

type SdlTicksHandler

type SdlTicksHandler struct {
	Id    uint16
	Ticks uint16
}

func (*SdlTicksHandler) Handle

func (s *SdlTicksHandler) Handle(m Memory, addr uint16) (errCode uint16)

type StdoutWriteHandler

type StdoutWriteHandler struct {
	Id       uint16 // 0x0101
	PZString uint16 // pointer to zero-terminated string
}

func (*StdoutWriteHandler) Handle

func (s *StdoutWriteHandler) Handle(m Memory, addr uint16) (errCode uint16)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL