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

Subversion Repositories amber

[/] [amber/] [trunk/] [hw/] [vlog/] [amber23/] [a23_decompile.v] - Blame information for rev 63

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

Line No. Rev Author Line
1 2 csantifort
//////////////////////////////////////////////////////////////////
2
//                                                              //
3
// Decompiler for Amber 2 Core                                  //
4
//                                                              //
5
//  This file is part of the Amber project                      //
6
//  http://www.opencores.org/project,amber                      //
7
//                                                              //
8
//  Description                                                 //
9
//  Decompiler for debugging core - not synthesizable           //
10
//  Shows instruction in Execute Stage at last clock of         //
11
//  the instruction                                             //
12
//                                                              //
13
//  Author(s):                                                  //
14
//      - Conor Santifort, csantifort.amber@gmail.com           //
15
//                                                              //
16
//////////////////////////////////////////////////////////////////
17
//                                                              //
18
// Copyright (C) 2010 Authors and OPENCORES.ORG                 //
19
//                                                              //
20
// This source file may be used and distributed without         //
21
// restriction provided that this copyright statement is not    //
22
// removed from the file and that any derivative work contains  //
23
// the original copyright notice and the associated disclaimer. //
24
//                                                              //
25
// This source file is free software; you can redistribute it   //
26
// and/or modify it under the terms of the GNU Lesser General   //
27
// Public License as published by the Free Software Foundation; //
28
// either version 2.1 of the License, or (at your option) any   //
29
// later version.                                               //
30
//                                                              //
31
// This source is distributed in the hope that it will be       //
32
// useful, but WITHOUT ANY WARRANTY; without even the implied   //
33
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
34
// PURPOSE.  See the GNU Lesser General Public License for more //
35
// details.                                                     //
36
//                                                              //
37
// You should have received a copy of the GNU Lesser General    //
38
// Public License along with this source; if not, download it   //
39
// from http://www.opencores.org/lgpl.shtml                     //
40
//                                                              //
41
//////////////////////////////////////////////////////////////////
42 63 csantifort
`include "global_defines.v"
43 15 csantifort
`include "a23_config_defines.v"
44 2 csantifort
 
45 15 csantifort
module a23_decompile
46 2 csantifort
(
47
input                       i_clk,
48
input                       i_fetch_stall,
49
input       [31:0]          i_instruction,
50
input                       i_instruction_valid,
51
input                       i_instruction_undefined,
52
input                       i_instruction_execute,
53
input       [2:0]           i_interrupt,            // non-zero value means interrupt triggered
54
input                       i_interrupt_state,
55
input       [31:0]          i_instruction_address,
56
input       [1:0]           i_pc_sel,
57
input                       i_pc_wen
58
 
59
);
60
 
61 15 csantifort
`include "a23_localparams.v"
62 2 csantifort
 
63 15 csantifort
`ifdef A23_DECOMPILE
64 2 csantifort
 
65
integer i;
66
 
67
wire    [31:0]         imm32;
68
wire    [7:0]          imm8;
69
wire    [11:0]         offset12;
70
wire    [7:0]          offset8;
71
wire    [3:0]          reg_n, reg_d, reg_m, reg_s;
72
wire    [4:0]          shift_imm;
73
wire    [3:0]          opcode;
74
wire    [3:0]          condition;
75
wire    [3:0]          type;
76
wire                   opcode_compare;
77
wire                   opcode_move;
78
wire                   no_shift;
79
wire                   shift_op_imm;
80
wire    [1:0]          mtrans_type;
81
wire                   s_bit;
82
 
83
reg     [(5*8)-1:0]    xINSTRUCTION_EXECUTE;
84
reg     [(5*8)-1:0]    xINSTRUCTION_EXECUTE_R = "---   ";
85
wire    [(8*8)-1:0]    TYPE_NAME;
86
reg     [3:0]          fchars;
87
reg     [31:0]         execute_address = 'd0;
88
reg     [2:0]          interrupt_d1;
89
reg     [31:0]         execute_instruction = 'd0;
90
reg                    execute_now = 'd0;
91
reg                    execute_valid = 'd0;
92
reg                    execute_undefined = 'd0;
93
 
94
 
95
// ========================================================
96
// Delay instruction to Execute stage
97
// ========================================================
98
always @( posedge i_clk )
99
    if ( !i_fetch_stall && i_instruction_valid )
100
        begin
101
        execute_instruction <= i_instruction;
102
        execute_address     <= i_instruction_address;
103
        execute_undefined   <= i_instruction_undefined;
104
        execute_now         <= 1'd1;
105
        end
106
    else
107
        execute_now         <= 1'd0;
108
 
109
 
110
always @ ( posedge i_clk )
111
    if ( !i_fetch_stall )
112
        execute_valid <= i_instruction_valid;
113
 
114
// ========================================================
115
// Open File
116
// ========================================================
117
integer decompile_file;
118
 
119
initial
120 15 csantifort
    #1 decompile_file = $fopen(`A23_DECOMPILE_FILE, "w");
121 2 csantifort
 
122
 
123
// ========================================================
124
// Fields within the instruction
125
// ========================================================
126
assign opcode      = execute_instruction[24:21];
127
assign condition   = execute_instruction[31:28];
128
assign s_bit       = execute_instruction[20];
129
assign reg_n       = execute_instruction[19:16];
130
assign reg_d       = execute_instruction[15:12];
131
assign reg_m       = execute_instruction[3:0];
132
assign reg_s       = execute_instruction[11:8];
133
assign shift_imm   = execute_instruction[11:7];
134
assign offset12    = execute_instruction[11:0];
135
assign offset8     = {execute_instruction[11:8], execute_instruction[3:0]};
136
assign imm8        = execute_instruction[7:0];
137
 
138
assign no_shift    = execute_instruction[11:4] == 8'h0;
139
assign mtrans_type = execute_instruction[24:23];
140
 
141
 
142
assign opcode_compare =
143
            opcode == CMP ||
144
            opcode == CMN ||
145
            opcode == TEQ ||
146
            opcode == TST ;
147
 
148
assign opcode_move =
149
            opcode == MOV ||
150
            opcode == MVN ;
151
 
152
assign shift_op_imm = type == REGOP && execute_instruction[25] == 1'd1;
153
 
154
assign imm32 =  execute_instruction[11:8] == 4'h0 ? {            24'h0, imm8[7:0] } :
155
                execute_instruction[11:8] == 4'h1 ? { imm8[1:0], 24'h0, imm8[7:2] } :
156
                execute_instruction[11:8] == 4'h2 ? { imm8[3:0], 24'h0, imm8[7:4] } :
157
                execute_instruction[11:8] == 4'h3 ? { imm8[5:0], 24'h0, imm8[7:6] } :
158
                execute_instruction[11:8] == 4'h4 ? { imm8[7:0], 24'h0            } :
159
                execute_instruction[11:8] == 4'h5 ? { 2'h0,  imm8[7:0], 22'h0 }     :
160
                execute_instruction[11:8] == 4'h6 ? { 4'h0,  imm8[7:0], 20'h0 }     :
161
                execute_instruction[11:8] == 4'h7 ? { 6'h0,  imm8[7:0], 18'h0 }     :
162
                execute_instruction[11:8] == 4'h8 ? { 8'h0,  imm8[7:0], 16'h0 }     :
163
                execute_instruction[11:8] == 4'h9 ? { 10'h0, imm8[7:0], 14'h0 }     :
164
                execute_instruction[11:8] == 4'ha ? { 12'h0, imm8[7:0], 12'h0 }     :
165
                execute_instruction[11:8] == 4'hb ? { 14'h0, imm8[7:0], 10'h0 }     :
166
                execute_instruction[11:8] == 4'hc ? { 16'h0, imm8[7:0], 8'h0  }     :
167
                execute_instruction[11:8] == 4'hd ? { 18'h0, imm8[7:0], 6'h0  }     :
168
                execute_instruction[11:8] == 4'he ? { 20'h0, imm8[7:0], 4'h0  }     :
169
                                                    { 22'h0, imm8[7:0], 2'h0  }     ;
170
 
171
 
172
// ========================================================
173
// Instruction decode
174
// ========================================================
175
// the order of these matters
176
assign type =
177
    {execute_instruction[27:23], execute_instruction[21:20], execute_instruction[11:4] } == { 5'b00010, 2'b00, 8'b00001001 } ? SWAP     :  // Before REGOP
178
    {execute_instruction[27:22], execute_instruction[7:4]                              } == { 6'b000000, 4'b1001           } ? MULT     :  // Before REGOP
179
    {execute_instruction[27:26]                                                        } == { 2'b00                        } ? REGOP    :
180
    {execute_instruction[27:26]                                                        } == { 2'b01                        } ? TRANS    :
181
    {execute_instruction[27:25]                                                        } == { 3'b100                       } ? MTRANS   :
182
    {execute_instruction[27:25]                                                        } == { 3'b101                       } ? BRANCH   :
183
    {execute_instruction[27:25]                                                        } == { 3'b110                       } ? CODTRANS :
184
    {execute_instruction[27:24], execute_instruction[4]                                } == { 4'b1110, 1'b0                } ? COREGOP  :
185
    {execute_instruction[27:24], execute_instruction[4]                                } == { 4'b1110, 1'b1                } ? CORTRANS :
186
                                                                                                                               SWI      ;
187
 
188
 
189
//
190
// Convert some important signals to ASCII
191
// so their values can easily be displayed on a waveform viewer
192
//
193
assign TYPE_NAME    = type == REGOP    ? "REGOP   " :
194
                      type == MULT     ? "MULT    " :
195
                      type == SWAP     ? "SWAP    " :
196
                      type == TRANS    ? "TRANS   " :
197
                      type == MTRANS   ? "MTRANS  " :
198
                      type == BRANCH   ? "BRANCH  " :
199
                      type == CODTRANS ? "CODTRANS" :
200
                      type == COREGOP  ? "COREGOP " :
201
                      type == CORTRANS ? "CORTRANS" :
202
                      type == SWI      ? "SWI     " :
203
                                         "UNKNOWN " ;
204
 
205
 
206
always @*
207
    begin
208
 
209
    if ( !execute_now )
210
        begin
211
        xINSTRUCTION_EXECUTE =  xINSTRUCTION_EXECUTE_R;
212
        end // stalled
213
 
214
    else if ( type == REGOP    && opcode == ADC                                                          ) xINSTRUCTION_EXECUTE = "adc  ";
215
    else if ( type == REGOP    && opcode == ADD                                                          ) xINSTRUCTION_EXECUTE = "add  ";
216
    else if ( type == REGOP    && opcode == AND                                                          ) xINSTRUCTION_EXECUTE = "and  ";
217
    else if ( type == BRANCH   && execute_instruction[24] == 1'b0                                        ) xINSTRUCTION_EXECUTE = "b    ";
218
    else if ( type == REGOP    && opcode == BIC                                                          ) xINSTRUCTION_EXECUTE = "bic  ";
219
    else if ( type == BRANCH   && execute_instruction[24] == 1'b1                                        ) xINSTRUCTION_EXECUTE = "bl   ";
220
    else if ( type == COREGOP                                                                            ) xINSTRUCTION_EXECUTE = "cdp  ";
221
    else if ( type == REGOP    && opcode == CMN                                                          ) xINSTRUCTION_EXECUTE = "cmn  ";
222
    else if ( type == REGOP    && opcode == CMP                                                          ) xINSTRUCTION_EXECUTE = "cmp  ";
223
    else if ( type == REGOP    && opcode == EOR                                                          ) xINSTRUCTION_EXECUTE = "eor  ";
224
    else if ( type == CODTRANS && execute_instruction[20] == 1'b1                                        ) xINSTRUCTION_EXECUTE = "ldc  ";
225
    else if ( type == MTRANS   && execute_instruction[20] == 1'b1                                        ) xINSTRUCTION_EXECUTE = "ldm  ";
226
    else if ( type == TRANS    && {execute_instruction[22],execute_instruction[20]}    == {1'b0, 1'b1}   ) xINSTRUCTION_EXECUTE = "ldr  ";
227
    else if ( type == TRANS    && {execute_instruction[22],execute_instruction[20]}    == {1'b1, 1'b1}   ) xINSTRUCTION_EXECUTE = "ldrb ";
228
    else if ( type == CORTRANS && execute_instruction[20] == 1'b0                                        ) xINSTRUCTION_EXECUTE = "mcr  ";
229
    else if ( type == MULT     && execute_instruction[21] == 1'b1                                        ) xINSTRUCTION_EXECUTE = "mla  ";
230
    else if ( type == REGOP    && opcode == MOV                                                          ) xINSTRUCTION_EXECUTE = "mov  ";
231
    else if ( type == CORTRANS && execute_instruction[20] == 1'b1                                        ) xINSTRUCTION_EXECUTE = "mrc  ";
232
    else if ( type == MULT     && execute_instruction[21] == 1'b0                                        ) xINSTRUCTION_EXECUTE = "mul  ";
233
    else if ( type == REGOP    && opcode == MVN                                                          ) xINSTRUCTION_EXECUTE = "mvn  ";
234
    else if ( type == REGOP    && opcode == ORR                                                          ) xINSTRUCTION_EXECUTE = "orr  ";
235
    else if ( type == REGOP    && opcode == RSB                                                          ) xINSTRUCTION_EXECUTE = "rsb  ";
236
    else if ( type == REGOP    && opcode == RSC                                                          ) xINSTRUCTION_EXECUTE = "rsc  ";
237
    else if ( type == REGOP    && opcode == SBC                                                          ) xINSTRUCTION_EXECUTE = "sbc  ";
238
    else if ( type == CODTRANS && execute_instruction[20] == 1'b0                                        ) xINSTRUCTION_EXECUTE = "stc  ";
239
    else if ( type == MTRANS   && execute_instruction[20] == 1'b0                                        ) xINSTRUCTION_EXECUTE = "stm  ";
240
    else if ( type == TRANS    && {execute_instruction[22],execute_instruction[20]}    == {1'b0, 1'b0}   ) xINSTRUCTION_EXECUTE = "str  ";
241
    else if ( type == TRANS    && {execute_instruction[22],execute_instruction[20]}    == {1'b1, 1'b0}   ) xINSTRUCTION_EXECUTE = "strb ";
242
    else if ( type == REGOP    && opcode == SUB                                                          ) xINSTRUCTION_EXECUTE = "sub  ";
243
    else if ( type == SWI                                                                                ) xINSTRUCTION_EXECUTE = "swi  ";
244
    else if ( type == SWAP     && execute_instruction[22] == 1'b0                                        ) xINSTRUCTION_EXECUTE = "swp  ";
245
    else if ( type == SWAP     && execute_instruction[22] == 1'b1                                        ) xINSTRUCTION_EXECUTE = "swpb ";
246
    else if ( type == REGOP    && opcode == TEQ                                                          ) xINSTRUCTION_EXECUTE = "teq  ";
247
    else if ( type == REGOP    && opcode == TST                                                          ) xINSTRUCTION_EXECUTE = "tst  ";
248
    else                                                                                                   xINSTRUCTION_EXECUTE = "unkow";
249
    end
250
 
251
always @ ( posedge i_clk )
252
    xINSTRUCTION_EXECUTE_R <= xINSTRUCTION_EXECUTE;
253
 
254
always @( posedge i_clk )
255
    if ( execute_now )
256
        begin
257
 
258
            // Interrupts override instructions that are just starting
259
        if ( interrupt_d1 == 3'd0 || interrupt_d1 == 3'd7 )
260
            begin
261 58 csantifort
            $fwrite(decompile_file,"%09d  ", `U_TB.clk_count);
262 2 csantifort
 
263
            // Right justify the address
264
            if      ( execute_address < 32'h10)        $fwrite(decompile_file,"       %01x:  ", {execute_address[ 3:1], 1'd0});
265
            else if ( execute_address < 32'h100)       $fwrite(decompile_file,"      %02x:  ",  {execute_address[ 7:1], 1'd0});
266
            else if ( execute_address < 32'h1000)      $fwrite(decompile_file,"     %03x:  ",   {execute_address[11:1], 1'd0});
267
            else if ( execute_address < 32'h10000)     $fwrite(decompile_file,"    %04x:  ",    {execute_address[15:1], 1'd0});
268
            else if ( execute_address < 32'h100000)    $fwrite(decompile_file,"   %05x:  ",     {execute_address[19:1], 1'd0});
269
            else if ( execute_address < 32'h1000000)   $fwrite(decompile_file,"  %06x:  ",      {execute_address[23:1], 1'd0});
270
            else if ( execute_address < 32'h10000000)  $fwrite(decompile_file," %07x:  ",       {execute_address[27:1], 1'd0});
271
            else                                       $fwrite(decompile_file,"%8x:  ",         {execute_address[31:1], 1'd0});
272
 
273
            // Mark that the instruction is not being executed 
274
            // condition field in execute stage allows instruction to execute ?
275
            if (!i_instruction_execute)
276
                begin
277
                $fwrite(decompile_file,"-");
278
                if ( type == SWI )
279 58 csantifort
                    $display ("Cycle %09d  SWI not taken *************", `U_TB.clk_count);
280 2 csantifort
                end
281
            else
282
                $fwrite(decompile_file," ");
283
 
284
            // ========================================
285
            // print the instruction name
286
            // ========================================
287
            case (numchars( xINSTRUCTION_EXECUTE ))
288
                4'd1: $fwrite(decompile_file,"%s", xINSTRUCTION_EXECUTE[39:32] );
289
                4'd2: $fwrite(decompile_file,"%s", xINSTRUCTION_EXECUTE[39:24] );
290
                4'd3: $fwrite(decompile_file,"%s", xINSTRUCTION_EXECUTE[39:16] );
291
                4'd4: $fwrite(decompile_file,"%s", xINSTRUCTION_EXECUTE[39: 8] );
292
            default:  $fwrite(decompile_file,"%s", xINSTRUCTION_EXECUTE[39: 0] );
293
            endcase
294
 
295
            fchars = 8 - numchars(xINSTRUCTION_EXECUTE);
296
 
297
            // Print the Multiple transfer type
298
            if (type   == MTRANS )
299
                begin
300
                w_mtrans_type;
301
                fchars = fchars - 2;
302
                end
303
 
304
            // Print the s bit
305
           if ( ((type == REGOP && !opcode_compare) || type == MULT ) && s_bit == 1'b1 )
306
                begin
307
                $fwrite(decompile_file,"s");
308
                fchars = fchars - 1;
309
                end
310
 
311
            // Print the p bit
312
           if ( type == REGOP && opcode_compare && s_bit == 1'b1 && reg_d == 4'd15 )
313
                begin
314
                $fwrite(decompile_file,"p");
315
                fchars = fchars - 1;
316
                end
317
 
318
            // Print the condition code
319
            if ( condition != AL )
320
                begin
321
                wcond;
322
                fchars = fchars - 2;
323
                end
324
 
325
            // Align spaces after instruction    
326
            case ( fchars )
327
                4'd0: $fwrite(decompile_file,"");
328
                4'd1: $fwrite(decompile_file," ");
329
                4'd2: $fwrite(decompile_file,"  ");
330
                4'd3: $fwrite(decompile_file,"   ");
331
                4'd4: $fwrite(decompile_file,"    ");
332
                4'd5: $fwrite(decompile_file,"     ");
333
                4'd6: $fwrite(decompile_file,"      ");
334
                4'd7: $fwrite(decompile_file,"       ");
335
                4'd8: $fwrite(decompile_file,"        ");
336
            default:  $fwrite(decompile_file,"         ");
337
            endcase
338
 
339
            // ========================================
340
            // print the arguments for the instruction
341
            // ========================================
342
            case ( type )
343
                REGOP:     regop_args;
344
                TRANS:     trans_args;
345
                MTRANS:    mtrans_args;
346
                BRANCH:    branch_args;
347
                MULT:      mult_args;
348
                SWAP:      swap_args;
349
                CODTRANS:  codtrans_args;
350
                COREGOP:   begin
351
                           // `TB_ERROR_MESSAGE
352
                           $write("Coregop not implemented in decompiler yet\n");
353
                           end
354
                CORTRANS:  cortrans_args;
355
                SWI:       $fwrite(decompile_file,"#0x%06h", execute_instruction[23:0]);
356
                default: begin
357
                         `TB_ERROR_MESSAGE
358
                         $write("Unknown Instruction Type ERROR\n");
359
                         end
360
            endcase
361
 
362
            $fwrite( decompile_file,"\n" );
363
            end
364
 
365
        // Undefined Instruction Interrupts    
366
        if ( i_instruction_execute && execute_undefined )
367
            begin
368 58 csantifort
            $fwrite( decompile_file,"%09d              interrupt undefined instruction", `U_TB.clk_count );
369 2 csantifort
            $fwrite( decompile_file,", return addr " );
370
            $fwrite( decompile_file,"%08x\n",  pcf(get_reg_val(5'd21)-4'd4) );
371
            end
372
 
373
        // Software Interrupt  
374
        if ( i_instruction_execute && type == SWI )
375
            begin
376 58 csantifort
            $fwrite( decompile_file,"%09d              interrupt swi", `U_TB.clk_count );
377 2 csantifort
            $fwrite( decompile_file,", return addr " );
378
            $fwrite( decompile_file,"%08x\n",  pcf(get_reg_val(5'd21)-4'd4) );
379
            end
380
        end
381
 
382
 
383
always @( posedge i_clk )
384
    if ( !i_fetch_stall )
385
        begin
386
        interrupt_d1 <= i_interrupt;
387
 
388
        // Asynchronous Interrupts    
389
        if ( interrupt_d1 != 3'd0 && i_interrupt_state )
390
            begin
391 58 csantifort
            $fwrite( decompile_file,"%09d              interrupt ", `U_TB.clk_count );
392 2 csantifort
            case ( interrupt_d1 )
393
                3'd1:    $fwrite( decompile_file,"data abort" );
394
                3'd2:    $fwrite( decompile_file,"firq" );
395
                3'd3:    $fwrite( decompile_file,"irq" );
396
                3'd4:    $fwrite( decompile_file,"address exception" );
397
                3'd5:    $fwrite( decompile_file,"instruction abort" );
398
                default: $fwrite( decompile_file,"unknown type" );
399
            endcase
400
            $fwrite( decompile_file,", return addr " );
401
 
402
            case ( interrupt_d1 )
403
                3'd1:    $fwrite(decompile_file,"%08h\n",  pcf(get_reg_val(5'd16)));
404
                3'd2:    $fwrite(decompile_file,"%08h\n",  pcf(get_reg_val(5'd17)));
405
                3'd3:    $fwrite(decompile_file,"%08h\n",  pcf(get_reg_val(5'd18)));
406
                3'd4:    $fwrite(decompile_file,"%08h\n",  pcf(get_reg_val(5'd19)));
407
                3'd5:    $fwrite(decompile_file,"%08h\n",  pcf(get_reg_val(5'd19)));
408
                3'd7:    $fwrite(decompile_file,"%08h\n",  pcf(get_reg_val(5'd20)));
409
                default: ;
410
            endcase
411
            end
412
        end
413
 
414
 
415
// jump
416
// Dont print a jump message for interrupts
417
always @( posedge i_clk )
418
        if (
419
             i_pc_sel != 2'd0 &&
420
             i_pc_wen &&
421
             !i_fetch_stall &&
422
             i_instruction_execute &&
423
             i_interrupt == 3'd0 &&
424
             !execute_undefined &&
425
             type != SWI &&
426
             execute_address != get_32bit_signal(0)  // Don't print jump to same address
427
             )
428
            begin
429 58 csantifort
            $fwrite(decompile_file,"%09d              jump    from ", `U_TB.clk_count);
430 2 csantifort
            fwrite_hex_drop_zeros(decompile_file,  pcf(execute_address));
431
            $fwrite(decompile_file," to ");
432
            fwrite_hex_drop_zeros(decompile_file,  pcf(get_32bit_signal(0)) ); // u_execute.pc_nxt
433
            $fwrite(decompile_file,", r0 %08h, ",  get_reg_val ( 5'd0 ));
434
            $fwrite(decompile_file,"r1 %08h\n",    get_reg_val ( 5'd1 ));
435
            end
436
 
437
// =================================================================================
438
// Memory Writes - Peek into fetch module
439
// =================================================================================
440
 
441
reg [31:0] tmp_address;
442
 
443
    // Data access
444
always @( posedge i_clk )
445
    // Data Write    
446
    if ( get_1bit_signal(0) && !get_1bit_signal(1) )
447
        begin
448
 
449 58 csantifort
        $fwrite(decompile_file, "%09d              write   addr ", `U_TB.clk_count);
450 2 csantifort
        tmp_address = get_32bit_signal(2);
451
        fwrite_hex_drop_zeros(decompile_file, {tmp_address [31:2], 2'd0} );
452
 
453
        $fwrite(decompile_file, ", data %08h, be %h",
454
                get_32bit_signal(3),    // u_cache.i_write_data
455
                get_4bit_signal (0));   // u_cache.i_byte_enable
456
 
457
        if ( get_1bit_signal(2) ) // Abort! address translation failed
458
            $fwrite(decompile_file, " aborted!\n");
459
        else
460
            $fwrite(decompile_file, "\n");
461
        end
462
 
463
    // Data Read    
464
    else if (get_1bit_signal(3) && !get_1bit_signal(0)  && !get_1bit_signal(1))
465
        begin
466
 
467 58 csantifort
        $fwrite(decompile_file, "%09d              read    addr ", `U_TB.clk_count);
468 2 csantifort
        tmp_address = get_32bit_signal(2);
469
        fwrite_hex_drop_zeros(decompile_file, {tmp_address[31:2], 2'd0} );
470
 
471
        $fwrite(decompile_file, ", data %08h", get_32bit_signal(4));  // u_decode.i_read_data
472
 
473
        if ( get_1bit_signal(2) ) // Abort! address translation failed
474
            $fwrite(decompile_file, " aborted!\n");
475
        else
476
            $fwrite(decompile_file, "\n");
477
        end
478
 
479
 
480
// =================================================================================
481
// Tasks
482
// =================================================================================
483
 
484
// Write Condition field
485
task wcond;
486
    begin
487
    case( condition)
488
        4'h0:    $fwrite(decompile_file,"eq");
489
        4'h1:    $fwrite(decompile_file,"ne");
490
        4'h2:    $fwrite(decompile_file,"cs");
491
        4'h3:    $fwrite(decompile_file,"cc");
492
        4'h4:    $fwrite(decompile_file,"mi");
493
        4'h5:    $fwrite(decompile_file,"pl");
494
        4'h6:    $fwrite(decompile_file,"vs");
495
        4'h7:    $fwrite(decompile_file,"vc");
496
        4'h8:    $fwrite(decompile_file,"hi");
497
        4'h9:    $fwrite(decompile_file,"ls");
498
        4'ha:    $fwrite(decompile_file,"ge");
499
        4'hb:    $fwrite(decompile_file,"lt");
500
        4'hc:    $fwrite(decompile_file,"gt");
501
        4'hd:    $fwrite(decompile_file,"le");
502
        4'he:    $fwrite(decompile_file,"  ");  // Always
503
        default: $fwrite(decompile_file,"nv");  // Never
504
    endcase
505
    end
506
endtask
507
 
508
// ldm and stm types
509
task w_mtrans_type;
510
    begin
511
    case( mtrans_type )
512
        4'h0:    $fwrite(decompile_file,"da");
513
        4'h1:    $fwrite(decompile_file,"ia");
514
        4'h2:    $fwrite(decompile_file,"db");
515
        4'h3:    $fwrite(decompile_file,"ib");
516
        default: $fwrite(decompile_file,"xx");
517
    endcase
518
    end
519
endtask
520
 
521 15 csantifort
// e.g. mrc     15, 0, r9, cr0, cr0, {0}
522 2 csantifort
task cortrans_args;
523
    begin
524
    // Co-Processor Number
525 15 csantifort
    $fwrite(decompile_file,"%1d, ", execute_instruction[11:8]);
526 2 csantifort
    // opcode1
527
    $fwrite(decompile_file,"%1d, ", execute_instruction[23:21]);
528
    // Rd [15:12]
529
    warmreg(reg_d);
530
    // CRn [19:16]
531 15 csantifort
    $fwrite(decompile_file,", cr%1d", execute_instruction[19:16]);
532 2 csantifort
    // CRm [3:0]
533 15 csantifort
    $fwrite(decompile_file,", cr%1d", execute_instruction[3:0]);
534 2 csantifort
    // Opcode2 [7:5]
535 15 csantifort
    $fwrite(decompile_file,", {%1d}",   execute_instruction[7:5]);
536 2 csantifort
    end
537
endtask
538
 
539
 
540
// ldc  15, 0, r9, cr0, cr0, {0}
541
task codtrans_args;
542
    begin
543
    // Co-Processor Number
544 15 csantifort
    $fwrite(decompile_file,"%1d, ", execute_instruction[11:8]);
545 2 csantifort
    // CRd [15:12]
546 15 csantifort
    $fwrite(decompile_file,"cr%1d, ", execute_instruction[15:12]);
547 2 csantifort
    // Rd [19:16]
548
    warmreg(reg_n);
549
    end
550
endtask
551
 
552
 
553
task branch_args;
554
reg [31:0] shift_amount;
555
    begin
556
    if (execute_instruction[23]) // negative
557
        shift_amount = {~execute_instruction[23:0] + 24'd1, 2'd0};
558
    else
559
        shift_amount = {execute_instruction[23:0], 2'd0};
560
 
561
    if (execute_instruction[23]) // negative
562
        fwrite_hex_drop_zeros ( decompile_file, get_reg_val( 5'd21 ) - shift_amount );
563
    else
564
        fwrite_hex_drop_zeros ( decompile_file, get_reg_val( 5'd21 ) + shift_amount );
565
    end
566
endtask
567
 
568
 
569
task mult_args;
570
    begin
571
    warmreg(reg_n);  // Rd is in the Rn position for MULT instructions
572
    $fwrite(decompile_file,", ");
573
    warmreg(reg_m);
574
    $fwrite(decompile_file,", ");
575
    warmreg(reg_s);
576
 
577
    if (execute_instruction[21]) // MLA
578
        begin
579
        $fwrite(decompile_file,", ");
580
        warmreg(reg_d);
581
        end
582
    end
583
endtask
584
 
585
 
586
task swap_args;
587
    begin
588
    warmreg(reg_d);
589
    $fwrite(decompile_file,", ");
590
    warmreg(reg_m);
591
    $fwrite(decompile_file,", [");
592
    warmreg(reg_n);
593
    $fwrite(decompile_file,"]");
594
    end
595
endtask
596
 
597
 
598
task regop_args;
599
    begin
600
    if (!opcode_compare)
601
        warmreg(reg_d);
602
 
603
    if (!opcode_move )
604
        begin
605
        if (!opcode_compare)
606
            begin
607
            $fwrite(decompile_file,", ");
608
            if (reg_d < 4'd10 || reg_d > 4'd12)
609
                $fwrite(decompile_file," ");
610
            end
611
        warmreg(reg_n);
612
        $fwrite(decompile_file,", ");
613
        if (reg_n < 4'd10 || reg_n > 4'd12)
614
            $fwrite(decompile_file," ");
615
        end
616
    else
617
        begin
618
        $fwrite(decompile_file,", ");
619
        if (reg_d < 4'd10 || reg_d > 4'd12)
620
            $fwrite(decompile_file," ");
621
        end
622
 
623
    if (shift_op_imm)
624
        begin
625
        if (|imm32[31:15])
626
            $fwrite(decompile_file,"#0x%08h", imm32);
627
        else
628
            $fwrite(decompile_file,"#%1d", imm32);
629
        end
630
    else // Rm
631
        begin
632
        warmreg(reg_m);
633
        if (execute_instruction[4])
634
            // Register Shifts
635
            wshiftreg;
636
        else
637
            // Immediate shifts
638
            wshift;
639
        end
640
    end
641
endtask
642
 
643
 
644
task trans_args;
645
    begin
646
    warmreg(reg_d);   // Destination register
647
 
648
    casez ({execute_instruction[25:23], execute_instruction[21], no_shift, offset12==12'd0})
649
           6'b0100?0 : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,", #-%1d]" , offset12); end
650
           6'b0110?0 : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,", #%1d]"  , offset12); end
651
           6'b0100?1 : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,"]"); end
652
           6'b0110?1 : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,"]"); end
653
           6'b0101?? : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,", #-%1d]!", offset12); end
654
           6'b0111?? : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,", #%1d]!" , offset12); end
655
 
656
           6'b0000?0 : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,"], #-%1d", offset12); end
657
           6'b0010?0 : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,"], #%1d" , offset12); end
658
           6'b0001?0 : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,"], #-%1d", offset12); end
659
           6'b0011?0 : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,"], #%1d" , offset12); end
660
 
661
           6'b0000?1 : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,"]"); end
662
           6'b0010?1 : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,"]"); end
663
           6'b0001?1 : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,"]"); end
664
           6'b0011?1 : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,"]"); end
665
 
666
           6'b11001? : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,", -");  warmreg(reg_m); $fwrite(decompile_file,"]");  end
667
           6'b11101? : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,", ");   warmreg(reg_m); $fwrite(decompile_file,"]");  end
668
           6'b11011? : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,", -");  warmreg(reg_m); $fwrite(decompile_file,"]!"); end
669
           6'b11111? : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,", ");   warmreg(reg_m); $fwrite(decompile_file,"]!"); end
670
 
671
           6'b10001? : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,"], -"); warmreg(reg_m);  end
672
           6'b10101? : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,"], ");  warmreg(reg_m);  end
673
           6'b10011? : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,"], -"); warmreg(reg_m);  end
674
           6'b10111? : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,"], ");  warmreg(reg_m);  end
675
 
676
           6'b11000? : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,", -");  warmreg(reg_m); wshift; $fwrite(decompile_file,"]"); end
677
           6'b11100? : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,", ");   warmreg(reg_m); wshift; $fwrite(decompile_file,"]"); end
678
           6'b11010? : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,", -");  warmreg(reg_m); wshift; $fwrite(decompile_file,"]!");end
679
           6'b11110? : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,", ");   warmreg(reg_m); wshift; $fwrite(decompile_file,"]!");end
680
 
681
           6'b10000? : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,"], -"); warmreg(reg_m); wshift; end
682
           6'b10100? : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,"], ");  warmreg(reg_m); wshift; end
683
           6'b10010? : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,"], -"); warmreg(reg_m); wshift; end
684
           6'b10110? : begin $fwrite(decompile_file,", ["); warmreg(reg_n); $fwrite(decompile_file,"], ");  warmreg(reg_m); wshift; end
685
 
686
    endcase
687
    end
688
endtask
689
 
690
 
691
task mtrans_args;
692
    begin
693
    warmreg(reg_n);
694
    if (execute_instruction[21]) $fwrite(decompile_file,"!");
695
    $fwrite(decompile_file,", {");
696
    for (i=0;i<16;i=i+1)
697
        if (execute_instruction[i])
698
            begin
699
            warmreg(i);
700
            if (more_to_come(execute_instruction[15:0], i))
701
                $fwrite(decompile_file,", ");
702
            end
703
    $fwrite(decompile_file,"}");
704
    // SDM: store the user mode registers, when in priviledged mode     
705
    if (execute_instruction[22:20] == 3'b100)
706
        $fwrite(decompile_file,"^");
707
    end
708
endtask
709
 
710
 
711
task wshift;
712
    begin
713
    // Check that its a valid shift operation. LSL by #0 is the null operator                                    
714
    if (execute_instruction[6:5] != LSL || shift_imm != 5'd0)
715
        begin
716
        case(execute_instruction[6:5])
717
            2'd0: $fwrite(decompile_file,", lsl");
718
            2'd1: $fwrite(decompile_file,", lsr");
719
            2'd2: $fwrite(decompile_file,", asr");
720
            2'd3: if (shift_imm == 5'd0) $fwrite(decompile_file,", rrx"); else $fwrite(decompile_file,", ror");
721
        endcase
722
 
723
       if (execute_instruction[6:5] != 2'd3 || shift_imm != 5'd0)
724
           $fwrite(decompile_file," #%1d", shift_imm);
725
       end
726
    end
727
endtask
728
 
729
 
730
task wshiftreg;
731
    begin
732
    case(execute_instruction[6:5])
733
        2'd0: $fwrite(decompile_file,", lsl ");
734
        2'd1: $fwrite(decompile_file,", lsr ");
735
        2'd2: $fwrite(decompile_file,", asr ");
736
        2'd3: $fwrite(decompile_file,", ror ");
737
    endcase
738
 
739
    warmreg(reg_s);
740
    end
741
endtask
742
 
743
 
744
task warmreg;
745
input [3:0] regnum;
746
    begin
747 15 csantifort
    if (regnum < 4'd12)
748 2 csantifort
        $fwrite(decompile_file,"r%1d", regnum);
749
    else
750
    case (regnum)
751 15 csantifort
        4'd12   : $fwrite(decompile_file,"ip");
752 2 csantifort
        4'd13   : $fwrite(decompile_file,"sp");
753
        4'd14   : $fwrite(decompile_file,"lr");
754
        4'd15   : $fwrite(decompile_file,"pc");
755
    endcase
756
    end
757
endtask
758
 
759
 
760
task fwrite_hex_drop_zeros;
761
input [31:0] file;
762
input [31:0] num;
763
    begin
764
    if (num[31:28] != 4'd0)
765
        $fwrite(file, "%x", num);
766
    else if (num[27:24] != 4'd0)
767
        $fwrite(file, "%x", num[27:0]);
768
    else if (num[23:20] != 4'd0)
769
        $fwrite(file, "%x", num[23:0]);
770
    else if (num[19:16] != 4'd0)
771
        $fwrite(file, "%x", num[19:0]);
772
    else if (num[15:12] != 4'd0)
773
        $fwrite(file, "%x", num[15:0]);
774
    else if (num[11:8] != 4'd0)
775
        $fwrite(file, "%x", num[11:0]);
776
    else if (num[7:4] != 4'd0)
777
        $fwrite(file, "%x", num[7:0]);
778
    else
779
        $fwrite(file, "%x", num[3:0]);
780
 
781
    end
782
endtask
783
 
784
 
785
 
786
// =================================================================================
787
// Functions
788
// =================================================================================
789
 
790
// Get current value of register
791
function [31:0] get_reg_val;
792
input [4:0] regnum;
793
begin
794
    case (regnum)
795
        5'd0   : get_reg_val = `U_REGISTER_BANK.r0_out;
796
        5'd1   : get_reg_val = `U_REGISTER_BANK.r1_out;
797
        5'd2   : get_reg_val = `U_REGISTER_BANK.r2_out;
798
        5'd3   : get_reg_val = `U_REGISTER_BANK.r3_out;
799
        5'd4   : get_reg_val = `U_REGISTER_BANK.r4_out;
800
        5'd5   : get_reg_val = `U_REGISTER_BANK.r5_out;
801
        5'd6   : get_reg_val = `U_REGISTER_BANK.r6_out;
802
        5'd7   : get_reg_val = `U_REGISTER_BANK.r7_out;
803
        5'd8   : get_reg_val = `U_REGISTER_BANK.r8_out;
804
        5'd9   : get_reg_val = `U_REGISTER_BANK.r9_out;
805
        5'd10  : get_reg_val = `U_REGISTER_BANK.r10_out;
806
        5'd11  : get_reg_val = `U_REGISTER_BANK.r11_out;
807
        5'd12  : get_reg_val = `U_REGISTER_BANK.r12_out;
808
        5'd13  : get_reg_val = `U_REGISTER_BANK.r13_out;
809
        5'd14  : get_reg_val = `U_REGISTER_BANK.r14_out;
810
        5'd15  : get_reg_val = `U_REGISTER_BANK.r15_out_rm; // the version of pc with status bits 
811
 
812
        5'd16  : get_reg_val = `U_REGISTER_BANK.r14_svc;
813
        5'd17  : get_reg_val = `U_REGISTER_BANK.r14_firq;
814
        5'd18  : get_reg_val = `U_REGISTER_BANK.r14_irq;
815
        5'd19  : get_reg_val = `U_REGISTER_BANK.r14_svc;
816
        5'd20  : get_reg_val = `U_REGISTER_BANK.r14_svc;
817
        5'd21  : get_reg_val = `U_REGISTER_BANK.r15_out_rn; // the version of pc without status bits 
818
    endcase
819
end
820
endfunction
821
 
822
 
823
function [31:0] get_32bit_signal;
824
input [2:0] num;
825
begin
826
    case (num)
827
        3'd0: get_32bit_signal = `U_EXECUTE.pc_nxt;
828
        3'd1: get_32bit_signal = `U_FETCH.i_address;
829
        3'd2: get_32bit_signal = `U_FETCH.i_address;
830
        3'd3: get_32bit_signal = `U_CACHE.i_write_data;
831
        3'd4: get_32bit_signal = `U_DECODE.i_read_data;
832
    endcase
833
end
834
endfunction
835
 
836
 
837
function get_1bit_signal;
838
input [2:0] num;
839
begin
840
    case (num)
841
        3'd0: get_1bit_signal = `U_FETCH.i_write_enable;
842
        3'd1: get_1bit_signal = `U_AMBER.fetch_stall;
843
        3'd2: get_1bit_signal = 1'd0;
844
        3'd3: get_1bit_signal = `U_FETCH.i_data_access;
845
    endcase
846
end
847
endfunction
848
 
849
 
850
function [3:0] get_4bit_signal;
851
input [2:0] num;
852
begin
853
    case (num)
854
        3'd0: get_4bit_signal = `U_CACHE.i_byte_enable;
855
    endcase
856
end
857
endfunction
858
 
859
 
860
function [3:0] numchars;
861
input [(5*8)-1:0] xINSTRUCTION_EXECUTE;
862
begin
863
     if (xINSTRUCTION_EXECUTE[31:0] == "    ")
864
    numchars = 4'd1;
865
else if (xINSTRUCTION_EXECUTE[23:0] == "   ")
866
    numchars = 4'd2;
867
else if (xINSTRUCTION_EXECUTE[15:0] == "  ")
868
    numchars = 4'd3;
869
else if (xINSTRUCTION_EXECUTE[7:0]  == " ")
870
    numchars = 4'd4;
871
else
872
    numchars = 4'd5;
873
end
874
endfunction
875
 
876
 
877
function more_to_come;
878
input [15:0] regs;
879
input [31:0] i;
880
begin
881
case (i)
882
    15 : more_to_come = 1'd0;
883
    14 : more_to_come =  regs[15]    ? 1'd1 : 1'd0;
884
    13 : more_to_come = |regs[15:14] ? 1'd1 : 1'd0;
885
    12 : more_to_come = |regs[15:13] ? 1'd1 : 1'd0;
886
    11 : more_to_come = |regs[15:12] ? 1'd1 : 1'd0;
887
    10 : more_to_come = |regs[15:11] ? 1'd1 : 1'd0;
888
     9 : more_to_come = |regs[15:10] ? 1'd1 : 1'd0;
889
     8 : more_to_come = |regs[15: 9] ? 1'd1 : 1'd0;
890
     7 : more_to_come = |regs[15: 8] ? 1'd1 : 1'd0;
891
     6 : more_to_come = |regs[15: 7] ? 1'd1 : 1'd0;
892
     5 : more_to_come = |regs[15: 6] ? 1'd1 : 1'd0;
893
     4 : more_to_come = |regs[15: 5] ? 1'd1 : 1'd0;
894
     3 : more_to_come = |regs[15: 4] ? 1'd1 : 1'd0;
895
     2 : more_to_come = |regs[15: 3] ? 1'd1 : 1'd0;
896
     1 : more_to_come = |regs[15: 2] ? 1'd1 : 1'd0;
897
 
898
endcase
899
end
900
endfunction
901
 
902
`endif
903
 
904
endmodule
905
 

powered by: WebSVN 2.1.0

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