Skip to content

Instantly share code, notes, and snippets.

@tylereaves
Created February 27, 2014 18:35
Show Gist options
  • Save tylereaves/9256201 to your computer and use it in GitHub Desktop.
Save tylereaves/9256201 to your computer and use it in GitHub Desktop.
#The MIX memory architecture consists of 4000 words, where each word is 5 6-bit values and a sign bit
type TWord* = int32
const
masks: array[0..5, int32] = [0b01_000000_000000_000000_000000_000000'i32,
0b00_111111_000000_000000_000000_000000,
0b00_000000_111111_000000_000000_000000,
0b00_000000_000000_111111_000000_000000,
0b00_000000_000000_000000_111111_000000,
0b00_000000_000000_000000_000000_111111]
data_mask = 0b00_111111_111111_111111_111111_111111
var memory*: array[0..3999, TWord]
converter toInt32*(x: TWord): int32 =
var sign = (x and masks[0])
if sign == 0:
return x and data_mask
return -(x and data_mask)
converter toTWord(x: int32): TWord =
if abs(x) > data_mask:
raise newexception(EInvalidValue,"Value will not fit in a 30 bit word")
var t: TWord = data_mask and x
if x < 0:
t = t or masks[0]
return t
proc writeword *(addr, value: TWord): =
if addr < 4000:
memory[addr] = value
else:
raise newexception(EAccessViolation, "Address our of range")
proc readword *(addr: TWord, start, stop: int): int32
var mask = 0
for x in countup(start, stop):
mask or= masks[x]
var data = (memory[addr] and mask) shr (6 * (6 - stop))
if memory[addr] and sign != 0:
return -data
return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment