Skip to content

Commit

Permalink
added movk
Browse files Browse the repository at this point in the history
  • Loading branch information
AdinAck committed Oct 5, 2022
1 parent d1c7cc6 commit c41b609
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 46 deletions.
17 changes: 14 additions & 3 deletions LEGv8-Simulator/Models/CPUModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -432,16 +432,27 @@ class CPUModel: ObservableObject {

touchedFlags = false

// if alignment != "lsl" {
// throw CPUError.invalidInstruction(alignment)
// }
if ![0, 16, 32, 48].contains(shift) {
throw CPUError.invalidImmediate(String(shift))
}

registers[destination]! = value << shift
}

func movk(_ destination: String, _ value: Int64, _ alignment: String, _ shift: Int64) throws {
// verify valid registers and immediate
try isValidRegister(destination, true)
try isValidImmediate(value)

touchedFlags = false

if ![0, 16, 32, 48].contains(shift) {
throw CPUError.invalidImmediate(String(shift))
}

registers[destination]! = ((registers[destination]! << shift) >> shift) + value << shift
}

func mov(_ destination: String, _ source: String) throws {
// verify valid registers
try isValidRegister(destination, true)
Expand Down
65 changes: 26 additions & 39 deletions LEGv8-Simulator/Utils/Interpreter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,98 +128,77 @@ class Interpreter: ObservableObject {
}

// branching
func cbz(_ source: String, _ label: String) throws {
if cpu.registers[source]! == 0 {
lexer.cursor = labelMap[label]!
}
}

func cbnz(_ source: String, _ label: String) throws {
if cpu.registers[source]! != 0 {
lexer.cursor = labelMap[label]!
}
}

func b(_ label: String) throws {
// verify label exists
try isValidLabel(label)

lexer.cursor = labelMap[label]!
}

func b_eq(_ label: String) throws {
// verify label exists
try isValidLabel(label)

if !cpu.flags[1] { // z == 0
lexer.cursor = labelMap[label]!
}
}

func b_ne(_ label: String) throws {
// verify label exists
try isValidLabel(label)

if cpu.flags[1] { // z == 1
lexer.cursor = labelMap[label]!
}
}

func b_hs(_ label: String) throws {
// verify label exists
try isValidLabel(label)

if cpu.flags[2] { // c == 1
lexer.cursor = labelMap[label]!
}
}

func b_lo(_ label: String) throws {
// verify label exists
try isValidLabel(label)

if !cpu.flags[2] { // c == 0
lexer.cursor = labelMap[label]!
}
}

func b_hi(_ label: String) throws {
// verify label exists
try isValidLabel(label)

if !cpu.flags[1] && cpu.flags[2] { // z == 0 && c == 1
lexer.cursor = labelMap[label]!
}
}

func b_ls(_ label: String) throws {
// verify label exists
try isValidLabel(label)

if !(!cpu.flags[1] && cpu.flags[2]) { // !(z == 0 && c == 1)
lexer.cursor = labelMap[label]!
}
}

func b_ge(_ label: String) throws {
// verify label exists
try isValidLabel(label)

if cpu.flags[0] == cpu.flags[3] { // n == v
lexer.cursor = labelMap[label]!
}
}

func b_lt(_ label: String) throws {
// verify label exists
try isValidLabel(label)

if cpu.flags[0] != cpu.flags[3] { // n != v
lexer.cursor = labelMap[label]!
}
}

func b_gt(_ label: String) throws {
// verify label exists
try isValidLabel(label)

if !cpu.flags[1] && cpu.flags[0] == cpu.flags[3] { // z == 0 && n == v
lexer.cursor = labelMap[label]!
}
}

func b_le(_ label: String) throws {
// verify label exists
try isValidLabel(label)

if !(!cpu.flags[1] && cpu.flags[0] == cpu.flags[3]) { // !(z == 0 && n == v)
lexer.cursor = labelMap[label]!
}
Expand Down Expand Up @@ -274,7 +253,7 @@ class Interpreter: ObservableObject {
if arguments.count > 2 {
let _ = try parseLiteral(arguments[2])
}
case "movz":
case "movz", "movk":
try verifyArgumentCount(arguments.count, [4])
try isValidRegister(arguments[0])
let _ = try parseLiteral(arguments[1])
Expand All @@ -299,12 +278,13 @@ class Interpreter: ObservableObject {
try isValidRegister(arguments[0])
try isValidRegister(arguments[1])
let _ = try parseLiteral(arguments[2])
case "cbz", "cbnz":
try verifyArgumentCount(arguments.count, [2])
try isValidRegister(arguments[0])
try isValidLabel(arguments[1])
case "b", "b.eq", "b.ne", "b.hs", "b.lo", "b.hi", "b.ls", "b.ge", "b.lt", "b.gt", "b.le":
if labelMap.keys.contains(arguments[0]) {
try verifyArgumentCount(arguments.count, [1])
} else {
throw AssemblerError.invalidLabel(arguments[0])
}
try verifyArgumentCount(arguments.count, [1])
try isValidLabel(arguments[0])
case "_label":
step(mode: mode)
case "_end":
Expand Down Expand Up @@ -379,6 +359,9 @@ class Interpreter: ObservableObject {
case "movz":
try cpu.movz(arguments[0], parseLiteral(arguments[1]), arguments[2], parseLiteral(arguments[3]))
lastTouchedRegister = arguments[0]
case "movk":
try cpu.movk(arguments[0], parseLiteral(arguments[1]), arguments[2], parseLiteral(arguments[3]))
lastTouchedRegister = arguments[0]
case "mov":
try cpu.mov(arguments[0], arguments[1])
lastTouchedRegister = arguments[0]
Expand Down Expand Up @@ -412,6 +395,10 @@ class Interpreter: ObservableObject {
case "lsr":
try cpu.lsr(arguments[0], arguments[1], parseLiteral(arguments[2]))
lastTouchedRegister = arguments[0]
case "cbz":
try cbz(arguments[0], arguments[1])
case "cbnz":
try cbnz(arguments[0], arguments[1])
case "b":
try b(arguments[0])
case "b.eq":
Expand Down
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ Below the text editor, there is a "console" displaying a history of instructions
- STURB
- LDXR
- STXR
- MOVZ
- MOVK
- CBZ
- CBNZ
- BR
- BL

Expand Down

0 comments on commit c41b609

Please sign in to comment.