Skip to content

Commit

Permalink
Type opcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
dim13 committed Mar 18, 2024
1 parent bffe521 commit 41c44cf
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 29 deletions.
2 changes: 1 addition & 1 deletion core.go
Expand Up @@ -142,7 +142,7 @@ var boolValue = map[bool]uint16{
true: ^uint16(0),
}

func (c *Core) newST0(opcode uint16) uint16 {
func (c *Core) newST0(opcode Op) uint16 {
T, N, R := c.st0, c.d.peek(), c.r.peek()
switch opcode {
case opT: // T
Expand Down
46 changes: 46 additions & 0 deletions op.go
@@ -0,0 +1,46 @@
package j1

type Op uint8

const (
opT Op = iota
opN
opTplusN
opTandN
opTorN
opTxorN
opNotT
opNeqT
opNleT
opNrshiftT
opTminus1
opR
opAtT
opNlshiftT
opDepth
opNuleT
nOps
)

var opcodeNames = [nOps]string{
opT: "T",
opN: "N",
opTplusN: "T+N",
opTandN: "T∧N",
opTorN: "T∨N",
opTxorN: "T⊻N",
opNotT: "¬T",
opNeqT: "N=T",
opNleT: "N<T",
opNrshiftT: "N≫T",
opTminus1: "T-1",
opR: "R",
opAtT: "[T]",
opNlshiftT: "N≪T",
opDepth: "D",
opNuleT: "Nu<T",
}

func (op Op) String() string {
return opcodeNames[op]
}
32 changes: 4 additions & 28 deletions parse.go
Expand Up @@ -96,7 +96,7 @@ func (v Call) compile() uint16 { return v.value() | (2 << 13) }
// │ │ │ └────────────────────────────────────── R → PC
// └──┴──┴───────────────────────────────────────── 0 1 1
type ALU struct {
Opcode uint16
Opcode Op
RtoPC bool
TtoN bool
TtoR bool
Expand All @@ -110,7 +110,7 @@ var expand = []int8{0, 1, -2, -1}

func newALU(v uint16) ALU {
return ALU{
Opcode: (v >> 8) & 15,
Opcode: Op(v>>8) & 15,
RtoPC: v&(1<<12) != 0,
TtoN: v&(1<<7) != 0,
TtoR: v&(1<<6) != 0,
Expand All @@ -123,7 +123,7 @@ func newALU(v uint16) ALU {
func isALU(v uint16) bool { return v&(7<<13) == 3<<13 }

func (v ALU) value() uint16 {
ret := v.Opcode << 8
ret := uint16(v.Opcode) << 8
if v.RtoPC {
ret |= 1 << 12
}
Expand All @@ -143,32 +143,8 @@ func (v ALU) value() uint16 {

func (v ALU) compile() uint16 { return v.value() | (3 << 13) }

const (
opT = iota
opN
opTplusN
opTandN
opTorN
opTxorN
opNotT
opNeqT
opNleT
opNrshiftT
opTminus1
opR
opAtT
opNlshiftT
opDepth
opNuleT
)

var opcodeNames = []string{
"T", "N", "T+N", "T∧N", "T∨N", "T⊻N", "¬T", "N=T",
"N<T", "N≫T", "T-1", "R", "[T]", "N≪T", "D", "Nu<T",
}

func (v ALU) String() string {
s := "T ← " + opcodeNames[v.Opcode]
s := fmt.Sprintf("T ← %v", v.Opcode)
if v.RtoPC {
s += " R→PC"
}
Expand Down

0 comments on commit 41c44cf

Please sign in to comment.