Question

For example, instead of using reg [3:0] RAM [0:31]; I've made my own module attempting to use using a hardwired FlipFlopMod.

This is what I'm trying to do (but you'll see it obviously doesn't work):

module mem_mod(addr, mem_out);
   input [4:0] addr;        // 5-bit addr bus
   output [3:0] mem_out;    // 4-bit inst/data out

   // a real flip flop instead of using reg.
   FlipFlopMod_4bit RAM [0:31] (1/*enabled*/, 4'b1111, q, nq);    // # of nibbles

   assign mem_out = RAM[addr]; // change this to an AND selector.

   // read binary code into RAM, prepare inst.txt 1st
   initial $readmemb("inst.txt", RAM);
endmodule

// Gated Flip Flop
module FlipFlopMod_1bit(clk, d, q, nq);
    input clk, d;
    output q, nq;

    wire and0, and1, d_inv;

    not(d_inv, d); // d and d_inv are someties referred to as R and S, respectively.

    // these two and gates cause the input to be latched only when clk
    // (enable) is high.
    and(and0, d, clk);
    and(and1, clk, d_inv);

    // These nor gates are the actual flipflop/latch.
    nor(nq, and0, q);
    nor(q, nq, and1);
endmodule

// 4 bit gated flip flop made using 1 bit gated flip flops.
module FlipFlopMod_4bit(clk, d, q, nq);
    input clk;
    input [3:0] d;
    output [3:0] q, nq;

    FlipFlopMod_1bit latch3 (clk, d[3], q[3], nq[3]);
    FlipFlopMod_1bit latch2 (clk, d[2], q[2], nq[2]);
    FlipFlopMod_1bit latch1 (clk, d[1], q[1], nq[1]);
    FlipFlopMod_1bit latch0 (clk, d[0], q[0], nq[0]);
endmodule

This is where I started from, using reg (this works):

module mem_mod(addr, mem_out);
   input [4:0] addr;        // 5-bit addr bus
   output [3:0] mem_out;    // 4-bit inst/data out

   reg [3:0] RAM [0:31];    // # of nibbles

   assign mem_out = RAM[addr];

   // read binary code into RAM, prepare inst.txt 1st
   initial $readmemb("inst.txt", RAM);
endmodule

As you can see, I'm not quite sure how to read each 4bit piece of data from a file into my custom array of FlipFlops, so that each of my FlipFlops holds a different value, like the reg example. I think I could probably just hard code 32 flip flops, and hard code the values in each parameter instead of reading the file, but that's no fun!

The file that I'm reading from (inst.txt) looks like this:

0001
0011
1000
0011
...
...
... etc, 32 lines total.
Was it helpful?

Solution

Are you trying to do this for synthesis or simulation? One easy hack is to use $fscanf and have some initialization code that iterates through a file copying one line from the file into your various flops. Of course for synthesis this won't work. Can I ask why you don't want to just use reg?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top