-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatapath.rb
More file actions
73 lines (59 loc) · 2.01 KB
/
datapath.rb
File metadata and controls
73 lines (59 loc) · 2.01 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
require './check_style'
require './pipeline'
class Datapath
attr_accessor :pc, :instructions, :labels, :registerValues, :memory
# @@registerValues = {"$zero"=>0, "$0"=>0, "$at"=>0, "$v0"=>0, "$v1"=>0, "$t8"=>0,
# "$t9"=>0, "$k0"=>0, "$k1"=>0, "$gp"=>0, "$sp"=>0, "$fp"=>0, "$ra"=>0, "$t0"=>0,
# "$t1"=>0, "$t2"=>0, "$t3"=>0, "$t4"=>0, "$t5"=>0, "$t6"=>0, "$t7"=>0, "$a0"=>0,
# "$a1"=>0, "$a2"=>0, "$a3"=>0, "$s0"=>0, "$s1"=>0, "$s2"=>0, "$s3"=>0, "$s4"=>0,
# "$s5"=>0, "$s6"=>0, "$s7"=>0, "IF"=>0, "ID"=>0,"EX"=>0, "MEM"=>0}
# @@stages = {"IF" => "", "ID"=>"", "EX"=>"", "MEM"=>"", "WB"=>""}
def initialize
@stages = {"IF" => "", "ID"=>"", "EX"=>"", "MEM"=>"", "WB"=>""}
@pc = 0
@instructions = []
@labels = {}
@registerValues = {"$zero"=>0, "$0"=>0, "$at"=>0, "$v0"=>0, "$v1"=>0, "$t8"=>0,
"$t9"=>0, "$k0"=>0, "$k1"=>0, "$gp"=>0, "$sp"=>50, "$fp"=>0, "$ra"=>0, "$t0"=>0,
"$t1"=>0, "$t2"=>0, "$t3"=>0, "$t4"=>0, "$t5"=>0, "$t6"=>0, "$t7"=>0, "$a0"=>0,
"$a1"=>0, "$a2"=>0, "$a3"=>0, "$s0"=>0, "$s1"=>0, "$s2"=>0, "$s3"=>0, "$s4"=>0,
"$s5"=>0, "$s6"=>0, "$s7"=>0}
@memory = []
end
def load_instructions instructions
out = CheckStyle.check instructions
puts "mips_> #{out[:errors]}" if out[:errors] != nil
return if out[:errors] != nil
@instructions = out[:instructions] if out[:instructions] != nil
@labels = out[:labels] if out[:labels] != nil
puts "mips_> #{@instructions}"
puts " #{@labels}"
pipeline = Pipeline.new self
# @pc = pipeline.pc
# @memory = pipeline.data
end
def mux sel, input0, input1
(sel == 0 || sel == '0') ? input0 : input1
end
def shift_left_two input
input + "00"
end
def signExtend input
(input[0] * 16) + input
end
def adder input0, input1
(input0 + input1).to_s 2
end
def sll input, amount
return (input << amount).to_s(2)
end
def srl input, amount
return (input >> amount).to_s(2)
end
def binary_string number, bits
"%0#{bits}b" % number
end
def get_bits str, s, e
str.reverse[s..e].reverse
end
end