Assign reg which has initial value

2.8k views Asked by At

I'm implementing a tetris game using basys 3 on 8x8 led matrix.

reg [7:0]redBlock;
reg [7:0]backupRed= 8'b00011000;
reg [7:0]temp;

assign redBlock=(toRight& oe) ? backupRed>> 1 : backupRed;

This code is working the redblock on led matrix goes right when I push button toRight but of couse it goes back to it's initial position since the condition 0 is equal to backupRed

What I really want is

reg [7:0]redBlock = 8'b00011000;;
reg [7:0]backupRed;
reg [7:0]temp;

assign redBlock=(toRight& oe) ? redBlock>> 1 : redBlock;

which should update the previous location of redBlock but it gives error

Variable redBlcok is written by both continuous and procedural assigments

I'm using Vivado for implemantion

Here is 10sec video of what I meant

http://sendvid.com/ei5bfqc3

1

There are 1 answers

0
Hida On BEST ANSWER

You are using an continuous assignment assign to describe data that you wish to be stored.

Continuous assignments are used to describe combinatorical logic, but what you want here is sequential logic, a register (flip-flop).

Something along these lines:

reg [7:0]redBlock = 8'b00011000;

always @(posedge ck or posedge rst) begin
  if(rst)
    redBlock <= 8'b00011000;
  else
    redBlock <= (toRight& oe) ? redBlock>> 1 : redBlock;
end

Edit: Some thoughts on what kind of troubles you may run into.

Not sure what kind of signal toRightis, assuming that toRight is the raw button input:

Firstly, you need to synchronize your button input.

If your clock is slow in relation to the duration of toRight, you may need to capture the short pulse and generate a clock cycle long pulse.

If your clock is fast in relation to the duration of toRight, you may need to add a counter which can ensure that a single press of the button doesn't increment redBlock too much per press.

Furthermore you may need to handle the case where redBlock has reached the edge of your display.