Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 874be190bd
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 170 lines (136 sloc) 4.466 kb
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 164 165 166 167 168 169
should = require 'should'
d16bunny = require '../src/d16bunny'

pp = require('../src/d16bunny/prettyprint').prettyPrinter

describe "Disassembler", ->
  dis1 = (bytes...) ->
    d = new d16bunny.Disassembler(bytes)
    d.nextInstruction()

  dis1at = (address, bytes...) ->
    memory = []
    for word, i in bytes then memory[address + i] = word
    d = new d16bunny.Disassembler(memory)
    d.address = address
    d.nextInstruction()

  disat = (address, bytes...) ->
    memory = []
    for word, i in bytes then memory[address + i] = word
    d = new d16bunny.Disassembler(memory)
    d.disassemble()


  it "decodes a register SET", ->
    dis1(0x0401).toString().should.eql("SET A, B")

  it "decodes a dereferenced SET", ->
    dis1(0x0561).toString().should.eql("SET [X], B")

  it "decodes an indexed SET", ->
    dis1(0x0661, 0x0004).toString().should.eql("SET [X + 4], B")
    dis1(0x0661, 0x0104).toString().should.eql("SET [X + 0x104], B")
    dis1(0x0661, 0x0104).resolve(0x104: [ "house" ]).toString().should.eql("SET [X + house], B")

  it "decodes an immediate", ->
    dis1(0x7c21, 0x0384).toString().should.eql("SET B, 0x384")

  it "decodes an immediate dereference", ->
    dis1(0x7821, 0x0384).toString().should.eql("SET B, [0x384]")

  it "decodes a packed immediate", ->
    dis1(0xa821).toString().should.eql("SET B, 9")
    dis1(0x8021).toString().should.eql("SET B, 0xffff")

  it "decodes POP", ->
    dis1(0x60e1).toString().should.eql("SET J, POP")

  it "decodes PUSH", ->
    dis1(0x1b01).toString().should.eql("SET PUSH, I")

  it "decodes PEEK", ->
    dis1(0x6461).toString().should.eql("SET X, PEEK")

  it "decodes PICK", ->
    dis1(0x6861, 0x0002).toString().should.eql("SET X, PICK 2")

  it "decodes SP, PC, EX", ->
    dis1(0x6c61).toString().should.eql("SET X, SP")
    dis1(0x7061).toString().should.eql("SET X, PC")
    dis1(0x7461).toString().should.eql("SET X, EX")

  it "gives up for DAT", ->
    dis1(0xfffd).toString().should.eql("DAT 0xfffd")

  it "decodes two immediate arguments", ->
    dis1(0x7fc1, 0xff05, 0x3344).toString().should.eql("SET [0x3344], 0xff05")

  describe "finds targets", ->
    it "in JSR", ->
      x = dis1(0x7c20, 0xfded)
      x.toString().should.eql("JSR 0xfded")
      x.target().should.eql(0xfded)

    it "in IAS", ->
      x = dis1(0x7d40, 0xfded)
      x.toString().should.eql("IAS 0xfded")
      x.target().should.eql(0xfded)

    it "in BRA", ->
      x = dis1at(0x2000, 0x9782)
      x.toString().should.eql("ADD PC, 4 ; 0x2005")
      x.target().should.eql(0x2005)

    it "in JMP", ->
      x = dis1(0x7f81, 0x9000)
      x.toString().should.eql("SET PC, 0x9000")
      x.target().should.eql(0x9000)

  describe "finds terminals", ->
    it "in RET", ->
      x = dis1(0x6381)
      x.toString().should.eql("SET PC, POP")
      x.terminal().should.eql(true)

    it "in JMP", ->
      x = dis1(0x7f81, 0x9000)
      x.toString().should.eql("SET PC, 0x9000")
      x.terminal().should.eql(true)

    it "in RFI", ->
      x = dis1(0x8560)
      x.toString().should.eql("RFI 0")
      x.terminal().should.eql(true)
    
    it "nowhere else", ->
      x = dis1(0xa821)
      x.toString().should.eql("SET B, 9")
      x.terminal().should.eql(false)

  it "processes a loop", ->
    x = disat(0x200, 0x8862, 0xd476, 0x9383, 0x6381)
    x.should.eql([
      ""
      ".ORG 0x200"
      ":t1"
      " ADD X, 1"
      " IFL X, 20"
      " SUB PC, 3 ; t1"
      " SET PC, POP"
    ])

  it "processes nested conditionals", ->
    x = disat(0, 0x9816, 0x9014, 0x8033, 0x14ac, 0x6381)
    x.should.eql([
      " IFL A, 5"
      " IFG A, 3"
      " IFN B, 0xffff"
      " XOR Z, Z"
      " SET PC, POP"
    ])

  it "processes jumps", ->
    x = disat(0x2000, 0x7f81, 0x2002, 0x7f81, 0x2000)
    x.should.eql([
      ""
      ".ORG 0x2000"
      ":t1"
      " SET PC, t2"
      ":t2"
      " SET PC, t1"
    ])

  it "processes gaps", ->
    x = disat(0x333, 0xa821, 0xa821, 0, 0, 0xa821, 0)
    x.should.eql([
      ""
      ".ORG 0x333"
      " SET B, 9"
      " SET B, 9"
      ""
      ".ORG 0x337"
      " SET B, 9"
    ])

  it "can handle jumping into the middle of an instruction", ->
    x = disat(0x3d, 0x3aa9, 0x301, 0x701, 0xb01, 0x413, 0x7c20, 0x3e)
    x.should.eql([
      ".DEFINE t1 0x3e"
      ""
      ".ORG 0x3d"
      " MDI [Z + 0x301], [I]"
      " SET PUSH, B"
      " SET PUSH, C"
      " IFN A, B"
      " JSR t1"
    ])


Something went wrong with that request. Please try again.