-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectorial_2_mc.txt
282 lines (226 loc) · 9.6 KB
/
vectorial_2_mc.txt
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# -----------------------------------------------------------------------------
# vectorial_2_mc.txt
#
# Lab assignment 2: Microprogramming Lab
# Implementation of subroutines addv_2 and seqv_2.
#
# @author Guillermo Escobero & Raul Olmedo
# @date November 2016
# -----------------------------------------------------------------------------
# -----------------------------------------------------------------------------
# Adds two vectors with the same number of elements and stores the resulting
# vector in the memory.
#
# Returns -1 in the first register if the number of elements provided is not
# correct.
#
# @param Register with the memory address where will be stored the resulting vector
# @param Register with the memory address of the first vector
# @param Register with the memory address of the second vector
# @param Register with the number of elements of the vectors
# -----------------------------------------------------------------------------
addv_2 reg1 reg2 reg3 reg4 {
co=011000,
nwords=1,
reg1=reg(25,21), # R0
reg2=reg(20,16), # V0
reg3=reg(15,11), # V1
reg4=reg(10,6), # number of elements of the vectors
{
# To begin, we save the registers reg1, reg2 and reg3 into the stack to
# preserve the values once the microprogram has ended
# First step: substract 4 to the stack pointer register ($sp)
(SELA=11101, MR=1, MA=0, MB=10, SELCOP=1011, MC=1, T6, SELC=11101, LC=1),
# Step 2: put into the address set by the stack pointer the value that must
# be preserved (reg1)
(SELA=11101, MR=1, T9, C0),
(SELB=10101, MR=0, T10, M1=0, C1),
(BW=11, TA=1, TD=1, W=1),
# Repeat step 1
(SELA=11101, MR=1, MA=0, MB=10, SELCOP=1011, MC=1, T6, SELC=11101, LC=1),
# Step2 (reg2)
(SELA=11101, MR=1, T9, C0),
(SELB=10000, MR=0, T10, M1=0, C1),
(BW=11, TA=1, TD=1, W=1),
# Repeat step 1
(SELA=11101, MR=1, MA=0, MB=10, SELCOP=1011, MC=1, T6, SELC=11101, LC=1),
# Step2 (reg3)
(SELA=11101, MR=1, T9, C0),
(SELB=01011, MR=0, T10, M1=0, C1),
(BW=11, TA=1, TD=1, W=1),
# We start the procedure comparing the number of elements(reg4), if it is
# negative, go to error1 label
(T8=1, C5=1),
(SELB=00000, MR=1, T10=1, C5),
(SELA=00110, MR=0, MA=0, MB=1, SELCOP=1011, MC=1, SELP=11, M7=1, C7=1),
(A0=0, B=0, C=0111, MADDR=error1),
# RT1 is set to 0 to start counting the number of iterations
(SELA=0000, MR=1, T9, C4),
# Compare the number in RT1 (where the counter is set) to the number of
# elements in reg4 (given by the user)
begin1:
(SELB=00110, MR=0, MB=0, MA=1, SELCOP=1011, MC=1, SELP=11, M7=1, C7=1),
(A0=0, B=0, C=0110, MADDR=restore1),
# Start loading the first word in the reg2
(MR=0, SELA=10000, T9=1, C0),
(TA=1, R=1, BW=11, M1=1, C1=1),
# We start retrieving the second word, in reg3
(MR=0, SELB=01011, T10=1, C0),
# Store the first word into RT2
(T1=1, C5, TA=1, R=1, BW=11, M1=1, C1=1),
# Move the value of RT1 into RT3
(SELB=00000, MR=1, SELCOP=1010, MC=1, MA=1, MB=0, C6),
# Store the second word into RT1
(T1=1, C4),
# We introduce the numbers in the ALU, operate them and then save the sum
# into RT2
(MA=1, MB=1, SELCOP=1010, MC=1, T6, C5),
# Move the value of RT3 to RT1 (to keep teh value of the counter unchanged)
(T7, C4),
# We get the address of R0 (reg1) and store in it the result of the sum (RT2)
(MR=0, SELA=10101, T9=1, C0),
(T5=1, M1=0, C1=1),
(BW=11, TA=1, TD=1, W=1),
# Increment the memory address of reg1, reg2 and reg3 (+4)
(MC=1, MR=0, SELA=10101, MA=0, MB=10, SELCOP=1010, T6, SELC=10101, LC=1),
(MC=1, MR=0, SELA=10000, MA=0, MB=10, SELCOP=1010, T6, SELC=10000, LC=1),
(MC=1, MR=0, SELA=01011, MA=0, MB=10, SELCOP=1010, T6, SELC=01011, LC=1),
# Increment the counter by 1
(MA=1, MB=11, SELCOP=1010, MC=1, T6, C4),
# Go to the label begin1 and start again the process
(A0=0, B=1, C=0, MADDR=begin1),
# Label reached when the size of the arrays given in reg4 is negative, it
# stores a -1 in reg1 and goes to label restore1
error1:
(MC=1, RA=0, MA=0, MB=11, SELCOP=1011, T6, SELC=10101, LC=1),
(A0=0, B=1, C=0, MADDR=restore1),
restore1:
# Step 1: retrieve the last value saved in the stack pointer
(SELA=11101, MR=1, T9, C0),
(TA=1, R=1, BW=11, M1=1, C1),
(SELC=01011, MR=0, T1, LC),
# Step 2: Add 4 to the stack pointer to access the next value stored
(SELA=11101, MR=1, MA=0, MB=10, SELCOP=1010, MC=1, T6, SELC=11101, LC=1),
# Step1
(SELA=11101, MR=1, T9, C0),
(TA=1, R=1, BW=11, M1=1, C1),
(SELC=10000, MR=0, T1, LC),
# Step2
(SELA=11101, MR=1, MA=0, MB=10, SELCOP=1010, MC=1, T6, SELC=11101, LC=1),
# Step1
(SELA=11101, MR=1, T9, C0),
(TA=1, R=1, BW=11, M1=1, C1),
(SELC=10101, MR=0, T1, LC),
# Step2
(SELA=11101, MR=1, MA=0, MB=10, SELCOP=1010, MC=1, T6, SELC=11101, LC=1),
# Go to fetch to end the program
(A0=1, B=1, C=0)
}
}
# -----------------------------------------------------------------------------
# Compares two vectors with the same number of elements and returns 1 in the
# first register if the vectors are equal, or 0 if are different.
#
# Returns -1 in the first register if the number of elements provided is not
# correct.
#
# @param Register where will be stored the result (0 or 1)
# @param Register with the memory address of the first vector
# @param Register with the memory address of the second vector
# @param Register with the number of elements of the vectors
# -----------------------------------------------------------------------------
seqv_2 reg1 reg2 reg3 reg4 {
co=011001,
nwords=1,
reg1=reg(25,21), # R0
reg2=reg(20,16), # V0
reg3=reg(15,11), # V1
reg4=reg(10,6), # number of elements of the vectors
{
# To begin, we save the registers reg2 and reg3 into the stack to preserve
# the values once the microprogram has ended
# First step: substract 4 to the stack pointer
(SELA=11101, MR=1, MA=0, MB=10, SELCOP=1011, MC=1, T6, SELC=11101, LC=1),
# Step 2: put into the address set by the stack pointer the value that must
# be preserved (reg2)
(SELA=11101, MR=1, T9, C0),
(SELB=10000, MR=0, T10, M1=0, C1),
(BW=11, TA=1, TD=1, W=1),
# Step1
(SELA=11101, MR=1, MA=0, MB=10, SELCOP=1011, MC=1, T6, SELC=11101, LC=1),
# Step2 (reg3)
(SELA=11101, MR=1, T9, C0),
(SELB=01011, MR=0, T10, M1=0, C1),
(BW=11, TA=1, TD=1, W=1),
# We start the procedure comparing the number of elements, if it is negative,
# go to error1 label
(T8=1, C5=1),
(SELB=00000, MR=1, T10=1, C5),
(SELA=00110, MR=0, MA=0, MB=1, SELCOP=1011, MC=1, SELP=11, M7=1, C7=1),
(A0=0, B=0, C=0111, MADDR=error2),
# RT1 is set to 0 to start counting the number of iterations
(SELA=0000, MR=1, T9, C4),
# Compare the number in RT1 (where the counter is set) to the number of
# elements in reg4 (given by the user)
begin2:
(SELB=00110, MR=0, MB=0, MA=1, SELCOP=1011, MC=1, SELP=11, M7=1, C7=1),
(A0=0, B=0, C=0110, MADDR=end2),
# Start loading the first word stored in the reg2
(MR=0, SELA=10000, T9=1, C0),
(TA=1, R=1, BW=11, M1=1, C1=1),
# We start retrieving the second word, in reg3
(MR=0, SELB=01011, T10=1, C0),
# Store the first word into RT2
(T1=1, C5, TA=1, R=1, BW=11, M1=1, C1=1),
# Move the value of RT1 into RT3 to avoid overriding the value of the
# counter set in RT1
(SELB=00000, MR=1, SELCOP=1010, MC=1, MA=1, MB=0, C6),
# Store the second word into RT1
(T1=1, C4),
# We introduce the values in the ALU, we operate using the XOR operation
# code, then it checks if it is 0 (if it is different it goes to label different)
(MA=1, MB=1, SELCOP=0100, MC=1, SELP=11, M7, C7),
(A0=0, B=1, C=0110, MADDR=different),
# Move the value of RT3 to RT1 (restore the value of the counter)
(T7, C4),
# Increment the memory address of reg1, reg2 and reg3 (+4)
(MC=1, MR=0, SELA=10101, MA=0, MB=10, SELCOP=1010, T6, SELC=10101, LC=1),
(MC=1, MR=0, SELA=10000, MA=0, MB=10, SELCOP=1010, T6, SELC=10000, LC=1),
(MC=1, MR=0, SELA=01011, MA=0, MB=10, SELCOP=1010, T6, SELC=01011, LC=1),
# Increment the counter by 1
(MA=1, MB=11, SELCOP=1010, MC=1, T6, C4),
# Go to the label begin2 and start again the process
(A0=0, B=1, C=0, MADDR=begin2),
# Label reached when the size of the arrays given in reg4 is negative, it
# stores a -1 in reg1 and goes to label end2
error2:
(MC=1, RA=0, MA=0, MB=11, SELCOP=1011, T6, SELC=10101, LC=1),
(A0=0, B=1, C=0, MADDR=end2),
# If during the process a different number is detected, this label is
# reached, setting reg1 to 0 and going to restore2 to end the microprogram
different:
(SELA=00000, MR=1, T9, C5),
(SELC=10101, MR=0, T5, LC),
(A0=0, B=1, C=0, MADDR=restore2),
# Sets reg1 to 1 (since all the elements are equal) and goes to restore2
end2:
(SELA=00000, MR=1, MA=0, MB=11, SELCOP=1010, MC=1, C6),
(SELC=10101, MR=0, T7, LC),
(A0=0, B=1, C=0, MADDR=restore2),
restore2:
# Step 1: retrieve the last value saved in the stack pointer (reg2)
(SELA=11101, MR=1, T9, C0),
(TA=1, R=1, BW=11, M1=1, C1),
(SELC=01011, MR=0, T1, LC),
# Step 2: add 4 to the stack ppointer to access the next value stored
(SELA=11101, MR=1, MA=0, MB=10, SELCOP=1010, MC=1, T6, SELC=11101, LC=1),
# Step1
(SELA=11101, MR=1, T9, C0),
(TA=1, R=1, BW=11, M1=1, C1),
(SELC=10000, MR=0, T1, LC),
# Step2
(SELA=11101, MR=1, MA=0, MB=10, SELCOP=1010, MC=1, T6, SELC=11101, LC=1),
# Jump to fetch
(A0=1, B=1, C=0)
}
}