I have a 50Mhz master clock clk and from that I have a derived baudClock clock which runs at 9600bps.
I have a transmitter module that I want to follow a state machine flow every baudClock.
The transmitter module receives strings from an external module/testbench and puts them into a queue. I want the state machine to take an item from the queue and then reduce queueCount by one.
On the other hand, I would like to be able to add an item to the queue on a clk cycle.
I cannot implement something such as that below because I would then be assigning queueCount from 2 always processes (resulting in multiple driver errors).
always_ff @(posedge clk )
begin
if( addToQueue )
begin
if( queueCount < MAX_ELEMENTS )
begin
textQueue[ queueCount ] = myString;
queueCount ++;
end
end
end
always_ff @(posedge baudClock )
begin
unique case( state )
IDLE : HandleIdle();
GETNEXT : HandleGetNextString(); // Decreases queueCount
SENDSTART : HandleSendStart();
SENDDATA : HandleSendData();
SENDBYTEDONE : HandleSendByteDone();
endcase
end
How would I go about doing this? ie. What's the best way to share variables in reading/writing or producer/consumer scenarios?
Best Answer
In comments you said this is for implementation in an FPGA.
In that case, you should reconsider your architecture.
Instead of dividing down your 50 MHz master clock and having a second 9600 Hz clock, use the 50 MHz clock to drive your 9600 Hz logic, but use a strobe signal to enable that logic.
Now your queue becomes part of the 50 MHz logic, with the
str9600
signal as an input, telling it whether something can be pulled out of the queue, but able to add to the queue on any 50 MHz clock edge.