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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [orpsocv2/] [boards/] [generic/] [ft/] [bench/] [verilog/] [or1200_monitor.v] - Blame information for rev 483

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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