cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Observer
Observer
3,370 Views
Registered: ‎07-26-2016

Can't initialize memory

Jump to solution

Hi

I'm learning how to build a simple MIPS SoC on Basys3 board, and am having a problem initializing my memory.

The design source hierarchy is as follows.

FO1$3@L@$VYIB`4]A4SL5`M.png

The top.v code:

 

module top #(parameter WIDTH = 8, REGBITS = 3)(
    input                          clk, reset,
    input          [WIDTH-1:0]     swu,		//upper 8 switches
    input          [WIDTH-1:0]     swl,		//lower 8 switches
    input          [4:0]           btn,		//5 push buttons
    output  reg    [3:0]           an,		//digit selection for 7-seg display
    output  reg    [WIDTH-1:0]     seg,		//seg selection for 7-seg display
    output  reg    [WIDTH-1:0]     ldu,		//upper 8 LEDs
    output  reg    [WIDTH-1:0]     ldl);	//lower 8 LEDs

    wire   	         memread, memwrite;
    wire    [WIDTH-1:0]  adr, writedata;
    wire    [WIDTH-1:0]	 memdata;

   // instantiate devices to be tested
   mips	 #(WIDTH, REGBITS) dut(clk, reset, memdata, memread, memwrite, adr, writedata);

   // external memory for code and data
   memory_io #(WIDTH) mem_io(clk, memwrite, adr, writedata, memdata);
endmodule

The memory_io.v uses memory-mapped I/O solution:

module memory_io #(parameter WIDTH = 8)(
   input   			clk,
   input 			memwrite,
   input	[WIDTH-1:0]     adr,
   input        [WIDTH-1:0]     writedata,
   output   reg	[WIDTH-1:0]     memdata,
   input        [WIDTH-1:0]     swu,
   input        [WIDTH-1:0]     swl,
   input        [4:0]           btn,
   output   reg [3:0]           an,
   output   reg [WIDTH-1:0]     seg,
   output   reg [WIDTH-1:0]     ldu,
   output   reg [WIDTH-1:0]     ldl);
   
   reg  [31:0] 	RAM [(1<<WIDTH-2)-1:0];
   wire [31:0] 	word;
   
//   initial
//      begin
//         $readmemh("D:/Computer/mips_8/memfile.txt", RAM, 4);
//      end
initial begin RAM[4] <= 32'h0000_0008; RAM[8] <= 32'h0000_0004; RAM[12] <= 32'h0000_0002; RAM[16] <= 32'h0000_0001; RAM[20] <= 32'h8001_0000; RAM[24] <= 32'h8002_0004; RAM[28] <= 32'h8003_0008; RAM[32] <= 32'h8004_000C; RAM[36] <= 32'h8005_0010; RAM[40] <= 32'h1085_0002; RAM[44] <= 32'h0043_0822; RAM[48] <= 32'h0800_000E; RAM[52] <= 32'h0043_0820; RAM[56] <= 32'hA001_0000; end // read and write bytes from 32-bit word always @(posedge clk) if (memwrite) if (adr >= 8'hf0) case (adr) 8'hf9: an <= writedata[3:0]; 8'hf8: seg <= writedata; 8'hf1: ldu <= writedata; 8'hf0: ldl <= writedata; endcase else case (adr[1:0]) 2'b00: RAM[adr>>2][7:0] <= writedata; 2'b01: RAM[adr>>2][15:8] <= writedata; 2'b10: RAM[adr>>2][23:16] <= writedata; 2'b11: RAM[adr>>2][31:24] <= writedata; endcase assign word = RAM[adr >> 2]; always @(*) if (adr >= 8'hf0) case (adr) 8'hf4: memdata <= {3'b000, btn}; 8'hfc: memdata <= swl; 8'hfd: memdata <= swu; endcase else case (adr[1:0]) 2'b00: memdata <= word[31:24]; 2'b01: memdata <= word[23:16]; 2'b10: memdata <= word[15:8]; 2'b11: memdata <= word[7:0]; endcase endmodule

And my testbench code:

`timescale 1ns / 1ps

module mips_8_tb #(parameter WIDTH = 8, REGBITS = 3)();
    reg     		clk;
    reg                 reset;
    wire                memread, memwrite;
    wire    [WIDTH-1:0] adr, writedata;
    wire    [WIDTH-1:0] memdata;
    reg     [WIDTH-1:0] swu, swl;
    reg     [4:0]       btn;
    wire    [3:0]       an;
    wire    [WIDTH-1:0] seg, ldu, ldl; 

// instantiate devices to be tested
    mips        #(WIDTH, REGBITS)    dut_tb(clk, reset, memdata, memread, memwrite, 
                                            adr, writedata);
// external memory for code and data
    memory_io   #(WIDTH)    mem_io_tb(clk, memwrite, adr, writedata, memdata,
                                      swu, swl, btn, an, seg, ldu, ldl);

    initial
        begin
            reset <= 1; #22 reset <= 0;
        end

always begin clk <= 0; #5; clk <= ~clk; #5; end endmodule

I added RAM to wave window, but got the following wave:

{78$[P4HV9%QJ7UCNSD~NEW.png

Thanks for your answer! :)

 

0 Kudos
1 Solution

Accepted Solutions
Highlighted
Moderator
Moderator
5,777 Views
Registered: ‎05-31-2017

Re: Can't initialize memory

Jump to solution

Hi cmpt_design,

You would get the waveform as shown in the attached file.

From the waveform , it is clear that the RAM values which are initialized are shown as green and those which are not initialized are shown as red. As, RAM is of type REG the default value is X.

Please initialize the remaining values of RAM so that the register is not left uninitialized.

 

Thanks & Regards,

A.Shameer

View solution in original post

waveform.JPG
0 Kudos
6 Replies
Highlighted
Observer
Observer
3,367 Views
Registered: ‎07-26-2016

Re: Can't initialize memory

Jump to solution
I noticed there's a correct value: a0010000, among 2 others, but there're 15 values I'd like to pre-store into my memory. And also, why is the color red instead of green?
0 Kudos
Highlighted
Explorer
Explorer
3,337 Views
Registered: ‎04-12-2017

Re: Can't initialize memory

Jump to solution

Hi,

 

If you want 16 addresses you have to work with an address width of 5.

 

 

module mem #(parameter WIDTH = 5)(

Also, your RAM assignment must be continuous, you must assign values to each cell of the array, like this:

 

    initial
       begin
          RAM[0]    <=  32'h0000_0008;
          RAM[1]    <=  32'h0000_0004;
          RAM[2]   <=  32'h0000_0002;
          RAM[3]   <=  32'h0000_0001;
          RAM[4]   <=  32'h8001_0000;
          RAM[5]   <=  32'h8002_0004;
          RAM[6]   <=  32'h8003_0008;
          RAM[7]   <=  32'h8004_000C;
          RAM[8]   <=  32'h8005_0010;
          RAM[9]   <=  32'h1085_0002;          
          RAM[10]   <=  32'h0043_0822;
          RAM[11]   <=  32'h0800_000E;
          RAM[12]   <=  32'h0043_0820;
          RAM[13]   <=  32'hA001_0000;         

 

I have added the values you used, you have to add values for RAM[14] and RAM[15] so they won't be left undefined.

 

Hope this helps

Avi Chami MSc
FPGA Site
0 Kudos
Highlighted
Moderator
Moderator
5,778 Views
Registered: ‎05-31-2017

Re: Can't initialize memory

Jump to solution

Hi cmpt_design,

You would get the waveform as shown in the attached file.

From the waveform , it is clear that the RAM values which are initialized are shown as green and those which are not initialized are shown as red. As, RAM is of type REG the default value is X.

Please initialize the remaining values of RAM so that the register is not left uninitialized.

 

Thanks & Regards,

A.Shameer

View solution in original post

waveform.JPG
0 Kudos
Highlighted
Observer
Observer
3,217 Views
Registered: ‎07-26-2016

Re: Can't initialize memory

Jump to solution

You're absolutely right!

0 Kudos
Highlighted
Observer
Observer
3,216 Views
Registered: ‎07-26-2016

Re: Can't initialize memory

Jump to solution

Yeah, I got it, thx!

0 Kudos
Highlighted
Moderator
Moderator
3,188 Views
Registered: ‎09-15-2016

Re: Can't initialize memory

Jump to solution

Hi @cmpt_design,

 

Please close the thread my marking the appropriate post which helped you with the resolution as "Accepted solution". This will help the future users.

Regards,
Sravanthi B

 

Thanks & Regards,
Sravanthi B
----------------------------------------------------------------------------------------------
Kindly note- Please mark the Answer as "Accept as solution" if information provided is helpful.

Give Kudos to a post which you think is helpful and reply oriented.
----------------------------------------------------------------------------------------------
0 Kudos