OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [orpsocv2/] [bench/] [verilog/] [or1200_monitor.v] - Blame information for rev 863

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 6 julius
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3 449 julius
////  or1200_monitor.v                                            ////
4 360 julius
////                                                              ////
5
////  OR1200 processor monitor module                             ////
6
////                                                              ////
7 449 julius
////  Author(s):                                                  ////
8
////      - Damjan Lampret, lampret@opencores.org                 ////
9
////      - Julius Baxter, julius@opencores.org                   ////
10
////                                                              ////
11 360 julius
//////////////////////////////////////////////////////////////////////
12
////                                                              ////
13 348 julius
//// Copyright (C) 2009, 2010 Authors and OPENCORES.ORG           ////
14 6 julius
////                                                              ////
15
//// This source file may be used and distributed without         ////
16
//// restriction provided that this copyright statement is not    ////
17
//// removed from the file and that any derivative work contains  ////
18
//// the original copyright notice and the associated disclaimer. ////
19
////                                                              ////
20
//// This source file is free software; you can redistribute it   ////
21
//// and/or modify it under the terms of the GNU Lesser General   ////
22
//// Public License as published by the Free Software Foundation; ////
23
//// either version 2.1 of the License, or (at your option) any   ////
24
//// later version.                                               ////
25
////                                                              ////
26
//// This source is distributed in the hope that it will be       ////
27
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
28
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
29
//// PURPOSE.  See the GNU Lesser General Public License for more ////
30
//// details.                                                     ////
31
////                                                              ////
32
//// You should have received a copy of the GNU Lesser General    ////
33
//// Public License along with this source; if not, download it   ////
34
//// from http://www.opencores.org/lgpl.shtml                     ////
35
////                                                              ////
36
//////////////////////////////////////////////////////////////////////
37 67 julius
 
38 55 julius
`include "timescale.v"
39 6 julius
`include "or1200_defines.v"
40 485 julius
`include "or1200_monitor_defines.v"
41 415 julius
 
42 449 julius
 
43 6 julius
module or1200_monitor;
44
 
45 863 olof
   parameter TEST_NAME_STRING = "unnamed";
46
   parameter LOG_DIR          = ".";
47
 
48 6 julius
   integer fexe;
49 449 julius
   integer finsn;
50 860 olof
 
51 6 julius
   reg [23:0] ref;
52 860 olof
`ifdef OR1200_MONITOR_SPRS
53 6 julius
   integer    fspr;
54 860 olof
`endif
55 6 julius
   integer    fgeneral;
56 397 julius
`ifdef OR1200_MONITOR_LOOKUP
57 6 julius
   integer    flookup;
58 860 olof
`endif
59 6 julius
   integer    r3;
60
   integer    insns;
61
 
62 863 olof
   //Trim \0 characters from str and return a right-adjusted string
63
   function [128*8-1:0] trim;
64
      input [128*8-1:0] str;
65
      integer           wpos;
66
      integer           rpos;
67
      begin
68
         trim = 0;
69
         wpos = 0;
70
         for(rpos=0;rpos<=128*8;rpos=rpos+8)
71
           if(str[rpos+:8] != 0) begin
72
              trim[wpos+:8] = str[rpos+:8];
73
              wpos = wpos +8;
74
           end
75
      end
76
   endfunction
77 348 julius
 
78 6 julius
   //
79
   // Initialization
80
   //
81 863 olof
   reg [64*8-1:0]  testcase; //Maximum 64 characters
82
 
83 6 julius
   initial begin
84
      ref = 0;
85 863 olof
      if(!$value$plusargs("testcase=%s", testcase))
86
        testcase = TEST_NAME_STRING;
87
 
88 860 olof
`ifdef OR1200_MONITOR_EXEC_STATE
89 863 olof
      fexe = $fopen(trim({LOG_DIR, "/", testcase, "-executed.log"}));
90 449 julius
`endif
91
`ifdef OR1200_MONITOR_EXEC_LOG_DISASSEMBLY
92
      finsn = fexe;
93 860 olof
`endif
94 6 julius
      $timeformat (-9, 2, " ns", 12);
95 860 olof
`ifdef OR1200_MONITOR_SPRS
96 863 olof
      fspr = $fopen(trim({LOG_DIR, "/", testcase, "-sprs.log"}));
97 860 olof
`endif
98 863 olof
      fgeneral = $fopen(trim({LOG_DIR, "/", testcase, "-general.log"}));
99 860 olof
`ifdef OR1200_MONITOR_LOOKUP
100 863 olof
      flookup = $fopen(trim({LOG_DIR, "/", testcase, "-lookup.log"}));
101 860 olof
`endif
102 6 julius
      insns = 0;
103
 
104
   end
105
 
106
   //
107
   // Get GPR
108
   //
109
   task get_gpr;
110
      input     [4:0]    gpr_no;
111
      output [31:0]      gpr;
112
      integer           j;
113
      begin
114 348 julius
 
115 449 julius
`ifdef OR1200_RFRAM_GENERIC
116 6 julius
         for(j = 0; j < 32; j = j + 1) begin
117 348 julius
            gpr[j] = `OR1200_TOP.`CPU_cpu.`CPU_rf.rf_a.mem[gpr_no*32+j];
118 6 julius
         end
119 860 olof
 
120 449 julius
`else
121 348 julius
         //gpr = `OR1200_TOP.`CPU_cpu.`CPU_rf.rf_a.mem[gpr_no];
122
         gpr = `OR1200_TOP.`CPU_cpu.`CPU_rf.rf_a.get_gpr(gpr_no);
123 860 olof
 
124 449 julius
`endif
125 6 julius
 
126 348 julius
 
127
      end
128
   endtask
129
 
130 6 julius
   //
131
   // Write state of the OR1200 registers into a file
132
   //
133
   // Limitation: only a small subset of register file RAMs
134
   // are supported
135
   //
136
   task display_arch_state;
137
      reg [5:0] i;
138
      reg [31:0] r;
139
      integer    j;
140
      begin
141 397 julius
`ifdef OR1200_MONITOR_EXEC_STATE
142 6 julius
         ref = ref + 1;
143 860 olof
 `ifdef OR1200_MONITOR_LOOKUP
144 6 julius
         $fdisplay(flookup, "Instruction %d: %t", insns, $time);
145 860 olof
 `endif
146
         $fwrite(fexe, "\nEXECUTED(%d): %h:  %h", insns,
147
                 `OR1200_TOP.`CPU_cpu.`CPU_except.wb_pc,
148 351 julius
                 `OR1200_TOP.`CPU_cpu.`CPU_ctrl.wb_insn);
149 449 julius
 `ifdef OR1200_MONITOR_EXEC_LOG_DISASSEMBLY
150 860 olof
         $fwrite(fexe,"\t");
151 351 julius
         // Decode the instruction, print it out
152
         or1200_print_op(`OR1200_TOP.`CPU_cpu.`CPU_ctrl.wb_insn);
153 860 olof
 `endif
154 6 julius
         for(i = 0; i < 32; i = i + 1) begin
155
            if (i % 4 == 0)
156
              $fdisplay(fexe);
157
            get_gpr(i, r);
158
            $fwrite(fexe, "GPR%d: %h  ", i, r);
159
         end
160
         $fdisplay(fexe);
161 348 julius
         r = `OR1200_TOP.`CPU_cpu.`CPU_sprs.sr;
162 6 julius
         $fwrite(fexe, "SR   : %h  ", r);
163 348 julius
         r = `OR1200_TOP.`CPU_cpu.`CPU_sprs.epcr;
164 6 julius
         $fwrite(fexe, "EPCR0: %h  ", r);
165 348 julius
         r = `OR1200_TOP.`CPU_cpu.`CPU_sprs.eear;
166 6 julius
         $fwrite(fexe, "EEAR0: %h  ", r);
167 348 julius
         r = `OR1200_TOP.`CPU_cpu.`CPU_sprs.esr;
168 6 julius
         $fdisplay(fexe, "ESR0 : %h", r);
169 397 julius
`endif //  `ifdef OR1200_MONITOR_EXEC_STATE
170 348 julius
`ifdef OR1200_DISPLAY_EXECUTED
171
         ref = ref + 1;
172 860 olof
 `ifdef OR1200_MONITOR_LOOKUP
173 348 julius
         $fdisplay(flookup, "Instruction %d: %t", insns, $time);
174 860 olof
 `endif
175 348 julius
         $fwrite(fexe, "\nEXECUTED(%d): %h:  %h", insns, `OR1200_TOP.`CPU_cpu.`CPU_except.wb_pc, `OR1200_TOP.`CPU_cpu.`CPU_ctrl.wb_insn);
176 6 julius
`endif
177 348 julius
         insns = insns + 1;
178 449 julius
      end
179 6 julius
   endtask // display_arch_state
180
 
181 860 olof
   /* Keep a trace buffer of the last lot of instructions and addresses
182 6 julius
    * "executed",as read from the writeback stage, and cause a $finish if we hit
183
    * an instruction that is invalid, such as all zeros.
184 860 olof
    * Currently, only breaks on an all zero instruction, but should probably be
185 6 julius
    * made to break for anything with an X in it too. And of course ideally this
186
    * shouldn't be needed - but is handy if someone changes something and stops
187
    * the test continuing forever.
188
    */
189 49 julius
   integer num_nul_inst;
190
   initial num_nul_inst = 0;
191 860 olof
 
192 6 julius
   task monitor_for_crash;
193 348 julius
`define OR1200_MONITOR_CRASH_TRACE_SIZE 32
194
      //Trace buffer of 32 instructions
195
      reg [31:0] insn_trace [0:`OR1200_MONITOR_CRASH_TRACE_SIZE-1];
196
      //Trace buffer of the addresses of those instructions
197 860 olof
      reg [31:0] addr_trace [0:`OR1200_MONITOR_CRASH_TRACE_SIZE-1];
198 449 julius
      integer    i;
199 860 olof
 
200 449 julius
      begin
201
         if (`OR1200_TOP.`CPU_cpu.`CPU_ctrl.wb_insn == 32'h00000000)
202
           num_nul_inst = num_nul_inst + 1;
203
         else
204
           num_nul_inst = 0; // Reset it
205 49 julius
 
206 449 julius
         if (num_nul_inst == 1000) // Sat a loop a bit too long...
207
           begin
208 860 olof
              $fdisplay(fgeneral, "ERROR - no instruction at PC %h",
209 449 julius
                        `OR1200_TOP.`CPU_cpu.`CPU_except.wb_pc);
210
              $fdisplay(fgeneral, "Crash trace: Last %d instructions: ",
211
                        `OR1200_MONITOR_CRASH_TRACE_SIZE);
212 6 julius
 
213 449 julius
              $fdisplay(fgeneral, "PC\t\tINSTR");
214
              for(i=`OR1200_MONITOR_CRASH_TRACE_SIZE-1;i>=0;i=i-1) begin
215
                 $fdisplay(fgeneral, "%h\t%h",addr_trace[i], insn_trace[i]);
216
              end
217 860 olof
              $display("*");
218 485 julius
              $display("* or1200_monitor : OR1200 crash detected (suspected CPU PC corruption)");
219 860 olof
              $display("*");
220
 
221 449 julius
              #100 $finish;
222
           end
223
         else
224
           begin
225
              for(i=`OR1200_MONITOR_CRASH_TRACE_SIZE-1;i>0;i=i-1) begin
226
                 insn_trace[i] = insn_trace[i-1];
227
                 addr_trace[i] = addr_trace[i-1];
228
              end
229
              insn_trace[0] = `OR1200_TOP.`CPU_cpu.`CPU_ctrl.wb_insn;
230
              addr_trace[0] = `OR1200_TOP.`CPU_cpu.`CPU_except.wb_pc;
231
           end
232 860 olof
 
233 449 julius
      end
234 6 julius
   endtask // monitor_for_crash
235
 
236 860 olof
 
237 348 julius
   //
238
   // Write state of the OR1200 registers into a file; version for exception
239
   //
240
   task display_arch_state_except;
241
      reg [5:0] i;
242
      reg [31:0] r;
243
      integer    j;
244
      begin
245 397 julius
`ifdef OR1200_MONITOR_EXEC_STATE
246 348 julius
         ref = ref + 1;
247 860 olof
 `ifdef OR1200_MONITOR_LOOKUP
248 348 julius
         $fdisplay(flookup, "Instruction %d: %t", insns, $time);
249 860 olof
 `endif
250 348 julius
         $fwrite(fexe, "\nEXECUTED(%d): %h:  %h  (exception)", insns, `OR1200_TOP.`CPU_cpu.`CPU_except.ex_pc, `OR1200_TOP.`CPU_cpu.`CPU_ctrl.ex_insn);
251
         for(i = 0; i < 32; i = i + 1) begin
252
            if (i % 4 == 0)
253
              $fdisplay(fexe);
254
            get_gpr(i, r);
255
            $fwrite(fexe, "GPR%d: %h  ", i, r);
256
         end
257
         $fdisplay(fexe);
258
         r = `OR1200_TOP.`CPU_cpu.`CPU_sprs.sr;
259
         $fwrite(fexe, "SR   : %h  ", r);
260
         r = `OR1200_TOP.`CPU_cpu.`CPU_sprs.epcr;
261
         $fwrite(fexe, "EPCR0: %h  ", r);
262
         r = `OR1200_TOP.`CPU_cpu.`CPU_sprs.eear;
263
         $fwrite(fexe, "EEAR0: %h  ", r);
264
         r = `OR1200_TOP.`CPU_cpu.`CPU_sprs.esr;
265
         $fdisplay(fexe, "ESR0 : %h", r);
266
         insns = insns + 1;
267 397 julius
`endif //  `ifdef OR1200_MONITOR_EXEC_STATE
268 348 julius
`ifdef OR1200_DISPLAY_EXECUTED
269
         ref = ref + 1;
270 860 olof
 `ifdef OR1200_MONITOR_LOOKUP
271 348 julius
         $fdisplay(flookup, "Instruction %d: %t", insns, $time);
272 860 olof
 `endif
273
         $fwrite(fexe, "\nEXECUTED(%d): %h:  %h  (exception)", insns,
274
                 `OR1200_TOP.`CPU_cpu.`CPU_except.ex_pc,
275 348 julius
                 `OR1200_TOP.`CPU_cpu.`CPU_ctrl.ex_insn);
276
         insns = insns + 1;
277
`endif
278 860 olof
 
279 449 julius
      end
280 348 julius
   endtask
281
 
282 6 julius
   integer iwb_progress;
283
   reg [31:0] iwb_progress_addr;
284
   //
285
   // WISHBONE bus checker
286
   //
287
   always @(posedge `OR1200_TOP.iwb_clk_i)
288
     if (`OR1200_TOP.iwb_rst_i) begin
289
        iwb_progress = 0;
290
        iwb_progress_addr = `OR1200_TOP.iwb_adr_o;
291
     end
292
     else begin
293
        if (`OR1200_TOP.iwb_cyc_o && (iwb_progress != 2)) begin
294
           iwb_progress = 1;
295
        end
296
        if (`OR1200_TOP.iwb_stb_o) begin
297
           if (iwb_progress >= 1) begin
298
              if (iwb_progress == 1)
299 860 olof
                iwb_progress_addr = `OR1200_TOP.iwb_adr_o;
300 6 julius
              iwb_progress = 2;
301
           end
302
           else begin
303
              $fdisplay(fgeneral, "WISHBONE protocol violation: `OR1200_TOP.iwb_stb_o raised without `OR1200_TOP.iwb_cyc_o, at %t\n", $time);
304
              #100 $finish;
305
           end
306
        end
307
        if (`OR1200_TOP.iwb_ack_i & `OR1200_TOP.iwb_err_i) begin
308
           $fdisplay(fgeneral, "WISHBONE protocol violation: `OR1200_TOP.iwb_ack_i and `OR1200_TOP.iwb_err_i raised at the same time, at %t\n", $time);
309
        end
310
        if ((iwb_progress == 2) && (iwb_progress_addr != `OR1200_TOP.iwb_adr_o)) begin
311
           $fdisplay(fgeneral, "WISHBONE protocol violation: `OR1200_TOP.iwb_adr_o changed while waiting for `OR1200_TOP.iwb_err_i/`OR1200_TOP.iwb_ack_i, at %t\n", $time);
312
           #100 $finish;
313
        end
314
        if (`OR1200_TOP.iwb_ack_i | `OR1200_TOP.iwb_err_i)
315
          if (iwb_progress == 2) begin
316
             iwb_progress = 0;
317
             iwb_progress_addr = `OR1200_TOP.iwb_adr_o;
318
          end
319
          else begin
320
             $fdisplay(fgeneral, "WISHBONE protocol violation: `OR1200_TOP.iwb_ack_i/`OR1200_TOP.iwb_err_i raised without `OR1200_TOP.iwb_cyc_i/`OR1200_TOP.iwb_stb_i, at %t\n", $time);
321
             #100 $finish;
322
          end
323
        if ((iwb_progress == 2) && !`OR1200_TOP.iwb_stb_o) begin
324
           $fdisplay(fgeneral, "WISHBONE protocol violation: `OR1200_TOP.iwb_stb_o lowered without `OR1200_TOP.iwb_err_i/`OR1200_TOP.iwb_ack_i, at %t\n", $time);
325 348 julius
           #100 $finish;
326 6 julius
        end
327
     end
328
 
329
   integer dwb_progress;
330 449 julius
   reg [31:0] dwb_progress_addr;
331
   //
332
   // WISHBONE bus checker
333
   //
334
   always @(posedge `OR1200_TOP.dwb_clk_i)
335
     if (`OR1200_TOP.dwb_rst_i)
336
       dwb_progress = 0;
337
     else begin
338
        if (`OR1200_TOP.dwb_cyc_o && (dwb_progress != 2))
339
          dwb_progress = 1;
340
        if (`OR1200_TOP.dwb_stb_o)
341
          if (dwb_progress >= 1) begin
342
             if (dwb_progress == 1)
343 860 olof
               dwb_progress_addr = `OR1200_TOP.dwb_adr_o;
344 449 julius
             dwb_progress = 2;
345
          end
346
          else begin
347
             $fdisplay(fgeneral, "WISHBONE protocol violation: `OR1200_TOP.dwb_stb_o raised without `OR1200_TOP.dwb_cyc_o, at %t\n", $time);
348
             #100 $finish;
349
          end
350
        if (`OR1200_TOP.dwb_ack_i & `OR1200_TOP.dwb_err_i) begin
351
           $fdisplay(fgeneral, "WISHBONE protocol violation: `OR1200_TOP.dwb_ack_i and `OR1200_TOP.dwb_err_i raised at the same time, at %t\n", $time);
352
        end
353
        if ((dwb_progress == 2) && (dwb_progress_addr != `OR1200_TOP.dwb_adr_o)) begin
354
           $fdisplay(fgeneral, "WISHBONE protocol violation: `OR1200_TOP.dwb_adr_o changed while waiting for `OR1200_TOP.dwb_err_i/`OR1200_TOP.dwb_ack_i, at %t\n", $time);
355
           #100 $finish;
356
        end
357
        if (`OR1200_TOP.dwb_ack_i | `OR1200_TOP.dwb_err_i)
358
          if (dwb_progress == 2) begin
359
             dwb_progress = 0;
360
             dwb_progress_addr = `OR1200_TOP.dwb_adr_o;
361
          end
362
          else begin
363
             $fdisplay(fgeneral, "WISHBONE protocol violation: `OR1200_TOP.dwb_ack_i/`OR1200_TOP.dwb_err_i raised without `OR1200_TOP.dwb_cyc_i/`OR1200_TOP.dwb_stb_i, at %t\n", $time);
364
             #100 $finish;
365
          end
366
        if ((dwb_progress == 2) && !`OR1200_TOP.dwb_stb_o) begin
367
           $fdisplay(fgeneral, "WISHBONE protocol violation: `OR1200_TOP.dwb_stb_o lowered without `OR1200_TOP.dwb_err_i/`OR1200_TOP.dwb_ack_i, at %t\n", $time);
368
           #100 $finish;
369
        end
370 6 julius
     end
371
 
372 449 julius
   //
373
   // Hooks for:
374
   // - displaying registers
375
   // - end of simulation
376
   // - access to SPRs
377
   //
378
   always @(posedge `CPU_CORE_CLK)
379 348 julius
     if (!`OR1200_TOP.`CPU_cpu.`CPU_ctrl.wb_freeze) begin
380 449 julius
        //      #2;
381 348 julius
        if (((`OR1200_TOP.`CPU_cpu.`CPU_ctrl.wb_insn[31:26] != `OR1200_OR32_NOP)
382
             | !`OR1200_TOP.`CPU_cpu.`CPU_ctrl.wb_insn[16])
383 860 olof
            & !(`OR1200_TOP.`CPU_cpu.`CPU_except.except_flushpipe &
384 348 julius
                `OR1200_TOP.`CPU_cpu.`CPU_except.ex_dslot))
385 6 julius
          begin
386
             display_arch_state;
387
             monitor_for_crash;
388
          end
389
        else
390 348 julius
          if (`OR1200_TOP.`CPU_cpu.`CPU_except.except_flushpipe)
391 6 julius
            display_arch_state_except;
392 348 julius
        // small hack to stop simulation (l.nop 1):
393
        if (`OR1200_TOP.`CPU_cpu.`CPU_ctrl.wb_insn == 32'h1500_0001) begin
394 6 julius
           get_gpr(3, r3);
395
           $fdisplay(fgeneral, "%t: l.nop exit (%h)", $time, r3);
396 456 julius
`ifdef OR1200_MONITOR_VERBOSE_NOPS
397 789 julius
       // Note that the 'expect' scripts in or1ksim's test suite look for strings
398
       // like "exit(1)", therefore something like "exit(  1)" would fail.
399
       $display("exit(%0d)",r3);
400 860 olof
`endif
401 6 julius
           $finish;
402
        end
403 348 julius
        // debug if test (l.nop 10)
404
        if (`OR1200_TOP.`CPU_cpu.`CPU_ctrl.wb_insn == 32'h1500_000a) begin
405 6 julius
           $fdisplay(fgeneral, "%t: l.nop dbg_if_test", $time);
406
        end
407 348 julius
        // simulation reports (l.nop 2)
408 860 olof
        if (`OR1200_TOP.`CPU_cpu.`CPU_ctrl.wb_insn == 32'h1500_0002) begin
409 6 julius
           get_gpr(3, r3);
410 468 julius
           $fdisplay(fgeneral, "%t: l.nop report (0x%h)", $time, r3);
411 456 julius
`ifdef OR1200_MONITOR_VERBOSE_NOPS
412 789 julius
           // Note that the 'expect' scripts in or1ksim's test suite look for strings
413
           // like "report(0x7ffffffe);", therefore something like "report (0x7ffffffe);"
414
           // (note the extra space character) would fail.
415
           $display("report(0x%h);", r3);
416 456 julius
`endif
417 348 julius
        end
418
        // simulation printfs (l.nop 3)
419 860 olof
        if (`OR1200_TOP.`CPU_cpu.`CPU_ctrl.wb_insn == 32'h1500_0003) begin
420 348 julius
           get_gpr(3, r3);
421 6 julius
           $fdisplay(fgeneral, "%t: l.nop printf (%h)", $time, r3);
422
        end
423 860 olof
        if (`OR1200_TOP.`CPU_cpu.`CPU_ctrl.wb_insn == 32'h1500_0004) begin
424 348 julius
           // simulation putc (l.nop 4)
425
           get_gpr(3, r3);
426
           $write("%c", r3);
427
           $fdisplay(fgeneral, "%t: l.nop putc (%c)", $time, r3);
428
        end
429 860 olof
`ifdef OR1200_MONITOR_SPRS
430 397 julius
        if (`OR1200_TOP.`CPU_cpu.`CPU_sprs.spr_we)
431 6 julius
          $fdisplay(fspr, "%t: Write to SPR : [%h] <- %h", $time,
432 397 julius
                    `OR1200_TOP.`CPU_cpu.`CPU_sprs.spr_addr,
433 348 julius
                    `OR1200_TOP.`CPU_cpu.`CPU_sprs.spr_dat_o);
434 860 olof
        if ((|`OR1200_TOP.`CPU_cpu.`CPU_sprs.spr_cs) &
435 397 julius
            !`OR1200_TOP.`CPU_cpu.`CPU_sprs.spr_we)
436 6 julius
          $fdisplay(fspr, "%t: Read from SPR: [%h] -> %h", $time,
437 860 olof
                    `OR1200_TOP.`CPU_cpu.`CPU_sprs.spr_addr,
438 348 julius
                    `OR1200_TOP.`CPU_cpu.`CPU_sprs.to_wbmux);
439 860 olof
`endif
440 6 julius
     end
441
 
442 348 julius
 
443 415 julius
`ifdef RAM_WB
444 439 julius
 `define RAM_WB_TOP `DUT_TOP.ram_wb0.ram_wb_b3_0
445 415 julius
   task get_insn_from_wb_ram;
446
      input [31:0] addr;
447
      output [31:0] insn;
448
      begin
449 477 julius
         insn = `RAM_WB_TOP.get_mem32(addr[31:2]);
450 415 julius
      end
451
   endtask // get_insn_from_wb_ram
452
`endif
453 860 olof
 
454 348 julius
`ifdef VERSATILE_SDRAM
455 415 julius
 `define SDRAM_TOP `TB_TOP.sdram0
456 348 julius
   // Bit selects to define the bank
457
   // 32 MB part with 4 banks
458
 `define SDRAM_BANK_SEL_BITS 24:23
459
 `define SDRAM_WORD_SEL_TOP_BIT 22
460
   // Gets instruction word from correct bank
461
   task get_insn_from_sdram;
462
      input [31:0] addr;
463
      output [31:0] insn;
464
      reg [`SDRAM_WORD_SEL_TOP_BIT-1:0] word_addr;
465 860 olof
 
466 348 julius
      begin
467 860 olof
         word_addr = addr[`SDRAM_WORD_SEL_TOP_BIT:2];
468 348 julius
         if (addr[`SDRAM_BANK_SEL_BITS] == 2'b00)
469
           begin
470 860 olof
 
471
              //$display("%t: get_insn_from_sdram bank0, word 0x%h, (%h and %h in SDRAM)", $time, word_addr, `SDRAM_TOP.Bank0[{word_addr,1'b0}], `SDRAM_TOP.Bank0[{word_addr,1'b1}]);
472 348 julius
              insn[15:0] = `SDRAM_TOP.Bank0[{word_addr,1'b1}];
473
              insn[31:16] = `SDRAM_TOP.Bank0[{word_addr,1'b0}];
474
           end
475
      end
476 860 olof
 
477 348 julius
   endtask // get_insn_from_sdram
478
`endif //  `ifdef VERSATILE_SDRAM
479
 
480
`ifdef XILINX_DDR2
481 415 julius
 `define DDR2_TOP `TB_TOP.gen_cs[0]
482 348 julius
   // Gets instruction word from correct bank
483
   task get_insn_from_xilinx_ddr2;
484
      input [31:0] addr;
485
      output [31:0] insn;
486 415 julius
      reg [16*8-1:0] ddr2_array_line0,ddr2_array_line1,ddr2_array_line2,
487
                     ddr2_array_line3;
488 860 olof
      integer        word_in_line_num;
489 348 julius
      begin
490 860 olof
         // Get our 4 128-bit chunks (8 half-words in each!! Confused yet?),
491 449 julius
         // 16 words total
492 348 julius
         `DDR2_TOP.gen[0].u_mem0.memory_read(addr[28:27],addr[26:13],{addr[12:6],3'd0},ddr2_array_line0);
493
         `DDR2_TOP.gen[1].u_mem0.memory_read(addr[28:27],addr[26:13],{addr[12:6],3'd0},ddr2_array_line1);
494
         `DDR2_TOP.gen[2].u_mem0.memory_read(addr[28:27],addr[26:13],{addr[12:6],3'd0},ddr2_array_line2);
495
         `DDR2_TOP.gen[3].u_mem0.memory_read(addr[28:27],addr[26:13],{addr[12:6],3'd0},ddr2_array_line3);
496
         case (addr[5:2])
497
           4'h0:
498
             begin
499
                insn[15:0] = ddr2_array_line0[15:0];
500
                insn[31:16] = ddr2_array_line1[15:0];
501
             end
502
           4'h1:
503
             begin
504
                insn[15:0] = ddr2_array_line2[15:0];
505
                insn[31:16] = ddr2_array_line3[15:0];
506
             end
507
           4'h2:
508
             begin
509
                insn[15:0] = ddr2_array_line0[31:16];
510
                insn[31:16] = ddr2_array_line1[31:16];
511
             end
512
           4'h3:
513
             begin
514
                insn[15:0] = ddr2_array_line2[31:16];
515
                insn[31:16] = ddr2_array_line3[31:16];
516
             end
517
           4'h4:
518
             begin
519
                insn[15:0] = ddr2_array_line0[47:32];
520
                insn[31:16] = ddr2_array_line1[47:32];
521
             end
522
           4'h5:
523
             begin
524
                insn[15:0] = ddr2_array_line2[47:32];
525
                insn[31:16] = ddr2_array_line3[47:32];
526
             end
527
           4'h6:
528
             begin
529
                insn[15:0] = ddr2_array_line0[63:48];
530
                insn[31:16] = ddr2_array_line1[63:48];
531
             end
532
           4'h7:
533
             begin
534
                insn[15:0] = ddr2_array_line2[63:48];
535
                insn[31:16] = ddr2_array_line3[63:48];
536
             end
537
           4'h8:
538
             begin
539
                insn[15:0] = ddr2_array_line0[79:64];
540
                insn[31:16] = ddr2_array_line1[79:64];
541
             end
542
           4'h9:
543
             begin
544
                insn[15:0] = ddr2_array_line2[79:64];
545
                insn[31:16] = ddr2_array_line3[79:64];
546
             end
547
           4'ha:
548
             begin
549
                insn[15:0] = ddr2_array_line0[95:80];
550
                insn[31:16] = ddr2_array_line1[95:80];
551
             end
552
           4'hb:
553
             begin
554
                insn[15:0] = ddr2_array_line2[95:80];
555
                insn[31:16] = ddr2_array_line3[95:80];
556
             end
557
           4'hc:
558
             begin
559
                insn[15:0] = ddr2_array_line0[111:96];
560
                insn[31:16] = ddr2_array_line1[111:96];
561
             end
562
           4'hd:
563
             begin
564
                insn[15:0] = ddr2_array_line2[111:96];
565
                insn[31:16] = ddr2_array_line3[111:96];
566
             end
567
           4'he:
568
             begin
569
                insn[15:0] = ddr2_array_line0[127:112];
570
                insn[31:16] = ddr2_array_line1[127:112];
571
             end
572
           4'hf:
573
             begin
574
                insn[15:0] = ddr2_array_line2[127:112];
575
                insn[31:16] = ddr2_array_line3[127:112];
576 860 olof
             end
577 348 julius
         endcase // case (addr[5:2])
578
      end
579
   endtask // get_insn_from_xilinx_ddr2
580 860 olof
`endif
581
 
582
 
583 348 julius
   task get_insn_from_memory;
584
      input [31:0] id_pc;
585
      output [31:0] insn;
586
      begin
587
         // do a decode of which server we should look in
588
         case (id_pc[31:28])
589
`ifdef VERSATILE_SDRAM
590
           4'h0:
591
             get_insn_from_sdram(id_pc, insn);
592
`endif
593
`ifdef XILINX_DDR2
594
           4'h0:
595
             get_insn_from_xilinx_ddr2(id_pc, insn);
596 415 julius
`endif
597
`ifdef RAM_WB
598
           4'h0:
599
             get_insn_from_wb_ram(id_pc, insn);
600 860 olof
`endif
601 348 julius
           4'hf:
602
             // Flash isn't stored in a memory, it's an FSM so just skip/ignore
603
             insn = `OR1200_TOP.`CPU_cpu.`CPU_ctrl.id_insn;
604
           default:
605
             begin
606
                $fdisplay(fgeneral, "%t: Unknown memory server for address 0x%h", $time,id_pc);
607
                insn = 32'hxxxxxxxx; // Unknown server
608
             end
609
         endcase // case (id_pc[31:28])
610
      end
611
   endtask // get_insn_from_memory
612
 
613 860 olof
 
614 449 julius
   //
615
   // Look in the iMMU TLB MR for this address' page, if MMUs are on and enabled
616
   //
617
   task check_for_immu_entry;
618
      input [31:0] pc;
619
      output [31:0] physical_pc;
620 860 olof
      output        mmu_tlb_miss;
621 449 julius
      integer       w,x;
622
 
623
      reg [31:`OR1200_IMMU_PS] pc_vpn;
624 860 olof
 
625 449 julius
      reg [`OR1200_ITLBTRW-1:0] itlb_tr;
626
      reg [`OR1200_ITLBMRW-1:0] itlb_mr;
627
 
628
      integer                   tlb_index;
629
      reg                       mmu_en;
630 860 olof
 
631
 
632 449 julius
      begin
633
         mmu_tlb_miss = 0;
634 860 olof
 
635 449 julius
`ifdef OR1200_NO_IMMU
636
         physical_pc = pc;
637
`else
638
         mmu_en = `OR1200_TOP.`CPU_immu_top.immu_en;
639
         // If MMU is enabled
640
         if (mmu_en)
641
           begin
642 860 olof
 
643 449 julius
              // Look in the iTLB for mapping - get virtual page number
644
              pc_vpn = pc[31:`OR1200_IMMU_PS];
645
 
646 860 olof
              tlb_index = pc[`OR1200_ITLB_INDX];
647
 
648 449 julius
              // Look at the ITLB match register
649
              itlb_mr = `OR1200_TOP.`CPU_immu_top.`CPU_immu_tlb.itlb_mr_ram.mem[tlb_index];
650
 
651
              // Get the translate register here too, in case there's an error, we print it
652
              itlb_tr = `OR1200_TOP.`CPU_immu_top.`CPU_immu_tlb.itlb_tr_ram.mem[tlb_index];
653 860 olof
 
654 449 julius
              if ((itlb_mr[`OR1200_ITLBMR_V_BITS] === 1'b1) & (itlb_mr[`OR1200_ITLBMRW-1:1] === pc[`OR1200_ITLB_TAG]))
655
                begin
656
                   // Page number in match register matches page number of virtual PC, so get the physical
657 860 olof
                   // address from the translate memory
658 449 julius
                   // Now pull the physical page number out of the tranlsate register (it's after bottom 3 bits)
659
                   physical_pc = {itlb_tr[`OR1200_ITLBTRW-1:`OR1200_ITLBTRW-(32-`OR1200_IMMU_PS)],pc[`OR1200_IMMU_PS-1:0]};
660
                   //$display("check_for_immu_entry: found match for virtual PC 0x%h in entry %d of iMMU, mr = 0x%x tr = 0x%x, phys. PC = 0x%h", pc, pc[`OR1200_ITLB_INDX], itlb_mr, itlb_tr, physical_pc);
661
                end // if ((itlb_mr[`OR1200_ITLBMR_V_BITS]) & (itlb_mr[`OR1200_ITLBMRW-1:1] == pc[`OR1200_ITLB_TAG]))
662
              else
663
                begin
664
 
665
                   // Wait a couple of clocks, see if we're doing a miss
666
                   @(posedge `CPU_CORE_CLK);
667
                   @(posedge `CPU_CORE_CLK);
668
                   if (!(`OR1200_TOP.`CPU_immu_top.miss)) // MMU should indicate miss
669
                     begin
670
                        $display("%t: check_for_immu_entry - ERROR - no match found for virtual PC 0x%h in entry %d of iMMU, mr = 0x%x tr = 0x%x, and no miss generated",
671
                                 $time, pc, pc[`OR1200_ITLB_INDX], itlb_mr, itlb_tr);
672
                        #100;
673
                        $finish;
674
                     end
675
                   else
676
                     begin
677
                        mmu_tlb_miss = 1; // Started a miss, so ignore this instruction
678
                     end
679
                end // else: !if((itlb_mr[`OR1200_ITLBMR_V_BITS]) & (itlb_mr[`OR1200_ITLBMRW-1:1] == pc[`OR1200_ITLB_TAG]))
680 860 olof
 
681 449 julius
           end // if (`OR1200_TOP.`CPU_immu_top.immu_en === 1'b1)
682
         else
683
           physical_pc = pc;
684
`endif // !`ifdef OR1200_NO_IMMU
685
      end
686
   endtask // check_for_immu_entry
687
 
688 860 olof
 
689 449 julius
   /*
690
    Instruction memory coherence checking.
691 860 olof
 
692 449 julius
    For new instruction executed in the pipeline - ensure it matches
693
    what is in the main program memory. Perform MMU translations if
694
    it is enabled.
695
    */
696 860 olof
 
697 449 julius
   reg [31:0] mem_word;
698 348 julius
   reg [31:0] last_addr = 0;
699
   reg [31:0] last_mem_word;
700 449 julius
   reg [31:0] physical_pc;
701 860 olof
   reg        tlb_miss;
702 348 julius
 
703 860 olof
 
704 415 julius
`ifdef MEM_COHERENCE_CHECK
705
 `define MEM_COHERENCE_TRIGGER (`OR1200_TOP.`CPU_cpu.`CPU_ctrl.id_void === 1'b0)
706
 
707 449 julius
 `define INSN_TO_CHECK `OR1200_TOP.`CPU_cpu.`CPU_ctrl.id_insn
708
 `define PC_TO_CHECK `OR1200_TOP.`CPU_cpu.`CPU_except.id_pc
709 860 olof
 
710 348 julius
   // Check instruction in decode stage is what is in the RAM
711 449 julius
   always @(posedge `CPU_CORE_CLK)
712 348 julius
     begin
713 415 julius
        if (`MEM_COHERENCE_TRIGGER)
714 348 julius
          begin
715 860 olof
 
716 449 julius
             check_for_immu_entry(`PC_TO_CHECK, physical_pc, tlb_miss);
717 860 olof
 
718 348 julius
             // Check if it's a new PC - will also get triggered if the
719
             // instruction has changed since we last checked it
720 860 olof
             if (((physical_pc !== last_addr) ||
721 477 julius
                  (last_mem_word != `INSN_TO_CHECK)) & !tlb_miss)
722 348 julius
               begin
723
                  // Decode stage not void, check instruction
724
                  // get PC
725 449 julius
                  get_insn_from_memory(physical_pc, mem_word);
726 348 julius
 
727
                  if (mem_word !== `INSN_TO_CHECK)
728
                    begin
729 860 olof
                       $fdisplay(fgeneral, "%t: Instruction mismatch for PC 0x%h (phys. 0x%h) - memory had 0x%h, CPU had 0x%h",
730
                                 $time, `PC_TO_CHECK, physical_pc, mem_word,
731 477 julius
                                 `INSN_TO_CHECK);
732 860 olof
                       $display("%t: Instruction mismatch for PC 0x%h (phys. 0x%h) - memory had 0x%h, CPU had 0x%h",
733
                                $time, `PC_TO_CHECK, physical_pc, mem_word,
734 477 julius
                                `INSN_TO_CHECK);
735
                       #200;
736 860 olof
                       $finish;
737 348 julius
                    end
738 449 julius
                  last_addr = physical_pc;
739 860 olof
                  last_mem_word = mem_word;
740
 
741
               end // if (((physical_pc !== last_addr) || (last_mem_word != `INSN_TO_CHECK))...
742
          end // if (`MEM_COHERENCE_TRIGGER)
743 449 julius
     end // always @ (posedge `CPU_CORE_CLK)
744 860 olof
 
745 449 julius
`endif //  `ifdef MEM_COHERENCE_CHECK
746 860 olof
 
747 449 julius
   // Trigger on each instruction that gets into writeback stage properly
748
   reg exception_coming1, exception_coming2, exception_here;
749
   reg will_jump, jumping, jump_dslot, jumped;
750
   reg rfe, except_during_rfe;
751
   reg dslot_expt;
752
 
753 860 olof
 
754 449 julius
   // Maintain a copy of GPRS for previous instruction
755
   reg [31:0] current_gprs [0:31];
756 860 olof
   reg [31:0] current_epcr, current_eear, current_esr, current_sr;
757 449 julius
   reg [31:0] previous_gprs [0:31];
758
   reg [31:0] previous_epcr;
759
   reg [31:0] previous_eear;
760
   reg [31:0] previous_esr;
761
   reg [31:0] previous_sr;
762 860 olof
 
763
   task update_current_gprs;
764 449 julius
      integer j;
765
      begin
766
         for(j=0;j<32;j=j+1)
767
           begin
768
              get_gpr(j,current_gprs[j]);
769
           end
770 491 julius
         current_sr = `OR1200_TOP.`CPU_cpu.`CPU_sprs.sr ;
771
         current_esr = `OR1200_TOP.`CPU_cpu.`CPU_sprs.epcr ;
772
         current_epcr = `OR1200_TOP.`CPU_cpu.`CPU_sprs.epcr ;
773
         current_eear = `OR1200_TOP.`CPU_cpu.`CPU_sprs.eear ;
774 449 julius
      end
775
   endtask
776 860 olof
 
777
   task update_previous_gprs;
778 449 julius
      integer j;
779
      begin
780
         for(j=0;j<32;j=j+1)
781
           begin
782
              previous_gprs[j] = current_gprs[j];
783
           end
784 860 olof
         previous_sr = current_sr;
785
         previous_esr = current_esr;
786
         previous_epcr = current_epcr;
787 449 julius
         previous_eear = current_eear;
788
      end
789
   endtask // update_previous_gprs
790 860 olof
 
791 449 julius
   // Maintain a list of addresses we expect the processor to execute
792
   // Whenever we hit a branch or jump or rfe we add to this list - when we
793
   // execute it then we remove it from the list.
794
   reg [31:0] expected_addresses [0:31];
795
   reg        expected_addresses_waiting [0:31]; // List indicating if address is waiting
796
   reg        duplicate_expected_addresses_waiting [0:31]; // List indicating if a waiting address will be cleared by the single return
797
   integer    expected_address_num;
798
   // Initialise things on reset
799
   always @(`OR1200_TOP.iwb_rst_i)
800
     begin
801
        for (expected_address_num=0;expected_address_num<32;expected_address_num=expected_address_num+1)
802 860 olof
          begin
803 449 julius
             expected_addresses_waiting[expected_address_num] = 0;
804
             duplicate_expected_addresses_waiting[expected_address_num] = 0;
805 348 julius
          end
806 449 julius
        expected_address_num = 0;
807
     end
808 860 olof
 
809
   task add_expected_address;
810 449 julius
      input [31:0] expected_pc;
811
      begin
812
         if (expected_address_num == 31)
813
           begin
814
              $display("%t: Too many branches not reached",$time);
815 860 olof
              #100;
816 449 julius
              $finish;
817
           end
818
         if (expected_addresses_waiting[expected_address_num])
819
           begin
820
              $display("%t: expected_addresses tracker bugged out. expected_address_num = %0d",$time,expected_address_num);
821 860 olof
              #100;
822 449 julius
              $finish;
823
           end
824
         else
825
           begin
826 860 olof
`ifdef OR1200_MONITOR_JUMPTRACK_DEBUG_OUTPUT
827 449 julius
              // Debugging output...
828
              $display("%t: Adding address 0x%h to expected list index %0d",$time, expected_pc,expected_address_num);
829 860 olof
`endif
830 449 julius
              // Put the expected PC in the list, increase the index
831
              expected_addresses[expected_address_num] = expected_pc;
832
              expected_addresses_waiting[expected_address_num] = 1;
833
              expected_address_num = expected_address_num + 1;
834
           end // else: !if(expected_addresses_waiting[expected_address_num])
835
      end
836
   endtask // add_address_to_expect
837
 
838 860 olof
   // Use this in the case that there's an execption after a jump, in which
839
   // case we'll have two entries when we finally jump back (the one the
840
   // original jump put in, and the one put in by the l.rfe or l.jr/ when
841
   // returning outside of exception handler), so mark this one as OK for
842 449 julius
   // removing the duplicate of
843
   task mark_duplicate_expected_address;
844
      begin
845 860 olof
         // This will always be done on the first instruction of an exception
846
         // that has occured after a delay slot instruction, so
847 449 julius
         // expected_address_num will be one past the entry for the one we will
848
         // get a duplicate return call for
849
         duplicate_expected_addresses_waiting[expected_address_num-1] = 1;
850
      end
851
   endtask // mark_duplicate_expected_address
852
 
853 860 olof
 
854 449 julius
   task check_expected_address;
855
      input [31:0] pc;
856
      input        expecting_hit;
857
      integer      i,j;
858
      reg          hit;
859
      reg          duplicates;
860 860 olof
 
861 449 julius
      begin
862
         hit = 0;
863
         //$display("%t: check_expected_addr 0x%h, index %0d",
864 860 olof
         // $time,pc, expected_address_num);
865 449 julius
         if (expected_address_num > 0)
866
           begin
867
              // First check the last jump we did
868
              if (expected_addresses[expected_address_num-1] == pc)
869
                begin
870
                   // Jump address hit
871
                   // Debugging printout:
872
`ifdef OR1200_MONITOR_JUMPTRACK_DEBUG_OUTPUT
873
                   $display("%t: PC address 0x%h was in expected list, index %0d",$time, pc,expected_address_num-1);
874 860 olof
`endif
875 449 julius
                   expected_address_num = expected_address_num-1;
876
                   expected_addresses_waiting[expected_address_num] = 0;
877
                   hit = 1;
878
                end
879
              else
880
                begin
881
                   // Check through the list
882
                   for(i=0;i<expected_address_num;i=i+1)
883
                     begin
884
                        if (expected_addresses[i] == pc)
885
                          begin
886
                             // Jump address hit
887
                             // Debugging printout:
888 860 olof
`ifdef OR1200_MONITOR_JUMPTRACK_DEBUG_OUTPUT
889 449 julius
                             $display("%t: PC address 0x%h was in expected list, index %0d",$time, pc,i);
890 860 olof
`endif
891 449 julius
                             for(j=i;j<expected_address_num;j=j+1)
892
                               begin
893
                                  // Pull all of the ones above us down one
894 860 olof
                                  expected_addresses_waiting[j]
895 449 julius
                                    = expected_addresses_waiting[j+1];
896 860 olof
                                  expected_addresses[j]
897 449 julius
                                    = expected_addresses[j+1];
898 860 olof
                                  duplicate_expected_addresses_waiting[j]
899 449 julius
                                    = duplicate_expected_addresses_waiting[j+1];
900
                               end
901
                             expected_address_num = expected_address_num-1;
902
                             hit = 1;
903
                             // quit out. only allow 1 hit
904
                             i = expected_address_num;
905
                          end
906
                     end
907
                end // else: !if(expected_addresses[expected_ad...
908
           end // if (expected_address_num > 0)
909 348 julius
 
910 449 julius
         // Check for duplicates this way because of the way we've declared
911
         // the array...
912 860 olof
         duplicates=0;
913 449 julius
         for(i=0;i<32;i=i+1)
914
           duplicates = duplicates | duplicate_expected_addresses_waiting[i];
915 860 olof
 
916 449 julius
         if (hit & duplicates)
917
           begin
918
              // If we got a hit, check for duplicates we're also meant to clear
919 860 olof
`ifdef OR1200_MONITOR_JUMPTRACK_DEBUG_OUTPUT
920 449 julius
              $display;
921 860 olof
`endif
922 449 julius
              for(i=0;i<expected_address_num;i=i+1)
923
                begin
924 860 olof
                   if(duplicate_expected_addresses_waiting[i] &
925 449 julius
                      expected_addresses_waiting[i] &
926
                      expected_addresses[i] == pc)
927
                     begin
928
                        // Found a duplicate call address, clear it
929
                        duplicate_expected_addresses_waiting[i] = 0;
930
                        expected_addresses_waiting[i] = 0;
931
 
932
                        // Now reorder the list - pull all the ones above us
933
                        // down by one
934
                        for(j=i;j<expected_address_num;j=j+1)
935
                          begin
936
                             expected_addresses_waiting[j] = expected_addresses_waiting[j+1];
937
                             expected_addresses[j] = expected_addresses[j+1];
938
                             duplicate_expected_addresses_waiting[j] = duplicate_expected_addresses_waiting[j+1];
939
                          end
940
                        expected_address_num = expected_address_num - 1;
941
                     end
942
                end // for (i=0;i<expected_address_num;i=i+1)
943
           end // if (hit & duplicates)
944 860 olof
 
945 449 julius
         if (expecting_hit & !hit)
946
           begin
947
              // Expected this address to be one we're supposed to jump to, but it wasn't!
948
              $display("%t: Failed to find current PC, 0x%h, in expected PCs for branches/jumps",$time,pc);
949 860 olof
              #100;
950 449 julius
              $finish;
951
           end
952 860 olof
 
953 449 julius
      end
954
   endtask // check_expected_address
955
 
956
   // Task to assert value of GPR
957
   task assert_gpr_val;
958
      input [5:0] regnum;
959 860 olof
      input [31:0] assert_value;
960 449 julius
      input [31:0] pc;
961
      reg [31:0]   reg_val;
962 860 olof
 
963 449 julius
      begin
964
         get_gpr(regnum, reg_val);
965
         if (reg_val !== assert_value)
966
           begin
967
              $display("%t: Assert r%0d value (0x%h) = 0x%h failed. pc=0x%h",
968
                       $time, regnum, reg_val, assert_value,pc);
969 860 olof
              #100;
970 449 julius
              $finish;
971
           end
972
      end
973
   endtask // assert_gpr_val
974 860 olof
 
975 449 julius
   // Task to assert something is true
976
   task assert_this;
977 860 olof
      input assert_result;
978 449 julius
      input [31:0] pc;
979
      begin
980
         if (!assert_result)
981
           begin
982
              $display("%t: Assert failed for instruction at pc=0x%h",
983
                       $time , pc);
984 860 olof
              #100;
985 449 julius
              $finish;
986
           end
987
      end
988 860 olof
   endtask // assert_gpr_val
989 449 julius
 
990
   // The jumping variable doesn't get updated until we do the proper check of
991
   // the current instruction reaching the writeback stage. We need to know
992
   // earlier, eg. in the exception checking part, if this instruction will
993
   // jump. We do that with this task.
994
   task check_for_jump;
995
      input [31:0] insn;
996 860 olof
      reg [5:0]    opcode;
997
      reg          flag;
998 449 julius
      begin
999
         opcode = insn[`OR1K_OPCODE_POS];
1000 860 olof
         // Use the flag from the previous instruction, as the decision
1001
         // is made in the execute stage not in te writeback stage,
1002 449 julius
         // which is where we're getting our instructions.
1003 860 olof
         flag = previous_sr[`OR1200_SR_F];
1004
 
1005
         case (opcode)
1006 449 julius
           `OR1200_OR32_J,
1007
             `OR1200_OR32_JR,
1008
             `OR1200_OR32_JAL,
1009
             `OR1200_OR32_JALR:
1010
               will_jump = 1;
1011
           `OR1200_OR32_BNF:
1012
             will_jump = !flag;
1013
           `OR1200_OR32_BF:
1014
             will_jump = flag;
1015
           default:
1016
             will_jump = 0;
1017
         endcase // case (opcode)
1018
      end
1019 860 olof
   endtask // check_for_jump
1020 449 julius
 
1021
 
1022 860 olof
 
1023 449 julius
   // Detect exceptions from the processor here
1024 860 olof
   reg [13:0] except_trig_r;
1025 449 julius
   reg        exception_coming;
1026 860 olof
 
1027 449 julius
   always @(posedge `CPU_CORE_CLK)
1028
     if (`OR1200_TOP.iwb_rst_i)
1029
       begin
1030
          except_trig_r = 0;
1031
          exception_coming = 0;
1032 860 olof
          except_during_rfe = 0;
1033 449 julius
       end
1034
     else if ((|`OR1200_TOP.`CPU_cpu.`CPU_except.except_trig) && !exception_coming)
1035
       begin
1036
          exception_coming  = 1;
1037
          except_trig_r = `OR1200_TOP.`CPU_cpu.`CPU_except.except_trig;
1038 860 olof
          except_during_rfe = rfe;
1039 449 julius
       end
1040
 
1041
   task check_incoming_exceptions;
1042
      begin
1043 860 olof
 
1044 449 julius
         // Exception timing  - depends on the trigger.
1045 860 olof
         // Appears to be:
1046 449 julius
         // tick timer - dslot - 1 instruction delay, else 2
1047
         // tlb lookasides - 1 instruction for both
1048
 
1049
         casex (except_trig_r)
1050
           13'b1_xxxx_xxxx_xxxx: begin
1051
              //except_type <= #1 `OR1200_EXCEPT_TICK;
1052
              exception_here = exception_coming2;
1053
              exception_coming2 = jump_dslot ? exception_coming: exception_coming1 ;
1054
              exception_coming1 = jump_dslot ? 0 : exception_coming;
1055
           end
1056
           13'b0_1xxx_xxxx_xxxx: begin
1057
              //except_type <= #1 `OR1200_EXCEPT_INT;
1058 860 olof
              #1;
1059 449 julius
           end
1060
           13'b0_01xx_xxxx_xxxx: begin
1061
              //except_type <= #1 `OR1200_EXCEPT_ITLBMISS;
1062
              exception_here = exception_coming2;
1063
              exception_coming2 = jump_dslot ? exception_coming : exception_coming1 ;
1064
              exception_coming1 = jump_dslot ? 0 : exception_coming;
1065
           end
1066
           13'b0_001x_xxxx_xxxx: begin
1067
              //except_type <= #1 `OR1200_EXCEPT_IPF;
1068
              exception_here = exception_coming2;
1069
              exception_coming2 = jump_dslot ? exception_coming : exception_coming1 ;
1070
              exception_coming1 = jump_dslot ? 0 : exception_coming;
1071
           end
1072
           13'b0_0001_xxxx_xxxx: begin
1073
              //except_type <= #1 `OR1200_EXCEPT_BUSERR;
1074 860 olof
              exception_here = exception_coming;
1075
              exception_coming2 = 0;
1076
              exception_coming1 = 0;
1077 449 julius
           end
1078
           13'b0_0000_1xxx_xxxx: begin
1079
              //except_type <= #1 `OR1200_EXCEPT_ILLEGAL;
1080
              if (will_jump)
1081
                begin
1082
                   // Writeback stage instruction will jump, and we have an
1083
                   // illegal instruction in the decode/execute stage, which is
1084
                   // the delay slot, so indicate the exception is coming...
1085 860 olof
                   exception_here = exception_coming2;
1086
                   exception_coming2 = exception_coming;
1087
                   exception_coming1 = 0;
1088 449 julius
                end
1089
              else
1090
                begin
1091 860 olof
                   exception_here = jump_dslot ?
1092 449 julius
                                    exception_coming2 : exception_coming;
1093
                   exception_coming2 = jump_dslot ? exception_coming : 0;
1094 860 olof
                   exception_coming1 = 0;
1095 449 julius
                end
1096
           end
1097
           13'b0_0000_01xx_xxxx: begin
1098
              //except_type <= #1 `OR1200_EXCEPT_ALIGN;
1099
              if(will_jump)
1100
                begin
1101 860 olof
                   exception_here = exception_coming2;
1102
                   exception_coming2 = exception_coming;
1103
                   exception_coming1 = 0;
1104 449 julius
                end
1105
              else
1106
                begin
1107
                   exception_here =  (rfe) ? exception_coming : exception_coming2;
1108
                   exception_coming2 = (rfe) ? 0 : exception_coming;
1109
                   exception_coming1 = 0;
1110
                end
1111
           end
1112
           13'b0_0000_001x_xxxx: begin
1113
              //except_type <= #1 `OR1200_EXCEPT_DTLBMISS;
1114
              // Looks like except_trig goes high here after we check the
1115
              // instruction before the itlb miss after a delay slot, so we
1116
              // miss the dslot variable (it gets propegated before we call
1117
              // this task) so we use the jumped variable here to see if we
1118 860 olof
              // are an exception after a delay slot
1119 449 julius
              //exception_here = (jumped | rfe) ? exception_coming : exception_coming2 ;
1120
              //exception_coming2 = (jumped | rfe) ? 0 : exception_coming;
1121 860 olof
 
1122 449 julius
              exception_here = (jumped | rfe) ? exception_coming : exception_coming2 ;
1123
              exception_coming2 = (jumped | rfe) ? 0 : exception_coming;
1124
 
1125
              exception_coming1 = 0;
1126
           end
1127
           13'b0_0000_0001_xxxx: begin
1128
              //except_type <= #1 `OR1200_EXCEPT_DPF;
1129
              if (jumped) begin // Jumped onto illegal instruction
1130
                 exception_here = exception_coming ;
1131
                 exception_coming2 = 0;
1132
                 exception_coming1 = 0;
1133
              end
1134
              else begin
1135
                 exception_here =  exception_coming2;
1136
                 exception_coming2 = exception_coming;
1137
                 exception_coming1 = 0;
1138
              end
1139
           end
1140
           13'b0_0000_0000_1xxx: begin  // Data Bus Error
1141
              //except_type <= #1 `OR1200_EXCEPT_BUSERR;
1142
              exception_here = exception_coming2 ;
1143
              exception_coming2 = exception_coming;
1144 860 olof
              exception_coming1 = 0;
1145 449 julius
           end
1146
           13'b0_0000_0000_01xx: begin
1147
              //except_type <= #1 `OR1200_EXCEPT_RANGE;
1148 860 olof
              #1;
1149 449 julius
           end
1150
           13'b0_0000_0000_001x: begin
1151 860 olof
              // trap
1152
              #1;
1153 449 julius
           end
1154
           13'b0_0000_0000_0001: begin
1155
              //except_type <= #1 `OR1200_EXCEPT_SYSCALL;
1156 860 olof
              exception_here = exception_coming2;
1157 449 julius
              exception_coming2 = jumped ? exception_coming: exception_coming1 ;
1158
              exception_coming1 = jumped ? 0 : exception_coming;
1159
           end
1160
         endcase // casex (except_trig_r)
1161
 
1162
         exception_coming = 0;
1163
         except_during_rfe = 0;
1164 860 olof
 
1165 449 julius
      end
1166
   endtask // check_incoming_exceptions
1167
 
1168 860 olof
 
1169
 
1170
 
1171 351 julius
   /////////////////////////////////////////////////////////////////////////
1172 449 julius
   // Execution tracking task
1173 351 julius
   /////////////////////////////////////////////////////////////////////////
1174 348 julius
 
1175 860 olof
 
1176 449 julius
`ifdef OR1200_SYSTEM_CHECKER
1177
   always @(posedge `CPU_CORE_CLK)
1178
     begin
1179
        if (`OR1200_TOP.iwb_rst_i)
1180
          begin
1181
             exception_coming1 = 0;exception_coming2 = 0;exception_here= 0;
1182
             jumping = 0; jump_dslot = 0; jumped = 0;
1183
             rfe = 0;
1184
          end
1185
        if (!`OR1200_TOP.`CPU_cpu.`CPU_ctrl.wb_freeze) begin
1186
           //#2 ;
1187 860 olof
           // If instruction isn't a l.nop with bit 16 set (implementation's
1188
           // filler instruction in pipeline), and do not have an exception
1189 449 julius
           // signaled with a dslot instruction in the execute stage
1190 860 olof
           if (((`OR1200_TOP.`CPU_cpu.`CPU_ctrl.wb_insn[`OR1K_OPCODE_POS] !=
1191 449 julius
                 `OR1200_OR32_NOP) || !`OR1200_TOP.`CPU_cpu.`CPU_ctrl.wb_insn[16])
1192 860 olof
               && !(`OR1200_TOP.`CPU_cpu.`CPU_except.except_flushpipe &&
1193 449 julius
                    `OR1200_TOP.`CPU_cpu.`CPU_except.ex_dslot)) // and not except start
1194
             begin
1195 351 julius
 
1196 449 julius
                // Propegate jump-tracking variables
1197
                // If was exception in delay slot, we didn't actually jump
1198
                // so don't set jumped in this case.
1199
                jumped = exception_here ? 0 : jump_dslot;
1200
                jump_dslot = jumping;
1201
                jumping = 0;
1202
                rfe = 0;
1203 860 olof
 
1204 449 julius
                // Now, check if current instruction will jump/branch, this is
1205
                // needed by the exception checking code, sets will_jump=1
1206
                check_for_jump(`OR1200_TOP.`CPU_cpu.`CPU_ctrl.wb_insn);
1207
 
1208
                // Now check if it's an exception this instruction
1209
                check_incoming_exceptions;
1210
 
1211 860 olof
                // Case where we just went to an exception after a jump, so we
1212 449 julius
                // mark the address we were meant to jump to as a place which will
1213
                // have duplicate return entries in the expected address list
1214
                if (exception_here & (jumped | jump_dslot))
1215
                  begin
1216
                     $display("%t: marked as jump address with exception (dup)"
1217
                              ,$time);
1218
                     mark_duplicate_expected_address;
1219
                  end
1220 860 olof
 
1221 449 julius
                or1200_check_execution(`OR1200_TOP.`CPU_cpu.`CPU_ctrl.wb_insn,
1222 860 olof
                                       `OR1200_TOP.`CPU_cpu.`CPU_except.wb_pc,
1223
                                       exception_here);
1224 449 julius
                //$write("%t: pc:0x%h\t",$time,
1225
                //       `OR1200_TOP.`CPU_cpu.`CPU_except.wb_pc);
1226
                // Decode the instruction, print it out
1227 860 olof
                //or1200_print_op(`OR1200_TOP.`CPU_cpu.`CPU_ctrl.wb_insn);
1228 449 julius
                //$write("\t exc:%0h dsl:%0h\n",exception_here,jump_dslot);
1229
 
1230 860 olof
 
1231
 
1232 449 julius
             end
1233
        end // if (!`OR1200_TOP.`CPU_cpu.`CPU_ctrl.wb_freeze)
1234
     end // always @ (posedge `CPU_CORE_CLK)
1235 860 olof
`endif
1236 449 julius
 
1237 860 olof
 
1238 449 julius
   task or1200_check_execution;
1239
      input [31:0] insn;
1240
      input [31:0] pc;
1241
      input        exception;
1242 860 olof
 
1243 449 julius
      reg [5:0]    opcode;
1244
 
1245
      reg [25:0]   j_imm;
1246
      reg [25:0]   br_imm;
1247 860 olof
 
1248 449 julius
      reg [4:0]    rD_num, rA_num, rB_num;
1249
      reg [31:0]   rD_val, rA_val, rB_val;
1250
      reg [15:0]   imm_16bit;
1251 860 olof
 
1252 449 julius
      reg [15:0]   mtspr_imm;
1253 860 olof
 
1254 449 julius
      reg [3:0]    alu_op;
1255
      reg [1:0]    shrot_op;
1256
 
1257
      reg [5:0]    shroti_imm;
1258 860 olof
 
1259 449 julius
      reg [5:0]    sf_op;
1260
 
1261 860 olof
      reg [5:0]    xsync_op;
1262
 
1263 449 julius
      reg          flag;
1264
 
1265
      reg [31:0]   br_j_ea; // Branch/jump effective address
1266 860 olof
 
1267
 
1268 449 julius
      begin
1269 860 olof
 
1270 449 julius
         // Instruction opcode
1271
         opcode = insn[`OR1K_OPCODE_POS];
1272
         // Immediates for jump or branch instructions
1273
         j_imm = insn[`OR1K_J_BR_IMM_POS];
1274
         br_imm = insn[`OR1K_J_BR_IMM_POS];
1275
         // Register numbers (D, A and B)
1276
         rD_num = insn[`OR1K_RD_POS];
1277 860 olof
         rA_num = insn[`OR1K_RA_POS];
1278 449 julius
         rB_num = insn[`OR1K_RB_POS];
1279
         // Bottom 16 bits when used as immediates in various instructions
1280
         imm_16bit = insn[15:0];
1281
         // 16-bit immediate for mtspr instructions
1282
         mtspr_imm = {insn[25:21],insn[10:0]};
1283
         // ALU op for ALU instructions
1284
         alu_op = insn[`OR1K_ALU_OP_POS];
1285
         // Shift-rotate op for SHROT ALU instructions
1286
         shrot_op = insn[`OR1K_SHROT_OP_POS];
1287
         shroti_imm = insn[`OR1K_SHROTI_IMM_POS];
1288
 
1289
         // Set flag op
1290
         sf_op = insn[`OR1K_SF_OP];
1291 860 olof
 
1292 449 julius
         // Xsync/syscall/trap opcode
1293
         xsync_op = insn[`OR1K_XSYNC_OP_POS];
1294
 
1295 860 olof
         // Use the flag from the previous instruction, as the decision
1296
         // is made in the execute stage not in te writeback stage,
1297 449 julius
         // which is where we're getting our instructions.
1298
         flag = previous_sr[`OR1200_SR_F];
1299
 
1300
         update_current_gprs;
1301
 
1302
         // Check MSbit of the immediate, sign extend if set
1303 860 olof
         br_j_ea = j_imm[25] ? pc + {4'hf,j_imm,2'b00} :
1304 449 julius
                   pc + {4'h0,j_imm,2'b00};
1305
 
1306
         if (exception)
1307
           begin
1308
              $display("%t: exception - at 0x%x",$time, pc);
1309
              // get epcr, put it in the addresses we expect to jump
1310
              // back to
1311 860 olof
              // Maybe DON'T do this. Because maybe in linux things we
1312 449 julius
              // interrupt out of, we don't want to execute them again?
1313
              //add_expected_address(current_epcr);
1314
           end
1315
 
1316 860 olof
 
1317 449 julius
         check_expected_address(pc, (jumped & !exception));
1318
 
1319
         rfe = 0;
1320 860 olof
 
1321
         case (opcode)
1322 449 julius
           `OR1200_OR32_J:
1323
             begin
1324
                //
1325
                // PC < - exts(Immediate < < 2) + JumpInsnAddr
1326
                //
1327 860 olof
                //The immediate value is shifted left two bits, sign-extended
1328
                // to program counter width, and then added to the address of
1329
                // the jump instruction. The result is the effective address
1330
                // of the jump. The program unconditionally jumps to EA with
1331 449 julius
                // a delay of one instruction.
1332 860 olof
 
1333 449 julius
                add_expected_address(br_j_ea);
1334
 
1335
                jumping = 1;
1336
             end
1337
           `OR1200_OR32_JAL:
1338
             begin
1339
                //
1340
                //PC < - exts(Immediate < < 2) + JumpInsnAddr
1341
                //LR < - DelayInsnAddr + 4
1342
                //
1343
                // Link reg is r9, check it is PC+8
1344
                //
1345
                add_expected_address(br_j_ea);
1346
                assert_gpr_val(9, pc+8, pc);
1347 860 olof
                jumping = 1;    //
1348 449 julius
             end
1349
           `OR1200_OR32_BNF:
1350
             begin
1351
                //EA < - exts(Immediate < < 2) + BranchInsnAddr
1352
                //PC < - EA if SR[F] cleared
1353
                if (!flag)
1354
                  begin
1355
                     add_expected_address(br_j_ea);
1356
                     jumping = 1;
1357
                  end
1358
             end
1359
           `OR1200_OR32_BF:
1360
             begin
1361
                //EA < - exts(Immediate < < 2) + BranchInsnAddr
1362
                //PC < - EA if SR[F] set
1363
                if (flag)
1364
                  begin
1365
                     add_expected_address(br_j_ea);
1366
                     jumping = 1;
1367
                  end
1368
             end
1369
           `OR1200_OR32_RFE:
1370
             begin
1371
                add_expected_address(current_epcr);
1372 860 olof
                // jumping variable keeps track of jumps/branches with delay
1373 449 julius
                // slot - there is none for l.rfe
1374 860 olof
                rfe = 1;
1375 449 julius
             end
1376
           `OR1200_OR32_JR:
1377
             begin
1378
                //PC < - rB
1379
                get_gpr(rB_num, rB_val);
1380
                add_expected_address(rB_val);
1381
                jumping = 1;
1382
             end
1383
           `OR1200_OR32_JALR:
1384
             begin
1385
                //PC < - rB
1386
                //LR < - DelayInsnAddr + 4
1387
                get_gpr(rB_num, rB_val);
1388
                add_expected_address(rB_val);
1389
                assert_gpr_val(9, pc+8, pc);
1390
                jumping = 1;
1391
             end
1392
           /*
1393
            `OR1200_OR32_LWZ,
1394
            `OR1200_OR32_LBZ,
1395
            `OR1200_OR32_LBS,
1396
            `OR1200_OR32_LHZ,
1397
            `OR1200_OR32_LHS,
1398
            `OR1200_OR32_SW,
1399
            `OR1200_OR32_SB,
1400
            `OR1200_OR32_SH:
1401
            begin
1402
            // Should result in databus access if data cache disabled
1403
            $display("%t: lsu instruction",$time);
1404
end
1405
 
1406
            `OR1200_OR32_MFSPR,
1407
            `OR1200_OR32_MTSPR:
1408
            begin
1409
            // Confirm RF values end up in the correct SPR
1410
            $display("%t: mxspr",$time);
1411
end
1412
 
1413
            `OR1200_OR32_MOVHI,
1414
            `OR1200_OR32_ADDI,
1415
            `OR1200_OR32_ADDIC,
1416
            `OR1200_OR32_ANDI,
1417
            `OR1200_OR32_ORI,
1418
            `OR1200_OR32_XORI,
1419
            `OR1200_OR32_MULI,
1420
            `OR1200_OR32_ALU:
1421
            begin
1422
            // Double check operations done on RF and immediate values
1423
            $display("%t: ALU op",$time);
1424
end
1425 860 olof
 
1426 449 julius
            `OR1200_OR32_SH_ROTI:
1427
            begin
1428
            // Rotate according to immediate - maybe should be in ALU ops
1429
            $display("%t: rotate op",$time);
1430
end
1431 860 olof
 
1432 449 julius
            `OR1200_OR32_SFXXI,
1433
            `OR1200_OR32_SFXX:
1434
            begin
1435
            // Set flag - do the check oursevles, check flag
1436
            $display("%t: set flag op",$time);
1437
end
1438 860 olof
 
1439 449 julius
            `OR1200_OR32_MACI,
1440
            `OR1200_OR32_MACMSB:
1441
            begin
1442
            // Either, multiply signed and accumulate, l.mac
1443
            // or multiply signed and subtract, l.msb
1444
            $display("%t: MAC op",$time);
1445
end
1446
            */
1447 860 olof
 
1448 449 julius
           /*default:
1449
            begin
1450
            $display("%t: Unknown opcode 0x%h at pc 0x%x\n",
1451
            $time,opcode, pc);
1452
end
1453
            */
1454
         endcase // case (opcode)
1455
 
1456 860 olof
         update_previous_gprs;
1457
 
1458 449 julius
      end
1459
   endtask // or1200_check_execution
1460 860 olof
 
1461
 
1462 449 julius
   /////////////////////////////////////////////////////////////////////////
1463
   // Instruction decode task
1464
   /////////////////////////////////////////////////////////////////////////
1465 351 julius
 
1466
   task or1200_print_op;
1467
      input [31:0] insn;
1468
 
1469
      reg [5:0]    opcode;
1470 860 olof
 
1471 351 julius
      reg [25:0]   j_imm;
1472
      reg [25:0]   br_imm;
1473 860 olof
 
1474 351 julius
      reg [4:0]    rD_num, rA_num, rB_num;
1475
      reg [31:0]   rA_val, rB_val;
1476
      reg [15:0]   imm_16bit;
1477 860 olof
      reg [10:0]   imm_split16bit;
1478
 
1479 351 julius
      reg [3:0]    alu_op;
1480
      reg [1:0]    shrot_op;
1481
 
1482
      reg [5:0]    shroti_imm;
1483
 
1484 449 julius
      reg [5:0]    sf_op;
1485 860 olof
 
1486
      reg [5:0]    xsync_op;
1487
 
1488 351 julius
      begin
1489
         // Instruction opcode
1490 449 julius
         opcode = insn[`OR1K_OPCODE_POS];
1491 351 julius
         // Immediates for jump or branch instructions
1492 449 julius
         j_imm = insn[`OR1K_J_BR_IMM_POS];
1493
         br_imm = insn[`OR1K_J_BR_IMM_POS];
1494 351 julius
         // Register numbers (D, A and B)
1495 449 julius
         rD_num = insn[`OR1K_RD_POS];
1496 860 olof
         rA_num = insn[`OR1K_RA_POS];
1497 449 julius
         rB_num = insn[`OR1K_RB_POS];
1498 351 julius
         // Bottom 16 bits when used as immediates in various instructions
1499
         imm_16bit = insn[15:0];
1500
         // Bottom 11 bits used as immediates for l.sX instructions
1501
 
1502
         // Split 16-bit immediate for l.mtspr/l.sX instructions
1503
         imm_split16bit = {insn[25:21],insn[10:0]};
1504
         // ALU op for ALU instructions
1505 449 julius
         alu_op = insn[`OR1K_ALU_OP_POS];
1506 351 julius
         // Shift-rotate op for SHROT ALU instructions
1507 449 julius
         shrot_op = insn[`OR1K_SHROT_OP_POS];
1508
         shroti_imm = insn[`OR1K_SHROTI_IMM_POS];
1509 351 julius
 
1510
         // Set flag op
1511 449 julius
         sf_op = insn[`OR1K_SF_OP];
1512 860 olof
 
1513 449 julius
         // Xsync/syscall/trap opcode
1514
         xsync_op = insn[`OR1K_XSYNC_OP_POS];
1515 860 olof
 
1516 351 julius
         case (opcode)
1517
           `OR1200_OR32_J:
1518 860 olof
             begin
1519
                $fwrite(finsn,"l.j 0x%h", {j_imm,2'b00});
1520 351 julius
             end
1521 860 olof
 
1522 351 julius
           `OR1200_OR32_JAL:
1523
             begin
1524 449 julius
                $fwrite(finsn,"l.jal 0x%h", {j_imm,2'b00});
1525 351 julius
             end
1526
 
1527
           `OR1200_OR32_BNF:
1528
             begin
1529 860 olof
                $fwrite(finsn,"l.bnf 0x%h", {br_imm,2'b00});
1530 351 julius
             end
1531 860 olof
 
1532 351 julius
           `OR1200_OR32_BF:
1533
             begin
1534 449 julius
                $fwrite(finsn,"l.bf 0x%h", {br_imm,2'b00});
1535 351 julius
             end
1536 860 olof
 
1537 351 julius
           `OR1200_OR32_RFE:
1538
             begin
1539 860 olof
                $fwrite(finsn,"l.rfe");
1540 351 julius
             end
1541 860 olof
 
1542 351 julius
           `OR1200_OR32_JR:
1543
             begin
1544 449 julius
                $fwrite(finsn,"l.jr r%0d",rB_num);
1545 351 julius
             end
1546 860 olof
 
1547 351 julius
           `OR1200_OR32_JALR:
1548
             begin
1549 449 julius
                $fwrite(finsn,"l.jalr r%0d",rB_num);
1550 351 julius
             end
1551 860 olof
 
1552 351 julius
           `OR1200_OR32_LWZ:
1553
             begin
1554 449 julius
                $fwrite(finsn,"l.lwz r%0d,0x%0h(r%0d)",rD_num,imm_16bit,rA_num);
1555 351 julius
             end
1556 860 olof
 
1557 351 julius
           `OR1200_OR32_LBZ:
1558
             begin
1559 449 julius
                $fwrite(finsn,"l.lbz r%0d,0x%0h(r%0d)",rD_num,imm_16bit,rA_num);
1560 351 julius
             end
1561 860 olof
 
1562 351 julius
           `OR1200_OR32_LBS:
1563
             begin
1564 449 julius
                $fwrite(finsn,"l.lbs r%0d,0x%0h(r%0d)",rD_num,imm_16bit,rA_num);
1565 351 julius
             end
1566 860 olof
 
1567 351 julius
           `OR1200_OR32_LHZ:
1568
             begin
1569 449 julius
                $fwrite(finsn,"l.lhz r%0d,0x%0h(r%0d)",rD_num,imm_16bit,rA_num);
1570 351 julius
             end
1571 860 olof
 
1572 351 julius
           `OR1200_OR32_LHS:
1573
             begin
1574 449 julius
                $fwrite(finsn,"l.lhs r%0d,0x%0h(r%0d)",rD_num,imm_16bit,rA_num);
1575 351 julius
             end
1576 860 olof
 
1577 351 julius
           `OR1200_OR32_SW:
1578
             begin
1579 449 julius
                $fwrite(finsn,"l.sw 0x%0h(r%0d),r%0d",imm_split16bit,rA_num,rB_num);
1580 351 julius
             end
1581 860 olof
 
1582 351 julius
           `OR1200_OR32_SB:
1583
             begin
1584 449 julius
                $fwrite(finsn,"l.sb 0x%0h(r%0d),r%0d",imm_split16bit,rA_num,rB_num);
1585 351 julius
             end
1586 860 olof
 
1587 351 julius
           `OR1200_OR32_SH:
1588
             begin
1589 860 olof
                $fwrite(finsn,"l.sh 0x%0h(r%0d),r%0d",imm_split16bit,rA_num,rB_num);
1590 351 julius
             end
1591 860 olof
 
1592 351 julius
           `OR1200_OR32_MFSPR:
1593
             begin
1594 860 olof
                $fwrite(finsn,"l.mfspr r%0d,r%0d,0x%h",rD_num,rA_num,imm_16bit,);
1595
             end
1596 351 julius
 
1597
           `OR1200_OR32_MTSPR:
1598
             begin
1599 860 olof
                $fwrite(finsn,"l.mtspr r%0d,r%0d,0x%h",rA_num,rB_num,imm_split16bit);
1600 351 julius
             end
1601 860 olof
 
1602 351 julius
           `OR1200_OR32_MOVHI:
1603
             begin
1604
                if (!insn[16])
1605 449 julius
                  $fwrite(finsn,"l.movhi r%0d,0x%h",rD_num,imm_16bit);
1606 351 julius
                else
1607 449 julius
                  $fwrite(finsn,"l.macrc r%0d",rD_num);
1608 351 julius
             end
1609 860 olof
 
1610 351 julius
           `OR1200_OR32_ADDI:
1611
             begin
1612 449 julius
                $fwrite(finsn,"l.addi r%0d,r%0d,0x%h",rD_num,rA_num,imm_16bit);
1613 351 julius
             end
1614 860 olof
 
1615 351 julius
           `OR1200_OR32_ADDIC:
1616
             begin
1617 449 julius
                $fwrite(finsn,"l.addic r%0d,r%0d,0x%h",rD_num,rA_num,imm_16bit);
1618 351 julius
             end
1619 860 olof
 
1620 351 julius
           `OR1200_OR32_ANDI:
1621
             begin
1622 449 julius
                $fwrite(finsn,"l.andi r%0d,r%0d,0x%h",rD_num,rA_num,imm_16bit);
1623 860 olof
             end
1624
 
1625 351 julius
           `OR1200_OR32_ORI:
1626
             begin
1627 449 julius
                $fwrite(finsn,"l.ori r%0d,r%0d,0x%h",rD_num,rA_num,imm_16bit);
1628 860 olof
             end
1629 351 julius
 
1630
           `OR1200_OR32_XORI:
1631
             begin
1632 449 julius
                $fwrite(finsn,"l.xori r%0d,r%0d,0x%h",rD_num,rA_num,imm_16bit);
1633 860 olof
             end
1634 351 julius
 
1635
           `OR1200_OR32_MULI:
1636
             begin
1637 449 julius
                $fwrite(finsn,"l.muli r%0d,r%0d,0x%h",rD_num,rA_num,imm_16bit);
1638 351 julius
             end
1639 860 olof
 
1640 351 julius
           `OR1200_OR32_ALU:
1641
             begin
1642
                case(alu_op)
1643
                  `OR1200_ALUOP_ADD:
1644 860 olof
                    $fwrite(finsn,"l.add ");
1645 351 julius
                  `OR1200_ALUOP_ADDC:
1646 860 olof
                    $fwrite(finsn,"l.addc ");
1647 351 julius
                  `OR1200_ALUOP_SUB:
1648 860 olof
                    $fwrite(finsn,"l.sub ");
1649 351 julius
                  `OR1200_ALUOP_AND:
1650 860 olof
                    $fwrite(finsn,"l.and ");
1651 351 julius
                  `OR1200_ALUOP_OR:
1652 860 olof
                    $fwrite(finsn,"l.or ");
1653 351 julius
                  `OR1200_ALUOP_XOR:
1654 860 olof
                    $fwrite(finsn,"l.xor ");
1655 351 julius
                  `OR1200_ALUOP_MUL:
1656 860 olof
                    $fwrite(finsn,"l.mul ");
1657 351 julius
                  `OR1200_ALUOP_SHROT:
1658
                    begin
1659
                       case(shrot_op)
1660
                         `OR1200_SHROTOP_SLL:
1661 449 julius
                           $fwrite(finsn,"l.sll ");
1662 351 julius
                         `OR1200_SHROTOP_SRL:
1663 449 julius
                           $fwrite(finsn,"l.srl ");
1664 351 julius
                         `OR1200_SHROTOP_SRA:
1665 449 julius
                           $fwrite(finsn,"l.sra ");
1666 351 julius
                         `OR1200_SHROTOP_ROR:
1667 449 julius
                           $fwrite(finsn,"l.ror ");
1668 351 julius
                       endcase // case (shrot_op)
1669
                    end
1670
                  `OR1200_ALUOP_DIV:
1671 860 olof
                    $fwrite(finsn,"l.div ");
1672 351 julius
                  `OR1200_ALUOP_DIVU:
1673 860 olof
                    $fwrite(finsn,"l.divu ");
1674 351 julius
                  `OR1200_ALUOP_CMOV:
1675 860 olof
                    $fwrite(finsn,"l.cmov ");
1676 351 julius
                endcase // case (alu_op)
1677 449 julius
                $fwrite(finsn,"r%0d,r%0d,r%0d",rD_num,rA_num,rB_num);
1678 351 julius
             end
1679 860 olof
 
1680 351 julius
           `OR1200_OR32_SH_ROTI:
1681
             begin
1682
                case(shrot_op)
1683
                  `OR1200_SHROTOP_SLL:
1684 449 julius
                    $fwrite(finsn,"l.slli ");
1685 351 julius
                  `OR1200_SHROTOP_SRL:
1686 449 julius
                    $fwrite(finsn,"l.srli ");
1687 351 julius
                  `OR1200_SHROTOP_SRA:
1688 449 julius
                    $fwrite(finsn,"l.srai ");
1689 351 julius
                  `OR1200_SHROTOP_ROR:
1690 449 julius
                    $fwrite(finsn,"l.rori ");
1691 351 julius
                endcase // case (shrot_op)
1692 860 olof
                $fwrite(finsn,"r%0d,r%0d,0x%h",rD_num,rA_num,shroti_imm);
1693 351 julius
             end
1694 860 olof
 
1695 351 julius
           `OR1200_OR32_SFXXI:
1696
             begin
1697
                case(sf_op[2:0])
1698
                  `OR1200_COP_SFEQ:
1699 449 julius
                    $fwrite(finsn,"l.sfeqi ");
1700 351 julius
                  `OR1200_COP_SFNE:
1701 449 julius
                    $fwrite(finsn,"l.sfnei ");
1702 351 julius
                  `OR1200_COP_SFGT:
1703
                    begin
1704
                       if (sf_op[`OR1200_SIGNED_COMPARE])
1705 449 julius
                         $fwrite(finsn,"l.sfgtsi ");
1706 351 julius
                       else
1707 449 julius
                         $fwrite(finsn,"l.sfgtui ");
1708 351 julius
                    end
1709
                  `OR1200_COP_SFGE:
1710
                    begin
1711
                       if (sf_op[`OR1200_SIGNED_COMPARE])
1712 449 julius
                         $fwrite(finsn,"l.sfgesi ");
1713 351 julius
                       else
1714 449 julius
                         $fwrite(finsn,"l.sfgeui ");
1715 351 julius
                    end
1716
                  `OR1200_COP_SFLT:
1717
                    begin
1718
                       if (sf_op[`OR1200_SIGNED_COMPARE])
1719 449 julius
                         $fwrite(finsn,"l.sfltsi ");
1720 351 julius
                       else
1721 449 julius
                         $fwrite(finsn,"l.sfltui ");
1722 351 julius
                    end
1723
                  `OR1200_COP_SFLE:
1724
                    begin
1725
                       if (sf_op[`OR1200_SIGNED_COMPARE])
1726 449 julius
                         $fwrite(finsn,"l.sflesi ");
1727 351 julius
                       else
1728 449 julius
                         $fwrite(finsn,"l.sfleui ");
1729 860 olof
                    end
1730 351 julius
                endcase // case (sf_op[2:0])
1731 860 olof
 
1732 449 julius
                $fwrite(finsn,"r%0d,0x%h",rA_num, imm_16bit);
1733 860 olof
 
1734 351 julius
             end // case: `OR1200_OR32_SFXXI
1735
 
1736
           `OR1200_OR32_SFXX:
1737
             begin
1738
                case(sf_op[2:0])
1739
                  `OR1200_COP_SFEQ:
1740 449 julius
                    $fwrite(finsn,"l.sfeq ");
1741 351 julius
                  `OR1200_COP_SFNE:
1742 449 julius
                    $fwrite(finsn,"l.sfne ");
1743 351 julius
                  `OR1200_COP_SFGT:
1744
                    begin
1745
                       if (sf_op[`OR1200_SIGNED_COMPARE])
1746 449 julius
                         $fwrite(finsn,"l.sfgts ");
1747 351 julius
                       else
1748 449 julius
                         $fwrite(finsn,"l.sfgtu ");
1749 351 julius
                    end
1750
                  `OR1200_COP_SFGE:
1751
                    begin
1752
                       if (sf_op[`OR1200_SIGNED_COMPARE])
1753 449 julius
                         $fwrite(finsn,"l.sfges ");
1754 351 julius
                       else
1755 449 julius
                         $fwrite(finsn,"l.sfgeu ");
1756 351 julius
                    end
1757
                  `OR1200_COP_SFLT:
1758
                    begin
1759
                       if (sf_op[`OR1200_SIGNED_COMPARE])
1760 449 julius
                         $fwrite(finsn,"l.sflts ");
1761 351 julius
                       else
1762 449 julius
                         $fwrite(finsn,"l.sfltu ");
1763 351 julius
                    end
1764
                  `OR1200_COP_SFLE:
1765
                    begin
1766
                       if (sf_op[`OR1200_SIGNED_COMPARE])
1767 449 julius
                         $fwrite(finsn,"l.sfles ");
1768 351 julius
                       else
1769 449 julius
                         $fwrite(finsn,"l.sfleu ");
1770 351 julius
                    end
1771 860 olof
 
1772 351 julius
                endcase // case (sf_op[2:0])
1773 860 olof
 
1774 449 julius
                $fwrite(finsn,"r%0d,r%0d",rA_num, rB_num);
1775 860 olof
 
1776 351 julius
             end
1777 860 olof
 
1778 351 julius
           `OR1200_OR32_MACI:
1779
             begin
1780 449 julius
                $fwrite(finsn,"l.maci r%0d,0x%h",rA_num,imm_16bit);
1781 351 julius
             end
1782
 
1783
           `OR1200_OR32_MACMSB:
1784
             begin
1785
                if(insn[3:0] == 4'h1)
1786 860 olof
                  $fwrite(finsn,"l.mac ");
1787 351 julius
                else if(insn[3:0] == 4'h2)
1788 449 julius
                  $fwrite(finsn,"l.msb ");
1789 860 olof
 
1790 449 julius
                $fwrite(finsn,"r%0d,r%0d",rA_num,rB_num);
1791 351 julius
             end
1792
 
1793
           `OR1200_OR32_NOP:
1794
             begin
1795 449 julius
                $fwrite(finsn,"l.nop 0x%0h",imm_16bit);
1796 351 julius
             end
1797 860 olof
 
1798 351 julius
           `OR1200_OR32_XSYNC:
1799
             begin
1800
                case (xsync_op)
1801
                  5'd0:
1802 449 julius
                    $fwrite(finsn,"l.sys 0x%h",imm_16bit);
1803 351 julius
                  5'd8:
1804 449 julius
                    $fwrite(finsn,"l.trap 0x%h",imm_16bit);
1805 351 julius
                  5'd16:
1806 449 julius
                    $fwrite(finsn,"l.msync");
1807 351 julius
                  5'd20:
1808 449 julius
                    $fwrite(finsn,"l.psync");
1809 351 julius
                  5'd24:
1810 449 julius
                    $fwrite(finsn,"l.csync");
1811 351 julius
                  default:
1812
                    begin
1813
                       $display("%t: Instruction with opcode 0x%h has bad specific type information: 0x%h",$time,opcode,insn);
1814 449 julius
                       $fwrite(finsn,"%t: Instruction with opcode 0x%h has has bad specific type information: 0x%h",$time,opcode,insn);
1815 351 julius
                    end
1816
                endcase // case (xsync_op)
1817
             end
1818 860 olof
 
1819 351 julius
           default:
1820
             begin
1821
                $display("%t: Unknown opcode 0x%h",$time,opcode);
1822 449 julius
                $fwrite(finsn,"%t: Unknown opcode 0x%h",$time,opcode);
1823 351 julius
             end
1824 860 olof
 
1825 351 julius
         endcase // case (opcode)
1826 860 olof
 
1827 351 julius
      end
1828
   endtask // or1200_print_op
1829
 
1830
 
1831 860 olof
 
1832 6 julius
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.