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

Subversion Repositories amber

[/] [amber/] [trunk/] [hw/] [vlog/] [amber25/] [a25_decompile.v] - Blame information for rev 16

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

Line No. Rev Author Line
1 16 csantifort
//////////////////////////////////////////////////////////////////
2
//                                                              //
3
// Decompiler for Amber 25 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) 2011 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
`include "a25_config_defines.v"
44
 
45
module a25_decompile
46
(
47
input                       i_clk,
48
input                       i_access_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       [2:0]           i_pc_sel,
57
input                       i_pc_wen
58
 
59
);
60
 
61
`include "a25_localparams.v"
62
 
63
`ifdef A25_DECOMPILE
64
 
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_access_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_access_stall )
113
        execute_valid <= i_instruction_valid;
114
 
115
// ========================================================
116
// Open File
117
// ========================================================
118
integer decompile_file;
119
 
120
initial
121
    #1 decompile_file = $fopen(`A25_DECOMPILE_FILE, "w");
122
 
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_access_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 != 3'd0 &&
424
             i_pc_wen &&
425
             !i_access_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 Reads and Writes
443
// =================================================================================
444
 
445
reg [31:0] tmp_address;
446
 
447
    // Data access
448
always @( posedge i_clk )
449
    begin
450
    // Data Write    
451
    if ( get_1bit_signal(0) && !get_1bit_signal(3) )
452
        begin
453
 
454
        $fwrite(decompile_file, "%09d              write   addr ", clk_count);
455
        tmp_address = get_32bit_signal(2);
456
        fwrite_hex_drop_zeros(decompile_file, {tmp_address [31:2], 2'd0} );
457
 
458
        $fwrite(decompile_file, ", data %08h, be %h",
459
                get_32bit_signal(3),    // u_cache.i_write_data
460
                get_4bit_signal (0));   // u_cache.i_byte_enable
461
 
462
        $fwrite(decompile_file, "\n");
463
        end
464
 
465
    // Data Read    
466
    if ( get_1bit_signal(4) && !get_1bit_signal(1) )
467
        begin
468
        $fwrite(decompile_file, "%09d              read    addr ", clk_count);
469
        tmp_address = get_32bit_signal(5);
470
        fwrite_hex_drop_zeros(decompile_file, {tmp_address[31:2], 2'd0} );
471
 
472
        $fwrite(decompile_file, ", data %08h to ", get_32bit_signal(4));
473
        warmreg(get_4bit_signal(1));
474
 
475
        $fwrite(decompile_file, "\n");
476
        end
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
// e.g. mrc     15, 0, r9, cr0, cr0, {0}
522
task cortrans_args;
523
    begin
524
    // Co-Processor Number
525
    $fwrite(decompile_file,"%1d, ", execute_instruction[11:8]);
526
    // opcode1
527
    $fwrite(decompile_file,"%1d, ", execute_instruction[23:21]);
528
    // Rd [15:12]
529
    warmreg(reg_d);
530
    // CRn [19:16]
531
    $fwrite(decompile_file,", cr%1d", execute_instruction[19:16]);
532
    // CRm [3:0]
533
    $fwrite(decompile_file,", cr%1d", execute_instruction[3:0]);
534
    // Opcode2 [7:5]
535
    $fwrite(decompile_file,", {%1d}",   execute_instruction[7:5]);
536
    end
537
endtask
538
 
539
 
540
// ldc  15, 0, r9, cr0, cr0, {0}
541
task codtrans_args;
542
    begin
543
    // Co-Processor Number
544
    $fwrite(decompile_file,"%1d, ", execute_instruction[11:8]);
545
    // CRd [15:12]
546
    $fwrite(decompile_file,"cr%1d, ", execute_instruction[15:12]);
547
    // 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
    if (regnum < 4'd12)
748
        $fwrite(decompile_file,"r%1d", regnum);
749
    else
750
    case (regnum)
751
        4'd12   : $fwrite(decompile_file,"ip");
752
        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_EXECUTE.o_iaddress;
829
        3'd2: get_32bit_signal = `U_EXECUTE.o_daddress;
830
        3'd3: get_32bit_signal = `U_EXECUTE.o_write_data;
831
//         3'd4: get_32bit_signal = `U_EXECUTE.read_data_filtered;
832
        3'd4: get_32bit_signal = `U_EXECUTE.i_wb_read_data;
833
        3'd5: get_32bit_signal = `U_WB.daddress_r;
834
    endcase
835
end
836
endfunction
837
 
838
 
839
function get_1bit_signal;
840
input [2:0] num;
841
begin
842
    case (num)
843
        3'd0: get_1bit_signal = `U_EXECUTE.o_write_enable;
844
        3'd1: get_1bit_signal = `U_AMBER.mem_stall;
845
        3'd2: get_1bit_signal = `U_EXECUTE.o_daddress_valid;
846
        3'd3: get_1bit_signal = `U_AMBER.access_stall;
847
        3'd4: get_1bit_signal = `U_WB.mem_read_data_valid_r;
848
    endcase
849
end
850
endfunction
851
 
852
 
853
function [3:0] get_4bit_signal;
854
input [2:0] num;
855
begin
856
    case (num)
857
        3'd0: get_4bit_signal = `U_EXECUTE.o_byte_enable;
858
        3'd1: get_4bit_signal = `U_WB.mem_load_rd_r;
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.