op

package
v0.0.0-...-ec3c368 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2023 License: Unlicense Imports: 1 Imported by: 0

Documentation

Overview

Package op provides access to the instruction set of a simple RISC architecture.

ref: Appendix C: Computer science - An overview, 11th ed.

Index

Constants

View Source
const (
	// InstSize specifies the size in bytes of an encoded instruction.
	InstSize = 2
	// RegCount specifies the number of general purpose registers on the system.
	RegCount = 16
	// RegSize specifies the bit size of the general purpose registers.
	RegSize = 8
)

Information about the architecture.

Variables

This section is empty.

Functions

func Decode

func Decode(buf uint16) (inst interface{}, err error)

Decode decodes the 16 bit representation of an instruction and returns it.

func Encode

func Encode(inst interface{}) (buf uint16, err error)

Encode encodes the instruction and returns a 16-bit representation of it.

Types

type Add

type Add struct {
	Code Code
	Dst  Reg
	Src1 Reg
	Src2 Reg
}

The Add instruction adds the contents of the src1 and src2 registers, as though they represented values in two's complement notation, and store the result in the dst register.

func (*Add) String

func (inst *Add) String() string

type AddFloat

type AddFloat struct {
	Code Code
	Dst  Reg
	Src1 Reg
	Src2 Reg
}

The AddFloat instruction adds the contents of the src1 and src2 registers, as though they represented values in floating-point notation, and store the result in the dst register.

func (*AddFloat) String

func (inst *AddFloat) String() string

type Addr

type Addr uint8

Addr represents a memory address between 0 and 255.

func (Addr) String

func (addr Addr) String() string

type And

type And struct {
	Code Code
	Dst  Reg
	Src1 Reg
	Src2 Reg
}

The And instruction performs a bitwise AND operation with the src1 and src2 registers and store the result in the dst register.

func (*And) String

func (inst *And) String() string

type CmpBranch

type CmpBranch struct {
	Code Code
	Cmp  Reg
	Addr Addr
}

The CmpBranch instruction jumps to the instruction located at the addr memory address if the contents of the cmp registers is equal to the contents of the 0 register. Otherwise, continue with the normal sequence of execution.

This jump is "unconditional" when cmp == 0.

func (*CmpBranch) String

func (inst *CmpBranch) String() string

type Code

type Code uint8

Code represents an op-code.

const (
	// CodeNop performs no operation.
	//
	//    op-code: 0
	//    operand: 000
	CodeNop Code = iota
	// CodeLoadMem loads the contents of the src memory address into the dst
	// register.
	//
	//    op-code: 1
	//    operand: RXY
	//       R refers to the dst register.
	//       XY refers to the src memory address.
	CodeLoadMem
	// CodeLoadVal loads the src immediate value into the dst register.
	//
	//    op-code: 2
	//    operand: RXY
	//       R refers to the dst register.
	//       XY refers to the src immediate value.
	CodeLoadVal
	// CodeStore stores the contents of the src register into the dst memory
	// address.
	//
	//    op-code: 3
	//    operand: RXY
	//       R refers to the src register.
	//       XY refers to the dst memory address.
	CodeStore
	// CodeMove moves the contents of the src register into the dst register.
	//
	//    op-code: 4
	//    operand: 0RS
	//       R refers to the src register.
	//       S refers to the dst register.
	CodeMove
	// CodeAdd adds the contents of the src1 and src2 registers, as though they
	// represented values in two's complement notation, and store the result in
	// the dst register.
	//
	//    op-code: 5
	//    operand: RST
	//       R refers to the dst register.
	//       S refers to the src1 register.
	//       T refers to the src2 register.
	CodeAdd
	// CodeAddFloat adds the contents of the src1 and src2 registers, as though
	// they represented values in floating-point notation, and store the result
	// in the dst register.
	//
	//    op-code: 6
	//    operand: RST
	//       R refers to the dst register.
	//       S refers to the src1 register.
	//       T refers to the src2 register.
	CodeAddFloat
	// CodeOr performs a bitwise OR operation between the bit patterns in the
	// src1 and src2 registers and store the result in the dst register.
	//
	//    op-code: 7
	//    operand: RST
	//       R refers to the dst register.
	//       S refers to the src1 register.
	//       T refers to the src2 register.
	CodeOr
	// CodeAnd performs a bitwise AND operation between the bit patterns in the
	// src1 and src2 registers and store the result in the dst register.
	//
	//    op-code: 8
	//    operand: RST
	//       R refers to the dst register.
	//       S refers to the src1 register.
	//       T refers to the src2 register.
	CodeAnd
	// CodeXor performs a bitwise XOR operation between the bit patterns in the
	// src1 and src2 registers and store the result in the dst register.
	//
	//    op-code: 9
	//    operand: RST
	//       R refers to the dst register.
	//       S refers to the src1 register.
	//       T refers to the src2 register.
	CodeXor
	// CodeRor rotates the bit pattern in the reg register x bits to the right.
	// Each time a bit is rotated out of the low-order end it is placed at the
	// high-order end.
	//
	//    op-code: A
	//    operand: R0X
	//       R refers to the register.
	//       X refers to the immediate value x.
	CodeRor
	// CodeCmpBranch jumps to the instruction located at the addr memory address
	// if the contents of the cmp registers is equal to the contents of the 0
	// register. Otherwise, continue with the normal sequence of execution.
	//
	// This jump is "unconditional" when cmp == 0.
	//
	//    op-code: B
	//    operand: RXY
	//       R refers to the cmp register.
	//       XY refers to the memory address addr.
	CodeCmpBranch
	// CodeHalt halts execution.
	//
	//    op-code: C
	//    operand: 000
	CodeHalt
)

Op-codes.

func (Code) String

func (code Code) String() string

type Halt

type Halt struct {
	Code Code
}

The Halt instruction halts execution.

func (*Halt) String

func (inst *Halt) String() string

type LoadMem

type LoadMem struct {
	Code Code
	Dst  Reg
	Src  Addr
}

The LoadMem instruction loads the contents of the src memory address into the dst register.

func (*LoadMem) String

func (inst *LoadMem) String() string

type LoadVal

type LoadVal struct {
	Code Code
	Dst  Reg
	Src  Val
}

The LoadVal instruction loads the src immediate value into the dst register.

func (*LoadVal) String

func (inst *LoadVal) String() string

type Move

type Move struct {
	Code Code
	Dst  Reg
	Src  Reg
}

The Move instruction moves the contents of the src register into the dst register.

func (*Move) String

func (inst *Move) String() string

type Nop

type Nop struct {
	Code Code
}

The Nop instruction performs no operation.

func (*Nop) String

func (inst *Nop) String() string

type Or

type Or struct {
	Code Code
	Dst  Reg
	Src1 Reg
	Src2 Reg
}

The Or instruction performs a bitwise OR operation with the src1 and src2 registers and store the result in the dst register.

func (*Or) String

func (inst *Or) String() string

type Reg

type Reg uint8

Reg represents a register between 0 and 15.

func (Reg) String

func (reg Reg) String() string

type Ror

type Ror struct {
	Code Code
	Reg  Reg
	X    Val
}

The Ror instruction rotates the bit pattern in the reg register x bits to the right. Each time a bit is rotated out of the low-order end it is placed at the high-order end.

func (*Ror) String

func (inst *Ror) String() string

type Store

type Store struct {
	Code Code
	Dst  Addr
	Src  Reg
}

The Store instruction stores the contents of the src register into the dst memory address.

func (*Store) String

func (inst *Store) String() string

type Val

type Val uint8

Val represents an immediate value between 0 and 255.

func (Val) String

func (val Val) String() string

type Xor

type Xor struct {
	Code Code
	Dst  Reg
	Src1 Reg
	Src2 Reg
}

The Xor instruction performs a bitwise XOR operation with the src1 and src2 registers and store the result in the dst register.

func (*Xor) String

func (inst *Xor) String() string

Jump to

Keyboard shortcuts

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