`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date: 28.02.2019 11:57:19
// Design Name: 
// Module Name: sign_extend
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//////////////////////////////////////////////////////////////////////////////////

module sign_extend (
    input [31:0] instruction,
    output reg [31:0] data_out
);    

     wire [11:0] immediate;
     reg [31:0] rotated;
     
     always@(*)
           casex(instruction[31:21])
               // LDUR OR STUR
               11'b111110000x0:    data_out = {{23{instruction[20]}},instruction[20:12]};
               // CBZ
               11'b10110100xxx:    data_out = {{13{instruction[23]}},instruction[23:5]};
               // B
               11'b000101xxxxx:    data_out = {{6{instruction[25]}},instruction[25:0]};
               // I-format
               11'b1xx100xx00x:    data_out = rotated;
               default:            data_out = 32'bx;
           endcase

    assign immediate = instruction[21:10];

    always@(*)
        case(immediate[11:8])
            4'h0:       rotated = {24'b0,immediate[7:0]};
            4'h1:       rotated = {immediate[1:0],24'b0,immediate[7:2]};
            4'h2:       rotated = {immediate[3:0],24'b0,immediate[7:4]};
            4'h3:       rotated = {immediate[5:0],24'b0,immediate[7:6]};
            4'h4:       rotated = {immediate[7:0],24'b0};
            4'h5:       rotated = {2'b0,immediate[7:0],22'b0};
            4'h6:       rotated = {4'b0,immediate[7:0],20'b0};
            4'h7:       rotated = {6'b0,immediate[7:0],18'b0};
            4'h8:       rotated = {8'b0,immediate[7:0],16'b0};
            4'h9:       rotated = {10'b0,immediate[7:0],14'b0};
            4'hA:       rotated = {12'b0,immediate[7:0],12'b0};
            4'hB:       rotated = {14'b0,immediate[7:0],10'b0};
            4'hC:       rotated = {16'b0,immediate[7:0],8'b0};
            4'hD:       rotated = {18'b0,immediate[7:0],6'b0};
            4'hE:       rotated = {20'b0,immediate[7:0],4'b0};
            4'hF:       rotated = {22'b0,immediate[7:0],2'b0};
            default:    rotated = 32'bx;
        endcase


endmodule
