Skip to content

Commit

Permalink
Added int instruction which calls a trap.
Browse files Browse the repository at this point in the history
This is work-in-progress on skx#1, because it lays the groundwork
for a series of traps but doesn't implement any itself.

They will come next.
  • Loading branch information
skx committed Jul 1, 2018
1 parent 4e78c28 commit 1fa4987
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
35 changes: 30 additions & 5 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ func (p *Compiler) Compile() {
case token.GOTO:
p.jumpOp(opcode.JUMP_TO)

case token.TRAP:
p.trapOp()

case token.JMP:
p.jumpOp(opcode.JUMP_TO)

Expand Down Expand Up @@ -462,7 +465,8 @@ func (p *Compiler) callOp() {
switch p.curToken.Type {

case token.INT:
addr, _ := strconv.Atoi(p.curToken.Literal)
addr, _ := strconv.ParseInt(p.curToken.Literal, 0, 64)

len1 := addr % 256
len2 := (addr - len1) / 256

Expand All @@ -481,6 +485,28 @@ func (p *Compiler) callOp() {

}

// trapOp inserts an interrupt call / trap
func (p *Compiler) trapOp() {

// advance to the target
p.nextToken()

// The jump might be an absolute target, or a label.
switch p.curToken.Type {

case token.INT:
addr, _ := strconv.ParseInt(p.curToken.Literal, 0, 64)
len1 := addr % 256
len2 := (addr - len1) / 256

p.bytecode = append(p.bytecode, byte(opcode.TRAP_OP))
p.bytecode = append(p.bytecode, byte(len1))
p.bytecode = append(p.bytecode, byte(len2))
default:
fmt.Printf("Fail!")
}
}

// jumpOp inserts a direct jump
func (p *Compiler) jumpOp(operator int) {

Expand All @@ -494,7 +520,7 @@ func (p *Compiler) jumpOp(operator int) {
switch p.curToken.Type {

case token.INT:
addr, _ := strconv.Atoi(p.curToken.Literal)
addr, _ := strconv.ParseInt(p.curToken.Literal, 0, 64)
len1 := addr % 256
len2 := (addr - len1) / 256

Expand Down Expand Up @@ -626,8 +652,7 @@ func (p *Compiler) storeOp() {
p.bytecode = append(p.bytecode, reg)

// Convert to low/high
i, _ := strconv.Atoi(p.curToken.Literal)

i, _ := strconv.ParseInt(p.curToken.Literal, 0, 64)
len1 := i % 256
len2 := (i - len1) / 256
p.bytecode = append(p.bytecode, byte(len1))
Expand Down Expand Up @@ -702,7 +727,7 @@ func (p *Compiler) cmpOp() {
p.bytecode = append(p.bytecode, reg)

// Convert to low/high
i, _ := strconv.Atoi(p.curToken.Literal)
i, _ := strconv.ParseInt(p.curToken.Literal, 0, 64)

len1 := i % 256
len2 := (i - len1) / 256
Expand Down
6 changes: 6 additions & 0 deletions cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,12 @@ func (c *CPU) Run() {
// jump to the call address
c.ip = addr

case 0x80:
debugPrintf("TRAP\n")
c.ip += 1

addr := c.read2Val()
fmt.Printf("Calling trap %04X\n", addr)
default:
fmt.Printf("Unrecognized/Unimplemented opcode %02X at IP %04X\n", instruction, c.ip)
os.Exit(1)
Expand Down
6 changes: 0 additions & 6 deletions examples/simple.in

This file was deleted.

3 changes: 3 additions & 0 deletions opcode/opcode.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,7 @@ var (
STACK_POP = 0x71
STACK_RET = 0x72
STACK_CALL = 0x73

// Interrupt / trap
TRAP_OP = 0x80
)
2 changes: 2 additions & 0 deletions token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const (
NOP = "NOP"
RANDOM = "RANDOM"
SYSTEM = "SYSTEM"
TRAP = "TRAP"
)

// reversed keywords
Expand Down Expand Up @@ -124,6 +125,7 @@ var keywords = map[string]TokenType{
"concat": CONCAT,
"DATA": DATA,
"DB": DB,
"int": TRAP,
"memcpy": MEMCPY,
"nop": NOP,
"random": RANDOM,
Expand Down

0 comments on commit 1fa4987

Please sign in to comment.