simulator

package
v0.0.0-...-9c58deb Latest Latest
Warning

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

Go to latest
Published: Jan 2, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultMaxSteps = 100000
	DefaultHaultAt  = -1
)
View Source
const (
	// Data transfer
	// Transfer MQ to AC, MQ is multiply-quotient register
	OpcodeLoadMQ = 0b00001010
	// Transfer M(X) to MQ
	OpcodeLoadMToMQ = 0b00001001
	// Transfer AC to memory location x
	OpcodeStoreM = 0b00100001
	// Transfer M(X) to the AC, AC is accumulator register
	OpcodeLoadM = 0b00000001
	// Transfer -M(X) to AC
	OpcodeLoadNegativeM = 0b00000010
	// Transfer |M(X)| to AC
	OpcodeLoadAbsM = 0b00000011
	// Transfer -|M(X)| to AC
	OpcodeLoadNegativeAbsM = 0b00000100

	// Unconditional branch
	// Take next instructionfrom left half of M(X)
	OpcodeJumpMLeft = 0b00001101
	// Take next instructionfrom right half of M(X)
	OpcodeJumpMRight = 0b00001110

	// Conditional branch
	// If the number in the AC is nonnegative take the next instruction from left half of M(X)
	OpcodeConditionalJumpMLeft = 0b00001111
	// If the number in the AC is nonnegative take the next instruction from right half of M(X)
	OpcodeConditionalJumpMRight = 0b00010000

	// Arithmetic
	// Add M(X) to AC, put the result in AC
	OpcodeAddM = 0b00000101
	// Add |M(X)| to AC, put the result in AC
	OpcodeAddAbsM = 0b00000111
	// Substract M(X) from AC, put the resul in AC
	OpcodeSubM = 0b00000110
	// Substract |M(X)| from AC, put the resul in AC
	OpcodeSubAbsM = 0b00001000
	// Multiply M(X) by MQ, put most significant bits of result in AC, put least significant bits in MQ
	OpcodeMultiplyM = 0b00001011
	// Divide AC by M(X), put the quotient in MQ and the remainder in AC
	OpcodeDivideM = 0b00001100
	// Multiply AC by 2, that is, shift left one bit position
	OpcodeLSH = 0b00010100
	// Divide AC by 2, that is, shift right one position
	OpcodeRSH = 0b00010101

	// Addresss modify
	// Replace left address field at M(X)[8:19] by 12 rightmost bits of AC
	OpcodeStoreMLeftAddr = 0b00010010
	// Replace right address field at M(X)[28:39] by 12 rightmost bits of AC
	OpcodeStoreMRightAddr = 0b00010011
)

Variables

View Source
var (
	FlagIsNextInstructionInIBR  bool
	FlagLeftInsturctionRequired bool // 下一个指令应该执行左边的指令吗
)
View Source
var (
	// Accumulator register, 40 bit
	AC *Register
	// Multiply-quotient register, 40 bit
	MQ *Register
	// Memory buffer register, 40 bit
	MBR *Register
	// Memory address register, 12 bit
	MAR *AddressRegister
	// Program counter, 12 bit
	PC *AddressRegister
	// Instruction buffer register, 20bit
	IBR *InstructionBufferRegister
	// Instruction register, 8 bit
	IR *InstructionRegister
)

Functions

func ConvertInstructionAndAddrListToHexStrList

func ConvertInstructionAndAddrListToHexStrList(data []*InstructionAndAddr) []string

将程序转化为16进制的格式

func ConvertIntToTwoByte

func ConvertIntToTwoByte(addr int) (higherByte, lowerByte byte)

func ConvertTwoByteToInt

func ConvertTwoByteToInt(higherByte, lowerByte byte) int

func DirectWrite

func DirectWrite(addr int, w *Word)

直接写入内存,只应该用于测试或初始化

func Execute

func Execute(options ...SimulateOption) error

连续执行,直到达到最大执行次数或遇到报错

func ExecuteOneInstruction

func ExecuteOneInstruction() error

执行一次

func GetLeftInstructionFromMBR

func GetLeftInstructionFromMBR() (opcode byte, addr int)

func GetRightInstructionFromMBR

func GetRightInstructionFromMBR() (opcode byte, addr int)

func Init

func Init()

func InitInstructionSet

func InitInstructionSet()

func PrintStatus

func PrintStatus()

print 当前各个寄存器的数据,用于debug

func SPrintStatus

func SPrintStatus() string

返回各个寄存器的数据

func SetInstructions

func SetInstructions(data []*InstructionAndAddr, startAddr int)

将程序写入内存,用于初始化的环节。将指令按顺序从指定地址开始写入内存。

func TakeInstructionFromLeftM

func TakeInstructionFromLeftM()

TakeInstructionFromLeftM takes next instruction from left half of M(x)

func TakeInstructionFromRightM

func TakeInstructionFromRightM()

TakeInstructionFromRightM takes next instruction from right half of M(x)

func TakeNextInstruction

func TakeNextInstruction()

TakeNextInstruction take the next instruction into IR

Types

type AddressRegister

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

func NewAddressRegister

func NewAddressRegister() *AddressRegister

func (*AddressRegister) GetAddr

func (r *AddressRegister) GetAddr() int

func (*AddressRegister) Increase

func (r *AddressRegister) Increase()

func (*AddressRegister) SetAddr

func (r *AddressRegister) SetAddr(addr int)

type ExecuteParam

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

type InstructionAddAbsM

type InstructionAddAbsM struct{}

ADD |M(X)|

func (*InstructionAddAbsM) Run

func (instruction *InstructionAddAbsM) Run()

type InstructionAddM

type InstructionAddM struct{}

ADD M(X)

func (*InstructionAddM) Run

func (instruction *InstructionAddM) Run()

type InstructionAndAddr

type InstructionAndAddr struct {
	OpCode byte
	Addr   int
}

type InstructionBufferRegister

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

func NewInstructionBufferRegister

func NewInstructionBufferRegister() *InstructionBufferRegister

func (*InstructionBufferRegister) Clear

func (r *InstructionBufferRegister) Clear()

func (*InstructionBufferRegister) IsEmpty

func (r *InstructionBufferRegister) IsEmpty() bool

func (*InstructionBufferRegister) Read

func (r *InstructionBufferRegister) Read() (code byte, addr int)

func (*InstructionBufferRegister) Write

func (r *InstructionBufferRegister) Write(code byte, addr int)

type InstructionConditionalJumpMLeft

type InstructionConditionalJumpMLeft struct{}

JUMP +M(X,0:19)

func (*InstructionConditionalJumpMLeft) Run

func (instruction *InstructionConditionalJumpMLeft) Run()

type InstructionConditionalJumpMRight

type InstructionConditionalJumpMRight struct{}

JUMP +M(X,20:39)

func (*InstructionConditionalJumpMRight) Run

func (instruction *InstructionConditionalJumpMRight) Run()

type InstructionDivideM

type InstructionDivideM struct{}

DIV M(X)

func (*InstructionDivideM) Run

func (instruction *InstructionDivideM) Run()

type InstructionInterface

type InstructionInterface interface {
	Run()
}

type InstructionJumpMLeft

type InstructionJumpMLeft struct{}

JUMP M(X,0:19)

func (*InstructionJumpMLeft) Run

func (instruction *InstructionJumpMLeft) Run()

type InstructionJumpMRight

type InstructionJumpMRight struct{}

JUMP M(X,20:39)

func (*InstructionJumpMRight) Run

func (instruction *InstructionJumpMRight) Run()

type InstructionLSH

type InstructionLSH struct{}

LSH

func (*InstructionLSH) Run

func (instruction *InstructionLSH) Run()

type InstructionLoadAbsM

type InstructionLoadAbsM struct{}

func (*InstructionLoadAbsM) Run

func (instruction *InstructionLoadAbsM) Run()

type InstructionLoadM

type InstructionLoadM struct{}

func (*InstructionLoadM) Run

func (instruction *InstructionLoadM) Run()

type InstructionLoadMQ

type InstructionLoadMQ struct{}

LOAD MQ

func (*InstructionLoadMQ) Run

func (instruction *InstructionLoadMQ) Run()

type InstructionLoadMToMQ

type InstructionLoadMToMQ struct{}

LOAD MQ,M(X)

func (*InstructionLoadMToMQ) Run

func (instruction *InstructionLoadMToMQ) Run()

type InstructionLoadNegativeAbsM

type InstructionLoadNegativeAbsM struct{}

func (*InstructionLoadNegativeAbsM) Run

func (instruction *InstructionLoadNegativeAbsM) Run()

type InstructionLoadNegativeM

type InstructionLoadNegativeM struct{}

func (*InstructionLoadNegativeM) Run

func (instruction *InstructionLoadNegativeM) Run()

type InstructionMultiplyM

type InstructionMultiplyM struct{}

MUL M(X)

func (*InstructionMultiplyM) Run

func (instruction *InstructionMultiplyM) Run()

type InstructionRSH

type InstructionRSH struct{}

RSH

func (*InstructionRSH) Run

func (instruction *InstructionRSH) Run()

type InstructionRegister

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

func NewInstructionRegister

func NewInstructionRegister() *InstructionRegister

func (*InstructionRegister) Read

func (r *InstructionRegister) Read() byte

func (*InstructionRegister) Write

func (r *InstructionRegister) Write(code byte)

type InstructionSet

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

func (*InstructionSet) GetInstruction

func (is *InstructionSet) GetInstruction(code byte) (InstructionInterface, error)

type InstructionStoreM

type InstructionStoreM struct{}

func (*InstructionStoreM) Run

func (instruction *InstructionStoreM) Run()

type InstructionStoreMLeftAddr

type InstructionStoreMLeftAddr struct{}

STORE M(X,8:19)

func (*InstructionStoreMLeftAddr) Run

func (instruction *InstructionStoreMLeftAddr) Run()

type InstructionStoreMRightAddr

type InstructionStoreMRightAddr struct{}

STORE M(X,28:39)

func (*InstructionStoreMRightAddr) Run

func (instruction *InstructionStoreMRightAddr) Run()

type InstructionSubAbsM

type InstructionSubAbsM struct{}

SUB |M(X)|

func (*InstructionSubAbsM) Run

func (instruction *InstructionSubAbsM) Run()

type InstructionSubM

type InstructionSubM struct{}

SUB M(X)

func (*InstructionSubM) Run

func (instruction *InstructionSubM) Run()

type Memory

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

func (*Memory) Read

func (m *Memory) Read()

Read reads the memory to MBR

func (*Memory) Write

func (m *Memory) Write()

Write writes MBR to memory

func (*Memory) WriteLeftAddr

func (m *Memory) WriteLeftAddr()

Write writes MBR[28:39] to M(X)[8:19]

func (*Memory) WriteRightAddr

func (m *Memory) WriteRightAddr()

Write writes MBR[28:39] to M(X)[28:39]

type Register

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

func NewRegister

func NewRegister() *Register

func (*Register) Clear

func (r *Register) Clear()

func (*Register) GetWord

func (r *Register) GetWord() *Word

func (*Register) IsEmpty

func (r *Register) IsEmpty() bool

func (*Register) IsNegative

func (r *Register) IsNegative() bool

func (*Register) SetWord

func (r *Register) SetWord(w *Word)

type SimulateOption

type SimulateOption func(*ExecuteParam)

func WithHaultAt

func WithHaultAt(haultAt int) SimulateOption

程序执行到该地址就结束(该地址的指令不会被执行)

func WithMaxSteps

func WithMaxSteps(maxSteps int) SimulateOption

type Word

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

One word has 40 bit

func DirectRead

func DirectRead(addr int) *Word

直接读取内存,只应该用于测试或初始化

func NewWord

func NewWord() *Word

func NewWordFromData

func NewWordFromData(data []byte) *Word

func NewWordFromHexStr

func NewWordFromHexStr(data string) (*Word, error)

func NewWordFromInt64

func NewWordFromInt64(v int64) *Word

func (*Word) Abs

func (w *Word) Abs() *Word

func (*Word) Add

func (w *Word) Add(w2 *Word) *Word

func (*Word) Clear

func (r *Word) Clear()

func (*Word) DeepCopy

func (w *Word) DeepCopy() *Word

func (*Word) Div

func (w *Word) Div(w2 *Word) (quotient, remainder *Word)

func (*Word) IsEmpty

func (r *Word) IsEmpty() bool

func (*Word) IsNegative

func (r *Word) IsNegative() bool

func (*Word) IsValid

func (w *Word) IsValid() bool

func (*Word) LSH

func (w *Word) LSH() *Word

Left shift one bit

func (*Word) Mul

func (w *Word) Mul(w2 *Word) (higherWord, lowerWord *Word)

func (*Word) Opposite

func (w *Word) Opposite() *Word

func (*Word) RSH

func (w *Word) RSH() *Word

Right shift one bit

func (*Word) Sub

func (w *Word) Sub(w2 *Word) *Word

func (*Word) ToHexStr

func (w *Word) ToHexStr() string

Right shift one bit

func (*Word) ToInt64

func (w *Word) ToInt64() int64

问就是懒得实现各种运算了,毕竟这不是该仿真的重点

Jump to

Keyboard shortcuts

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