Altera_Forum
Honored Contributor
19 years agoRegister write/read
I wrote a simple VHDL component for SOPC builder that just has one 32 bit register. I can read and write it OK, but I have to read it twice to read the value I just wrote. The first read gives me the old value.
Here's the sequence: write 0x01 to register read 0x00 from reg. Why isn't this 0x01? read 0x01 from reg. OK now it's right. write 0x02 to reg. read 0x01 from reg. Should be 0x02. read 0x02 from reg. correct. I'm using IORD/WR and there's no cache in the system. I set the read latency to 1 in component editor. Write latency, waits, setup, & hold are set to 0. Here's the VHDL for the register: signal scratch_reg : std_logic_vector(31 downto 0); ... reg_p : process (CLK, reset_n) is begin if reset_n = '0' then scratch_reg <= (others => '0'); elsif CLK'event and CLK = '1' then -- WRITE if chipselect = '1' and write = '1' then scratch_reg <= writedata; end if; -- READ if chipselect = '1' and read = '1' then readdata <= scratch_reg; end if; end if; end process reg_p; And the C code: alt_u32 data; ... scanf("%x", &data); //get write data IOWR(SCRATCHPAD_BASE, 0, data); printf("SCRATCH set to 0x%08X \n", IORD(SCRATCHPAD_BASE, 0) ); printf("SCRATCH set to 0x%08X \n", IORD(SCRATCHPAD_BASE, 0) ); The first printf prints the old value, the 2nd one prints the correct value. Any idea where the problem is? http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/blink.gif Thanks in advance for any suggestions.