aboutsummaryrefslogtreecommitdiff
path: root/parse.go
blob: 3777c76c74db9fda1b67f4d6cadda2cbadc2786e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package j1

import "fmt"

func isLit(v uint16) bool  { return v&(1<<15) != 0 }
func isJump(v uint16) bool { return v&(7<<13) == 0 }
func isCond(v uint16) bool { return v&(7<<13) == 1<<13 }
func isCall(v uint16) bool { return v&(7<<13) == 2<<13 }
func isALU(v uint16) bool  { return v&(7<<13) == 3<<13 }

// Decode instruction
func Decode(v uint16) Instruction {
	switch {
	case isLit(v):
		return ParseLit(v)
	case isJump(v):
		return ParseJump(v)
	case isCond(v):
		return ParseCond(v)
	case isCall(v):
		return ParseCall(v)
	case isALU(v):
		return ParseALU(v)
	}
	return nil
}

// Instruction interface
type Instruction interface {
	String() string
	Value() uint16
	Compile() uint16
}

// Lit is a literal
type Lit uint16

func ParseLit(v uint16) Lit   { return Lit(v &^ uint16(1<<15)) }
func (v Lit) String() string  { return fmt.Sprintf("LIT %0.4X", uint16(v)) }
func (v Lit) Value() uint16   { return uint16(v) }
func (v Lit) Compile() uint16 { return v.Value() | (1 << 15) }

// Jump is an unconditional branch
type Jump uint16

func ParseJump(v uint16) Jump  { return Jump(v &^ uint16(7<<13)) }
func (v Jump) String() string  { return fmt.Sprintf("UBRANCH %0.4X", uint16(v<<1)) }
func (v Jump) Value() uint16   { return uint16(v) }
func (v Jump) Compile() uint16 { return v.Value() }

// Cond is a conditional branch
type Cond uint16

func ParseCond(v uint16) Cond  { return Cond(v &^ uint16(7<<13)) }
func (v Cond) String() string  { return fmt.Sprintf("0BRANCH %0.4X", uint16(v<<1)) }
func (v Cond) Value() uint16   { return uint16(v) }
func (v Cond) Compile() uint16 { return v.Value() | (1 << 13) }

// Call procedure
type Call uint16

func ParseCall(v uint16) Call  { return Call(v &^ uint16(7<<13)) }
func (v Call) String() string  { return fmt.Sprintf("CALL %0.4X", uint16(v<<1)) }
func (v Call) Value() uint16   { return uint16(v) }
func (v Call) Compile() uint16 { return v.Value() | (2 << 13) }

// ALU instruction
type ALU struct {
	Opcode uint16
	RtoPC  bool
	TtoN   bool
	TtoR   bool
	NtoAtT bool
	Rdir   int8
	Ddir   int8
}

func ParseALU(v uint16) ALU {
	return ALU{
		Opcode: (v >> 8) & 15,
		RtoPC:  v&(1<<12) != 0,
		TtoN:   v&(1<<7) != 0,
		TtoR:   v&(1<<6) != 0,
		NtoAtT: v&(1<<5) != 0,
		Rdir:   expand((v >> 2) & 3),
		Ddir:   expand(v & 3),
	}
}

func (v ALU) Value() uint16 {
	ret := v.Opcode << 8
	if v.RtoPC {
		ret |= 1 << 12
	}
	if v.TtoN {
		ret |= 1 << 7
	}
	if v.TtoR {
		ret |= 1 << 6
	}
	if v.NtoAtT {
		ret |= 1 << 5
	}
	ret |= uint16(v.Rdir&3) << 2
	ret |= uint16(v.Ddir & 3)
	return ret
}

func (v ALU) Compile() uint16 { return v.Value() | (3 << 13) }

func expand(v uint16) int8 {
	if v&2 != 0 {
		v |= 0xfc
	}
	return int8(v)
}

const (
	opT        = iota // 0
	opN               // 1
	opTplusN          // 2
	opTandN           // 3
	opTorN            // 4
	opTxorN           // 5
	opNotT            // 6
	opNeqT            // 7
	opNleT            // 8
	opNrshiftT        // 9
	opTminus1         // 10
	opR               // 11
	opAtT             // 12
	opNlshiftT        // 13
	opDepth           // 14
	opNuleT           // 15
)

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", "depth", "Nu<T",
}

func (v ALU) String() string {
	s := "ALU " + opcodeNames[v.Opcode]
	if v.RtoPC {
		s += " R→PC"
	}
	if v.TtoN {
		s += " T→N"
	}
	if v.TtoR {
		s += " T→R"
	}
	if v.NtoAtT {
		s += " N→[T]"
	}
	if v.Rdir != 0 {
		s += fmt.Sprintf(" r%+d", v.Rdir)
	}
	if v.Ddir != 0 {
		s += fmt.Sprintf(" d%+d", v.Ddir)
	}
	return s
}