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

Subversion Repositories amber

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

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

powered by: WebSVN 2.1.0

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