mix

package
v0.0.0-...-99f2a91 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2024 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package mix simulates the MIX 1009 computer as described in Donald Knuth's "The Art of Computer Programming" (third edition).

Index

Constants

View Source
const (
	// The I/O devices supported by the MIX 1009 computer.
	Tape0Unit = iota
	Tape1Unit
	Tape2Unit
	Tape3Unit
	Tape4Unit
	Tape5Unit
	Tape6Unit
	Tape7Unit
	Drum8Unit
	Drum9Unit
	Drum10Unit
	Drum11Unit
	Disc12Unit
	Disc13Unit
	Disc14Unit
	Disc15Unit
	CardReaderUnit
	CardPunchUnit
	PrinterUnit
	TeletypeUnit
	PaperTapeUnit
	DeviceCount
)
View Source
const (
	// The registers of the MIX 1009 CPU.
	A = iota
	I1
	I2
	I3
	I4
	I5
	I6
	X
	J

	// Less, Equal and Greater are the possible values taken by the
	// computer's comparison indicator.
	Less    = -1
	Equal   = 0
	Greater = 1

	// MemorySize is the number of memory cells in a regular MIX computer.
	MemorySize = 4000
)
View Source
const (
	// The opcodes for the MIX 1009 computer (see Table 1, Section 1.3.1)
	NOP = iota
	ADD // FADD
	SUB // FSUB
	MUL // FMUL
	DIV // FDIV
	NUM // CHAR, HLT, AND, OR, XOR, FLOT, FIX, INT
	SLA // SRA, SLAX, SRAX, SLC, SRC, SLB, SRB
	MOVE
	LDA
	LD1
	LD2
	LD3
	LD4
	LD5
	LD6
	LDX
	LDAN
	LD1N
	LD2N
	LD3N
	LD4N
	LD5N
	LD6N
	LDXN
	STA
	ST1
	ST2
	ST3
	ST4
	ST5
	ST6
	STX
	STJ
	STZ
	JBUS
	IOC
	IN
	OUT
	JRED
	JMP  // JSJ, JOV, JNOV, JL, JE, JG, JGE, JNE, JLE
	JA   // JAN, JAZ, JAP, JANN, JANZ, JANP, JAE, JEO
	J1   // J1N, J1Z, J1P, J1NN, J1NZ, J1NP
	J2   // J2N, J2Z, J2P, J2NN, J2NZ, J2NP
	J3   // J3N, J3Z, J3P, J3NN, J3NZ, J3NP
	J4   // J4N, J4Z, J4P, J4NN, J4NZ, J4NP
	J5   // J5N, J5Z, J5P, J5NN, J5NZ, J5NP
	J6   // J6N, J6Z, J6P, J6NN, J6NZ, J6NP
	JX   // JXN, JXZ, JXP, JXNN, JXNZ, JXNP, JXE, JXO
	INCA // DECA, ENTA, ENNA
	INC1 // DEC1, ENT1, ENN1
	INC2 // DEC2, ENT2, ENN2
	INC3 // DEC3, ENT3, ENN3
	INC4 // DEC4, ENT4, ENN4
	INC5 // DEC5, ENT5, ENN5
	INC6 // DEC6, ENT6, ENN6
	INCX // DECX, ENTX, ENNX
	CMPA // FCMP
	CMP1
	CMP2
	CMP3
	CMP4
	CMP5
	CMP6
	CMPX
)
View Source
const (
	// The largest and smallest values that are representable
	// by a MIX word.
	MaxWord = 1<<30 - 1
	MinWord = -MaxWord

	// The largest and smallest values that are representable
	// by the MIX index registers and the jump register.
	MaxIndex = 1<<12 - 1
	MinIndex = -MaxIndex
)

Variables

View Source
var (
	DefaultBinding = &defBind
	ErrInvalidUnit = errors.New("mix: invalid I/O unit")
	ErrNoDevice    = errors.New("mix: no I/O device")
)
View Source
var (
	ErrInvalidAddress    = errors.New("mix: invalid address")
	ErrInvalidIndex      = errors.New("mix: invalid index")
	ErrInvalidOp         = errors.New("mix: invalid operation")
	ErrContentsInterlock = errors.New("mix: contents interlock")
)
View Source
var (
	ErrHalted       = errors.New("mix: halted")
	ErrNoInterrupts = errors.New("mix: interrupts are disabled")
)
View Source
var ErrFormat = errors.New("mix: format error")
View Source
var ErrInvalidBlock = errors.New("mix: invalid block")
View Source
var ErrInvalidChar = errors.New("mix: invalid character")
View Source
var ErrInvalidCommand = errors.New("mix: invalid I/O command")

ErrInvalidCommand is returned when an I/O device is asked to do something it does not support, eg. requesting input from the printer.

View Source
var ErrInvalidSpec = errors.New("mix: invalid field specification")
View Source
var ErrNotImplemented = errors.New("mix: not implemented")

Functions

func CompareFloatWord

func CompareFloatWord(u, v Word) int

func ConvertToUTF8

func ConvertToUTF8(w []Word) string

func Disassemble

func Disassemble(w Word) string

func IsChar

func IsChar(r rune) bool

func IsDigit

func IsDigit(r rune) bool

func IsLetter

func IsLetter(r rune) bool

func IsPunchable

func IsPunchable(s string) (rune, bool)

func IsSpace

func IsSpace(r rune) bool

func OverPunch

func OverPunch(digit rune) rune

func RotateBitsLeft

func RotateBitsLeft(high, low *Word, count int)

RotateBitsLeft rotates a double MIX word left by the given number of bits.

func RotateBitsRight

func RotateBitsRight(high, low *Word, count int)

RotateBitsRight rotates a double MIX word right by the given number of bits.

func ShiftBitsLeft

func ShiftBitsLeft(high, low *Word, count int)

ShiftBitsLeft shifts a double MIX word left by the given number of bits.

func ShiftBitsRight

func ShiftBitsRight(high, low *Word, count int)

ShiftBitsRight shifts a double MIX word right by the given number of bits.

func Spec

func Spec(left, right int) int

Spec returns the integer equivalent of a MIX field specification.

Types

type Binding

type Binding [DeviceCount]interface{}

type CardPunch

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

func (*CardPunch) BlockSize

func (*CardPunch) BlockSize() int

func (*CardPunch) Close

func (p *CardPunch) Close() error

func (*CardPunch) Control

func (p *CardPunch) Control(m int) (int64, error)

func (*CardPunch) Name

func (*CardPunch) Name() string

func (*CardPunch) Read

func (*CardPunch) Read([]Word) (int64, error)

func (*CardPunch) Write

func (p *CardPunch) Write(block []Word) (int64, error)

type CardReader

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

func (*CardReader) BlockSize

func (*CardReader) BlockSize() int

func (*CardReader) Close

func (r *CardReader) Close() error

func (*CardReader) Control

func (r *CardReader) Control(m int) (int64, error)

func (*CardReader) Name

func (*CardReader) Name() string

func (*CardReader) Read

func (r *CardReader) Read(block []Word) (int64, error)

func (*CardReader) Write

func (*CardReader) Write([]Word) (int64, error)

type Computer

type Computer struct {
	Overflow      bool
	Comparison    int
	Elapsed, Idle int64
	Reg           [10]Word
	Contents      []Word

	Devices []Peripheral

	Tracer  io.WriteCloser
	Trigger int

	Interrupts bool

	BootFrom int
	// contains filtered or unexported fields
}

func Load

func Load(filename string) (*Computer, error)

func NewComputer

func NewComputer() *Computer

func (*Computer) Bind

func (c *Computer) Bind(b *Binding) error

func (*Computer) Cycle

func (c *Computer) Cycle() (err error)

func (*Computer) GoButton

func (c *Computer) GoButton() error

GoButton starts the MIX computer, as described in Ex. 26, Section 1.3.1. The machine can be bootstrapped only from the card reader or paper tape.

func (*Computer) Save

func (c *Computer) Save(filename string) error

func (*Computer) Shutdown

func (c *Computer) Shutdown() error

type Disc

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

func NewDisc

func NewDisc(rwsc readWriteSeekCloser, unit int, c *Computer) (*Disc, error)

see Section 5.4.9

func (*Disc) BlockSize

func (*Disc) BlockSize() int

func (*Disc) Close

func (d *Disc) Close() error

func (*Disc) Control

func (d *Disc) Control(m int) (int64, error)

func (*Disc) Name

func (d *Disc) Name() string

func (*Disc) Read

func (d *Disc) Read(block []Word) (int64, error)

func (*Disc) Write

func (d *Disc) Write(block []Word) (int64, error)

type Drum

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

func NewDrum

func NewDrum(rwsc readWriteSeekCloser, unit int, c *Computer) (*Drum, error)

see Section 5.4.9

func (*Drum) BlockSize

func (*Drum) BlockSize() int

func (*Drum) Close

func (d *Drum) Close() error

func (*Drum) Control

func (d *Drum) Control(m int) (int64, error)

func (*Drum) Name

func (d *Drum) Name() string

func (*Drum) Read

func (d *Drum) Read(block []Word) (int64, error)

func (*Drum) Write

func (d *Drum) Write(block []Word) (int64, error)

type PaperTape

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

func NewPaperTape

func NewPaperTape(rwsc readWriteSeekCloser) (*PaperTape, error)

see https://en.wikipedia.org/wiki/Teletype_Model_33

func (*PaperTape) BlockSize

func (*PaperTape) BlockSize() int

func (*PaperTape) Close

func (p *PaperTape) Close() error

func (*PaperTape) Control

func (p *PaperTape) Control(m int) (int64, error)

func (*PaperTape) Name

func (*PaperTape) Name() string

func (*PaperTape) Read

func (p *PaperTape) Read(block []Word) (int64, error)

func (*PaperTape) Write

func (p *PaperTape) Write(block []Word) (int64, error)

type Peripheral

type Peripheral interface {
	Name() string
	BlockSize() int
	Read(block []Word) (duration int64, err error)
	Write(block []Word) (duration int64, err error)
	Control(m int) (duration int64, err error)
	Close() error
}

type Printer

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

func (*Printer) BlockSize

func (*Printer) BlockSize() int

func (*Printer) Close

func (p *Printer) Close() error

func (*Printer) Control

func (p *Printer) Control(m int) (int64, error)

func (*Printer) Name

func (*Printer) Name() string

func (*Printer) Read

func (*Printer) Read([]Word) (int64, error)

func (*Printer) Write

func (p *Printer) Write(block []Word) (int64, error)

type Tape

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

func NewTape

func NewTape(rwsc readWriteSeekCloser, unit int) (*Tape, error)

see Section 5.4.6

func (*Tape) BlockSize

func (*Tape) BlockSize() int

func (*Tape) Close

func (t *Tape) Close() error

func (*Tape) Control

func (t *Tape) Control(m int) (int64, error)

func (*Tape) Name

func (t *Tape) Name() string

func (*Tape) Read

func (t *Tape) Read(block []Word) (int64, error)

func (*Tape) Write

func (t *Tape) Write(block []Word) (int64, error)

type Teletype

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

func (*Teletype) BlockSize

func (*Teletype) BlockSize() int

func (*Teletype) Close

func (t *Teletype) Close() error

func (*Teletype) Control

func (t *Teletype) Control(m int) (int64, error)

func (*Teletype) Name

func (*Teletype) Name() string

func (*Teletype) Read

func (t *Teletype) Read(block []Word) (int64, error)

func (*Teletype) Write

func (t *Teletype) Write(block []Word) (int64, error)

type Word

type Word int32

Word represents a MIX machine word, which consists of five 6-bit bytes and a sign bit.

func AddFloatWord

func AddFloatWord(u, v Word) (result Word, overflow bool)

AddFloatWord returns the sum of two floating point MIX words, and whether overflow occurred. See Section 4.2.1.

func AddWord

func AddWord(w Word, v int) (result Word, overflow bool)

AddWord adds an integer to a MIX word, returning the result as a MIX word, and whether overflow occurred. See Section 1.3.1.

func AndWord

func AndWord(w Word, v int) Word

AndWord returns the bitwise-AND of a MIX word and an integer (the sign of the MIX word is preserved)

func ConvertToMIX

func ConvertToMIX(s string) ([]Word, error)

func DivFloatWord

func DivFloatWord(u, v Word) (result Word, overflow bool)

func DivWord

func DivWord(high, low Word, v int) (quot, rem Word, overflow bool)

DivWord divides a double-precision MIX word by an integer, returning the quotient and remainder as MIX words, and whether overflow or division by zero occurred. See Section 1.3.1.

func FixedToFloat

func FixedToFloat(w Word) (result Word, overflow bool)

func FloatToFixed

func FloatToFixed(w Word) (result Word, overflow bool)

func MulFloatWord

func MulFloatWord(u, v Word) (result Word, overflow bool)

func MulWord

func MulWord(w Word, v int) (high, low Word)

MulWord multiplies a MIX word by an integer, returning the product as two (double-precision) MIX words. See Section 1.3.1.

func NewWord

func NewWord(val int) Word

NewWord returns a MIX word with the given integer value. It panics if the value is out of range.

func OrWord

func OrWord(w Word, v int) Word

OrWord returns the bitwise-OR of a MIX word and an integer (the sign of the MIX word is preserved)

func SubFloatWord

func SubFloatWord(u, v Word) (result Word, overflow bool)

func SubWord

func SubWord(w Word, v int) (result Word, overflow bool)

SubWord subtracts an integer from a MIX word, returning the result as a MIX word, and whether overflow occurred. See Section 1.3.1.

func XorWord

func XorWord(w Word, v int) Word

XorWord returns the bitwise-XOR of a MIX word and an integer (the sign of the MIX word is preserved)

func (Word) Field

func (w Word) Field(f int) Word

Field returns the value of MIX word w's field f, as a MIX word.

func (Word) GoString

func (w Word) GoString() string

GoString returns a representation of a MIX word as an unsigned integer.

func (Word) Int

func (w Word) Int() int

Int returns the integer value of the given MIX word.

func (Word) Negate

func (w Word) Negate() Word

Negate returns the value of a MIX word with its sign inverted.

func (*Word) PackFloat

func (w *Word) PackFloat(e, f int)

PackFloat composes a MIX word from a floating point number's exponent and fraction.

func (*Word) PackOp

func (w *Word) PackOp(aa Word, i, f, c int)

PackOp composes a MIX word from a MIX instruction's address, index, field and opcode.

func (*Word) SetField

func (w *Word) SetField(f int, val Word)

SetField changes the field f of the MIX word w to the given value.

func (Word) Sign

func (w Word) Sign() int

Sign returns the sign of a MIX word, +1 for positive, -1 for negative.

func (Word) String

func (w Word) String() string

String returns a representation of the value of a MIX word as a decimal number. Note that a MIX word can have a value of negative zero.

func (Word) UnpackFloat

func (w Word) UnpackFloat() (e, f int)

UnpackFloat extracts the exponent and fraction of a floating point number in a MIX word.

func (Word) UnpackOp

func (w Word) UnpackOp() (aa Word, i, f, c int)

UnpackOp extracts a MIX instruction's address, index, field and opcode from a MIX word.

Jump to

Keyboard shortcuts

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