Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: whitequark/glasgow
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 31fd5cea2d4e
Choose a base ref
...
head repository: whitequark/glasgow
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 53725aefbf64
Choose a head ref
  • 2 commits
  • 3 files changed
  • 1 contributor

Commits on Dec 3, 2018

  1. Copy the full SHA
    c439bff View commit details
  2. Copy the full SHA
    53725ae View commit details
Showing with 64 additions and 30 deletions.
  1. +15 −15 software/glasgow/arch/boneless/__init__.py
  2. +43 −9 software/glasgow/arch/boneless/instr.py
  3. +6 −6 software/glasgow/arch/boneless/opcode.py
30 changes: 15 additions & 15 deletions software/glasgow/arch/boneless/__init__.py
Original file line number Diff line number Diff line change
@@ -72,16 +72,16 @@
# -----------------------
#
# * class=A
# - code=0 (arithmetic)
# + type=00 ADD
# + type=01 SUB
# + type=10 CMP
# + type=11 (1 unassigned)
# - code=1 (logic)
# - code=0 (logic)
# + type=00 AND
# + type=01 OR
# + type=10 XOR
# + type=11 (1 unassigned)
# - code=1 (arithmetic)
# + type=00 ADD
# + type=01 SUB
# + type=10 CMP
# + type=11 (1 unassigned)
# * class=S
# - code=0
# + type=0 SLL, MOV
@@ -133,6 +133,15 @@
# Mnemonic: MOVA Rd, ±off
# Operation: Rd ← PC+1+off
#
# Logic instructions
# ------------------
#
# Mnemonic: AND Rd, Ra, Rb
# OR Rd, Ra, Rb
# XOR Rd, Ra, Rb
# Operation: Rd ← Ra · Rb
# ZSCO ← flags(Rd)
#
# Arithmetic instructions
# -----------------------
#
@@ -150,15 +159,6 @@
# Operation: t ← Ra - Rb
# ZSCO ← flags(t)
#
# Logic instructions
# ------------------
#
# Mnemonic: AND Rd, Ra, Rb
# OR Rd, Ra, Rb
# XOR Rd, Ra, Rb
# Operation: Rd ← Ra · Rb
# ZSCO ← flags(Rd)
#
# Shift instructions
# ------------------
#
52 changes: 43 additions & 9 deletions software/glasgow/arch/boneless/instr.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import types

from .opcode import *


__all__ = [
"R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7",
"ADD", "ADDI", "ADDU", "AND", "CMP", "J", "JAL", "JE", "JG", "JGE", "JL", "JLE", "JNE",
"JNS", "JNZ", "JR", "JS", "JZ", "LD", "LDI", "LDX", "MOV", "MOVA", "MOVH", "MOVI", "MOVL",
"OR", "ROT", "SLL", "SRA", "SRL", "ST", "STI", "STX", "SUB", "SUBI", "SUBU", "XOR",
"ADD", "ADDI", "AND", "CMP", "J", "JAL", "JC", "JE", "JNC", "JNE", "JNO", "JNS", "JNZ", "JO",
"JR", "JS", "JSGE", "JSGT", "JSLE", "JSLT", "JUGE", "JUGT", "JULE", "JULT", "JZ", "LD", "LDI",
"LDX", "MOV", "MOVA", "MOVH", "MOVI", "MOVL", "NOP", "OR", "ROT", "SLL", "SRA", "SRL", "ST",
"STI", "STX", "SUB", "SUBI", "XOR",
"L", "assemble",
]


@@ -23,20 +30,28 @@ def S_FORMAT(opcode, optype, rd, ra, amt):
((optype & 0b1) << 0))

def M_FORMAT(opcode, rsd, ra, off):
assert rsd in range(8) and ra in range(8) and -16 <= off <= 15
assert rsd in range(8) and ra in range(8)
if isinstance(off, str):
return lambda resolve: M_FORMAT(opcode, rsd, ra, resolve(off))
assert -16 <= off <= 15
return (((opcode & 0b11111) << 10) |
(( rsd & 0b111) << 8) |
(( ra & 0b111) << 5) |
(( off & 0b11111) << 0))

def I_FORMAT(opcode, rsd, imm, u=False):
assert rsd in range(8) and ((not u and -128 <= imm <= 127) or
(u and imm in range(256)))
assert rsd in range(8)
if isinstance(imm, str):
return lambda resolve: I_FORMAT(opcode, rst, resolve(imm), u)
assert ((not u and -128 <= imm <= 127) or
(u and imm in range(256)))
return (((opcode & 0b11111) << 10) |
(( rsd & 0b111) << 8) |
(( imm & 0xff) << 0))

def C_FORMAT(opcode, off):
if isinstance(off, str):
return lambda resolve: C_FORMAT(opcode, resolve(off))
assert -1024 <= off <= 1023
return (((opcode & 0b11111) << 10) |
(( off & 0x7ff) << 0))
@@ -45,14 +60,16 @@ def C_FORMAT(opcode, off):
R0, R1, R2, R3, R4, R5, R6, R7 = range(8)


def ADD (rd, ra, rb): return [A_FORMAT(OPCODE_ARITH, OPTYPE_ADD, rd, ra, rb)]
def SUB (rd, ra, rb): return [A_FORMAT(OPCODE_ARITH, OPTYPE_SUB, rd, ra, rb)]
def CMP (rd, ra, rb): return [A_FORMAT(OPCODE_ARITH, OPTYPE_CMP, rd, ra, rb)]
def NOP (): return [A_FORMAT(OPCODE_LOGIC, OPTYPE_AND, 0, 0, 0)]

def AND (rd, ra, rb): return [A_FORMAT(OPCODE_LOGIC, OPTYPE_AND, rd, ra, rb)]
def OR (rd, ra, rb): return [A_FORMAT(OPCODE_LOGIC, OPTYPE_OR, rd, ra, rb)]
def XOR (rd, ra, rb): return [A_FORMAT(OPCODE_LOGIC, OPTYPE_XOR, rd, ra, rb)]

def ADD (rd, ra, rb): return [A_FORMAT(OPCODE_ARITH, OPTYPE_ADD, rd, ra, rb)]
def SUB (rd, ra, rb): return [A_FORMAT(OPCODE_ARITH, OPTYPE_SUB, rd, ra, rb)]
def CMP (rd, ra, rb): return [A_FORMAT(OPCODE_ARITH, OPTYPE_CMP, rd, ra, rb)]

def SLL (rd, ra, amt): return [S_FORMAT(OPCODE_SHIFT_L, OPTYPE_SLL, rd, ra, amt)]
def ROT (rd, ra, amt): return [S_FORMAT(OPCODE_SHIFT_L, OPTYPE_ROT, rd, ra, amt)]
def SRL (rd, ra, amt): return [S_FORMAT(OPCODE_SHIFT_R, OPTYPE_SRL, rd, ra, amt)]
@@ -105,3 +122,20 @@ def MOVI(rd, imm16):
else:
return MOVH(rd, (imm16 >> 8) + ((imm16 >> 7) & 1)) + \
I_FORMAT(OPCODE_ADDI, rd, imm16 & 0xff, u=True)


def L(label): return label

def assemble(code):
flat_code = []
labels = {}
for elem in code:
if isinstance(elem, str):
assert elem not in labels
labels[elem] = len(flat_code)
else:
flat_code += elem
for offset, elem in enumerate(flat_code):
if isinstance(elem, types.LambdaType):
flat_code[offset] = elem(lambda label: labels[label] - offset - 1)
return flat_code
12 changes: 6 additions & 6 deletions software/glasgow/arch/boneless/opcode.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
OPCODE_ARITH = 0b0000_0
OPTYPE_ADD = 0b00
OPTYPE_SUB = 0b01
OPTYPE_CMP = 0b10

OPCODE_LOGIC = 0b0000_1
OPCODE_LOGIC = 0b0000_0
OPTYPE_AND = 0b00
OPTYPE_OR = 0b01
OPTYPE_XOR = 0b10

OPCODE_ARITH = 0b0000_1
OPTYPE_ADD = 0b00
OPTYPE_SUB = 0b01
OPTYPE_CMP = 0b10

OPCODE_SHIFT_L = 0b0001_0
OPTYPE_SLL = 0b0
OPTYPE_ROT = 0b1