1 |
2 |
olivier.gi |
//----------------------------------------------------------------------------
|
2 |
117 |
olivier.gi |
// Copyright (C) 2009 , Olivier Girard
|
3 |
2 |
olivier.gi |
//
|
4 |
117 |
olivier.gi |
// Redistribution and use in source and binary forms, with or without
|
5 |
|
|
// modification, are permitted provided that the following conditions
|
6 |
|
|
// are met:
|
7 |
|
|
// * Redistributions of source code must retain the above copyright
|
8 |
|
|
// notice, this list of conditions and the following disclaimer.
|
9 |
|
|
// * Redistributions in binary form must reproduce the above copyright
|
10 |
|
|
// notice, this list of conditions and the following disclaimer in the
|
11 |
|
|
// documentation and/or other materials provided with the distribution.
|
12 |
|
|
// * Neither the name of the authors nor the names of its contributors
|
13 |
|
|
// may be used to endorse or promote products derived from this software
|
14 |
|
|
// without specific prior written permission.
|
15 |
2 |
olivier.gi |
//
|
16 |
117 |
olivier.gi |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
17 |
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
18 |
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
19 |
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
20 |
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
21 |
|
|
// OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
22 |
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
23 |
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
24 |
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
25 |
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
26 |
|
|
// THE POSSIBILITY OF SUCH DAMAGE
|
27 |
2 |
olivier.gi |
//
|
28 |
|
|
//----------------------------------------------------------------------------
|
29 |
|
|
//
|
30 |
34 |
olivier.gi |
// *File Name: omsp_register_file.v
|
31 |
2 |
olivier.gi |
//
|
32 |
|
|
// *Module Description:
|
33 |
|
|
// openMSP430 Register files
|
34 |
|
|
//
|
35 |
|
|
// *Author(s):
|
36 |
|
|
// - Olivier Girard, olgirard@gmail.com
|
37 |
|
|
//
|
38 |
|
|
//----------------------------------------------------------------------------
|
39 |
17 |
olivier.gi |
// $Rev: 117 $
|
40 |
|
|
// $LastChangedBy: olivier.girard $
|
41 |
|
|
// $LastChangedDate: 2011-06-23 21:30:51 +0200 (Thu, 23 Jun 2011) $
|
42 |
|
|
//----------------------------------------------------------------------------
|
43 |
103 |
olivier.gi |
`ifdef OMSP_NO_INCLUDE
|
44 |
|
|
`else
|
45 |
23 |
olivier.gi |
`include "openMSP430_defines.v"
|
46 |
103 |
olivier.gi |
`endif
|
47 |
2 |
olivier.gi |
|
48 |
34 |
olivier.gi |
module omsp_register_file (
|
49 |
2 |
olivier.gi |
|
50 |
|
|
// OUTPUTs
|
51 |
|
|
cpuoff, // Turns off the CPU
|
52 |
|
|
gie, // General interrupt enable
|
53 |
|
|
oscoff, // Turns off LFXT1 clock input
|
54 |
|
|
pc_sw, // Program counter software value
|
55 |
|
|
pc_sw_wr, // Program counter software write
|
56 |
|
|
reg_dest, // Selected register destination content
|
57 |
|
|
reg_src, // Selected register source content
|
58 |
|
|
scg1, // System clock generator 1. Turns off the SMCLK
|
59 |
|
|
status, // R2 Status {V,N,Z,C}
|
60 |
|
|
|
61 |
|
|
// INPUTs
|
62 |
|
|
alu_stat, // ALU Status {V,N,Z,C}
|
63 |
|
|
alu_stat_wr, // ALU Status write {V,N,Z,C}
|
64 |
|
|
inst_bw, // Decoded Inst: byte width
|
65 |
|
|
inst_dest, // Register destination selection
|
66 |
|
|
inst_src, // Register source selection
|
67 |
|
|
mclk, // Main system clock
|
68 |
|
|
pc, // Program counter
|
69 |
111 |
olivier.gi |
puc_rst, // Main system reset
|
70 |
2 |
olivier.gi |
reg_dest_val, // Selected register destination value
|
71 |
|
|
reg_dest_wr, // Write selected register destination
|
72 |
|
|
reg_pc_call, // Trigger PC update for a CALL instruction
|
73 |
|
|
reg_sp_val, // Stack Pointer next value
|
74 |
|
|
reg_sp_wr, // Stack Pointer write
|
75 |
|
|
reg_sr_wr, // Status register update for RETI instruction
|
76 |
|
|
reg_sr_clr, // Status register clear for interrupts
|
77 |
|
|
reg_incr // Increment source register
|
78 |
|
|
);
|
79 |
|
|
|
80 |
|
|
// OUTPUTs
|
81 |
|
|
//=========
|
82 |
|
|
output cpuoff; // Turns off the CPU
|
83 |
|
|
output gie; // General interrupt enable
|
84 |
|
|
output oscoff; // Turns off LFXT1 clock input
|
85 |
|
|
output [15:0] pc_sw; // Program counter software value
|
86 |
|
|
output pc_sw_wr; // Program counter software write
|
87 |
|
|
output [15:0] reg_dest; // Selected register destination content
|
88 |
|
|
output [15:0] reg_src; // Selected register source content
|
89 |
|
|
output scg1; // System clock generator 1. Turns off the SMCLK
|
90 |
|
|
output [3:0] status; // R2 Status {V,N,Z,C}
|
91 |
|
|
|
92 |
|
|
// INPUTs
|
93 |
|
|
//=========
|
94 |
|
|
input [3:0] alu_stat; // ALU Status {V,N,Z,C}
|
95 |
|
|
input [3:0] alu_stat_wr; // ALU Status write {V,N,Z,C}
|
96 |
|
|
input inst_bw; // Decoded Inst: byte width
|
97 |
|
|
input [15:0] inst_dest; // Register destination selection
|
98 |
|
|
input [15:0] inst_src; // Register source selection
|
99 |
|
|
input mclk; // Main system clock
|
100 |
|
|
input [15:0] pc; // Program counter
|
101 |
111 |
olivier.gi |
input puc_rst; // Main system reset
|
102 |
2 |
olivier.gi |
input [15:0] reg_dest_val; // Selected register destination value
|
103 |
|
|
input reg_dest_wr; // Write selected register destination
|
104 |
|
|
input reg_pc_call; // Trigger PC update for a CALL instruction
|
105 |
|
|
input [15:0] reg_sp_val; // Stack Pointer next value
|
106 |
|
|
input reg_sp_wr; // Stack Pointer write
|
107 |
|
|
input reg_sr_wr; // Status register update for RETI instruction
|
108 |
|
|
input reg_sr_clr; // Status register clear for interrupts
|
109 |
|
|
input reg_incr; // Increment source register
|
110 |
|
|
|
111 |
|
|
|
112 |
|
|
//=============================================================================
|
113 |
|
|
// 1) AUTOINCREMENT UNIT
|
114 |
|
|
//=============================================================================
|
115 |
|
|
|
116 |
|
|
wire [15:0] incr_op = inst_bw ? 16'h0001 : 16'h0002;
|
117 |
|
|
wire [15:0] reg_incr_val = reg_src+incr_op;
|
118 |
|
|
|
119 |
|
|
wire [15:0] reg_dest_val_in = inst_bw ? {8'h00,reg_dest_val[7:0]} : reg_dest_val;
|
120 |
|
|
|
121 |
|
|
|
122 |
|
|
//=============================================================================
|
123 |
|
|
// 2) SPECIAL REGISTERS (R1/R2/R3)
|
124 |
|
|
//=============================================================================
|
125 |
|
|
|
126 |
|
|
// Source input selection mask (for interrupt support)
|
127 |
|
|
//-----------------------------------------------------
|
128 |
|
|
|
129 |
|
|
wire [15:0] inst_src_in = reg_sr_clr ? 16'h0004 : inst_src;
|
130 |
|
|
|
131 |
|
|
|
132 |
|
|
// R0: Program counter
|
133 |
|
|
//---------------------
|
134 |
|
|
|
135 |
|
|
wire [15:0] r0 = pc;
|
136 |
|
|
|
137 |
|
|
wire [15:0] pc_sw = reg_dest_val_in;
|
138 |
|
|
wire pc_sw_wr = (inst_dest[0] & reg_dest_wr) | reg_pc_call;
|
139 |
|
|
|
140 |
|
|
|
141 |
|
|
// R1: Stack pointer
|
142 |
|
|
//-------------------
|
143 |
|
|
reg [15:0] r1;
|
144 |
|
|
wire r1_wr = inst_dest[1] & reg_dest_wr;
|
145 |
|
|
wire r1_inc = inst_src_in[1] & reg_incr;
|
146 |
|
|
|
147 |
111 |
olivier.gi |
always @(posedge mclk or posedge puc_rst)
|
148 |
|
|
if (puc_rst) r1 <= 16'h0000;
|
149 |
2 |
olivier.gi |
else if (r1_wr) r1 <= reg_dest_val_in & 16'hfffe;
|
150 |
|
|
else if (reg_sp_wr) r1 <= reg_sp_val & 16'hfffe;
|
151 |
|
|
else if (r1_inc) r1 <= reg_incr_val & 16'hfffe;
|
152 |
|
|
|
153 |
|
|
|
154 |
|
|
// R2: Status register
|
155 |
|
|
//---------------------
|
156 |
|
|
reg [15:0] r2;
|
157 |
|
|
wire r2_wr = (inst_dest[2] & reg_dest_wr) | reg_sr_wr;
|
158 |
|
|
|
159 |
|
|
wire r2_c = alu_stat_wr[0] ? alu_stat[0] :
|
160 |
|
|
r2_wr ? reg_dest_val_in[0] : r2[0]; // C
|
161 |
|
|
|
162 |
|
|
wire r2_z = alu_stat_wr[1] ? alu_stat[1] :
|
163 |
|
|
r2_wr ? reg_dest_val_in[1] : r2[1]; // Z
|
164 |
|
|
|
165 |
|
|
wire r2_n = alu_stat_wr[2] ? alu_stat[2] :
|
166 |
|
|
r2_wr ? reg_dest_val_in[2] : r2[2]; // N
|
167 |
|
|
|
168 |
|
|
wire [7:3] r2_nxt = r2_wr ? reg_dest_val_in[7:3] : r2[7:3];
|
169 |
|
|
|
170 |
|
|
wire r2_v = alu_stat_wr[3] ? alu_stat[3] :
|
171 |
|
|
r2_wr ? reg_dest_val_in[8] : r2[8]; // V
|
172 |
|
|
|
173 |
|
|
|
174 |
111 |
olivier.gi |
always @(posedge mclk or posedge puc_rst)
|
175 |
|
|
if (puc_rst) r2 <= 16'h0000;
|
176 |
2 |
olivier.gi |
else if (reg_sr_clr) r2 <= 16'h0000;
|
177 |
|
|
else r2 <= {7'h00, r2_v, r2_nxt, r2_n, r2_z, r2_c};
|
178 |
|
|
|
179 |
|
|
assign status = {r2[8], r2[2:0]};
|
180 |
|
|
assign gie = r2[3];
|
181 |
|
|
assign cpuoff = r2[4] | (r2_nxt[4] & r2_wr);
|
182 |
|
|
assign oscoff = r2[5];
|
183 |
|
|
assign scg1 = r2[7];
|
184 |
|
|
|
185 |
|
|
|
186 |
|
|
// R3: Constant generator
|
187 |
|
|
//------------------------
|
188 |
|
|
reg [15:0] r3;
|
189 |
|
|
wire r3_wr = inst_dest[3] & reg_dest_wr;
|
190 |
|
|
wire r3_inc = inst_src_in[3] & reg_incr;
|
191 |
|
|
|
192 |
111 |
olivier.gi |
always @(posedge mclk or posedge puc_rst)
|
193 |
|
|
if (puc_rst) r3 <= 16'h0000;
|
194 |
2 |
olivier.gi |
else if (r3_wr) r3 <= reg_dest_val_in;
|
195 |
|
|
else if (r3_inc) r3 <= reg_incr_val;
|
196 |
|
|
|
197 |
|
|
|
198 |
|
|
//=============================================================================
|
199 |
|
|
// 4) GENERAL PURPOSE REGISTERS (R4...R15)
|
200 |
|
|
//=============================================================================
|
201 |
|
|
|
202 |
|
|
// R4
|
203 |
|
|
reg [15:0] r4;
|
204 |
|
|
wire r4_wr = inst_dest[4] & reg_dest_wr;
|
205 |
|
|
wire r4_inc = inst_src_in[4] & reg_incr;
|
206 |
111 |
olivier.gi |
always @(posedge mclk or posedge puc_rst)
|
207 |
|
|
if (puc_rst) r4 <= 16'h0000;
|
208 |
2 |
olivier.gi |
else if (r4_wr) r4 <= reg_dest_val_in;
|
209 |
|
|
else if (r4_inc) r4 <= reg_incr_val;
|
210 |
|
|
|
211 |
|
|
// R5
|
212 |
|
|
reg [15:0] r5;
|
213 |
|
|
wire r5_wr = inst_dest[5] & reg_dest_wr;
|
214 |
|
|
wire r5_inc = inst_src_in[5] & reg_incr;
|
215 |
111 |
olivier.gi |
always @(posedge mclk or posedge puc_rst)
|
216 |
|
|
if (puc_rst) r5 <= 16'h0000;
|
217 |
2 |
olivier.gi |
else if (r5_wr) r5 <= reg_dest_val_in;
|
218 |
|
|
else if (r5_inc) r5 <= reg_incr_val;
|
219 |
|
|
|
220 |
|
|
// R6
|
221 |
|
|
reg [15:0] r6;
|
222 |
|
|
wire r6_wr = inst_dest[6] & reg_dest_wr;
|
223 |
|
|
wire r6_inc = inst_src_in[6] & reg_incr;
|
224 |
111 |
olivier.gi |
always @(posedge mclk or posedge puc_rst)
|
225 |
|
|
if (puc_rst) r6 <= 16'h0000;
|
226 |
2 |
olivier.gi |
else if (r6_wr) r6 <= reg_dest_val_in;
|
227 |
|
|
else if (r6_inc) r6 <= reg_incr_val;
|
228 |
|
|
|
229 |
|
|
// R7
|
230 |
|
|
reg [15:0] r7;
|
231 |
|
|
wire r7_wr = inst_dest[7] & reg_dest_wr;
|
232 |
|
|
wire r7_inc = inst_src_in[7] & reg_incr;
|
233 |
111 |
olivier.gi |
always @(posedge mclk or posedge puc_rst)
|
234 |
|
|
if (puc_rst) r7 <= 16'h0000;
|
235 |
2 |
olivier.gi |
else if (r7_wr) r7 <= reg_dest_val_in;
|
236 |
|
|
else if (r7_inc) r7 <= reg_incr_val;
|
237 |
|
|
|
238 |
|
|
// R8
|
239 |
|
|
reg [15:0] r8;
|
240 |
|
|
wire r8_wr = inst_dest[8] & reg_dest_wr;
|
241 |
|
|
wire r8_inc = inst_src_in[8] & reg_incr;
|
242 |
111 |
olivier.gi |
always @(posedge mclk or posedge puc_rst)
|
243 |
|
|
if (puc_rst) r8 <= 16'h0000;
|
244 |
2 |
olivier.gi |
else if (r8_wr) r8 <= reg_dest_val_in;
|
245 |
|
|
else if (r8_inc) r8 <= reg_incr_val;
|
246 |
|
|
|
247 |
|
|
// R9
|
248 |
|
|
reg [15:0] r9;
|
249 |
|
|
wire r9_wr = inst_dest[9] & reg_dest_wr;
|
250 |
|
|
wire r9_inc = inst_src_in[9] & reg_incr;
|
251 |
111 |
olivier.gi |
always @(posedge mclk or posedge puc_rst)
|
252 |
|
|
if (puc_rst) r9 <= 16'h0000;
|
253 |
2 |
olivier.gi |
else if (r9_wr) r9 <= reg_dest_val_in;
|
254 |
|
|
else if (r9_inc) r9 <= reg_incr_val;
|
255 |
|
|
|
256 |
|
|
// R10
|
257 |
|
|
reg [15:0] r10;
|
258 |
|
|
wire r10_wr = inst_dest[10] & reg_dest_wr;
|
259 |
|
|
wire r10_inc = inst_src_in[10] & reg_incr;
|
260 |
111 |
olivier.gi |
always @(posedge mclk or posedge puc_rst)
|
261 |
|
|
if (puc_rst) r10 <= 16'h0000;
|
262 |
2 |
olivier.gi |
else if (r10_wr) r10 <= reg_dest_val_in;
|
263 |
|
|
else if (r10_inc) r10 <= reg_incr_val;
|
264 |
|
|
|
265 |
|
|
// R11
|
266 |
|
|
reg [15:0] r11;
|
267 |
|
|
wire r11_wr = inst_dest[11] & reg_dest_wr;
|
268 |
|
|
wire r11_inc = inst_src_in[11] & reg_incr;
|
269 |
111 |
olivier.gi |
always @(posedge mclk or posedge puc_rst)
|
270 |
|
|
if (puc_rst) r11 <= 16'h0000;
|
271 |
2 |
olivier.gi |
else if (r11_wr) r11 <= reg_dest_val_in;
|
272 |
|
|
else if (r11_inc) r11 <= reg_incr_val;
|
273 |
|
|
|
274 |
|
|
// R12
|
275 |
|
|
reg [15:0] r12;
|
276 |
|
|
wire r12_wr = inst_dest[12] & reg_dest_wr;
|
277 |
|
|
wire r12_inc = inst_src_in[12] & reg_incr;
|
278 |
111 |
olivier.gi |
always @(posedge mclk or posedge puc_rst)
|
279 |
|
|
if (puc_rst) r12 <= 16'h0000;
|
280 |
2 |
olivier.gi |
else if (r12_wr) r12 <= reg_dest_val_in;
|
281 |
|
|
else if (r12_inc) r12 <= reg_incr_val;
|
282 |
|
|
|
283 |
|
|
// R13
|
284 |
|
|
reg [15:0] r13;
|
285 |
|
|
wire r13_wr = inst_dest[13] & reg_dest_wr;
|
286 |
|
|
wire r13_inc = inst_src_in[13] & reg_incr;
|
287 |
111 |
olivier.gi |
always @(posedge mclk or posedge puc_rst)
|
288 |
|
|
if (puc_rst) r13 <= 16'h0000;
|
289 |
2 |
olivier.gi |
else if (r13_wr) r13 <= reg_dest_val_in;
|
290 |
|
|
else if (r13_inc) r13 <= reg_incr_val;
|
291 |
|
|
|
292 |
|
|
// R14
|
293 |
|
|
reg [15:0] r14;
|
294 |
|
|
wire r14_wr = inst_dest[14] & reg_dest_wr;
|
295 |
|
|
wire r14_inc = inst_src_in[14] & reg_incr;
|
296 |
111 |
olivier.gi |
always @(posedge mclk or posedge puc_rst)
|
297 |
|
|
if (puc_rst) r14 <= 16'h0000;
|
298 |
2 |
olivier.gi |
else if (r14_wr) r14 <= reg_dest_val_in;
|
299 |
|
|
else if (r14_inc) r14 <= reg_incr_val;
|
300 |
|
|
|
301 |
|
|
// R15
|
302 |
|
|
reg [15:0] r15;
|
303 |
|
|
wire r15_wr = inst_dest[15] & reg_dest_wr;
|
304 |
|
|
wire r15_inc = inst_src_in[15] & reg_incr;
|
305 |
111 |
olivier.gi |
always @(posedge mclk or posedge puc_rst)
|
306 |
|
|
if (puc_rst) r15 <= 16'h0000;
|
307 |
2 |
olivier.gi |
else if (r15_wr) r15 <= reg_dest_val_in;
|
308 |
|
|
else if (r15_inc) r15 <= reg_incr_val;
|
309 |
|
|
|
310 |
|
|
|
311 |
|
|
//=============================================================================
|
312 |
|
|
// 5) READ MUX
|
313 |
|
|
//=============================================================================
|
314 |
|
|
|
315 |
|
|
assign reg_src = (r0 & {16{inst_src_in[0]}}) |
|
316 |
|
|
(r1 & {16{inst_src_in[1]}}) |
|
317 |
|
|
(r2 & {16{inst_src_in[2]}}) |
|
318 |
|
|
(r3 & {16{inst_src_in[3]}}) |
|
319 |
|
|
(r4 & {16{inst_src_in[4]}}) |
|
320 |
|
|
(r5 & {16{inst_src_in[5]}}) |
|
321 |
|
|
(r6 & {16{inst_src_in[6]}}) |
|
322 |
|
|
(r7 & {16{inst_src_in[7]}}) |
|
323 |
|
|
(r8 & {16{inst_src_in[8]}}) |
|
324 |
|
|
(r9 & {16{inst_src_in[9]}}) |
|
325 |
|
|
(r10 & {16{inst_src_in[10]}}) |
|
326 |
|
|
(r11 & {16{inst_src_in[11]}}) |
|
327 |
|
|
(r12 & {16{inst_src_in[12]}}) |
|
328 |
|
|
(r13 & {16{inst_src_in[13]}}) |
|
329 |
|
|
(r14 & {16{inst_src_in[14]}}) |
|
330 |
|
|
(r15 & {16{inst_src_in[15]}});
|
331 |
|
|
|
332 |
|
|
assign reg_dest = (r0 & {16{inst_dest[0]}}) |
|
333 |
|
|
(r1 & {16{inst_dest[1]}}) |
|
334 |
|
|
(r2 & {16{inst_dest[2]}}) |
|
335 |
|
|
(r3 & {16{inst_dest[3]}}) |
|
336 |
|
|
(r4 & {16{inst_dest[4]}}) |
|
337 |
|
|
(r5 & {16{inst_dest[5]}}) |
|
338 |
|
|
(r6 & {16{inst_dest[6]}}) |
|
339 |
|
|
(r7 & {16{inst_dest[7]}}) |
|
340 |
|
|
(r8 & {16{inst_dest[8]}}) |
|
341 |
|
|
(r9 & {16{inst_dest[9]}}) |
|
342 |
|
|
(r10 & {16{inst_dest[10]}}) |
|
343 |
|
|
(r11 & {16{inst_dest[11]}}) |
|
344 |
|
|
(r12 & {16{inst_dest[12]}}) |
|
345 |
|
|
(r13 & {16{inst_dest[13]}}) |
|
346 |
|
|
(r14 & {16{inst_dest[14]}}) |
|
347 |
|
|
(r15 & {16{inst_dest[15]}});
|
348 |
|
|
|
349 |
|
|
|
350 |
34 |
olivier.gi |
endmodule // omsp_register_file
|
351 |
2 |
olivier.gi |
|
352 |
103 |
olivier.gi |
`ifdef OMSP_NO_INCLUDE
|
353 |
|
|
`else
|
354 |
33 |
olivier.gi |
`include "openMSP430_undefines.v"
|
355 |
103 |
olivier.gi |
`endif
|