DataMem.v 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Data Memory
  3. */
  4. module DataMem(
  5. input wire clk, reset,
  6. input wire [31:0] addr,
  7. input wire we,
  8. input wire re,
  9. input wire [31:0] data,
  10. output wire [31:0] q,
  11. output busy,
  12. // bus
  13. output [31:0] bus_addr,
  14. output [31:0] bus_data,
  15. output bus_we,
  16. output bus_start,
  17. input [31:0] bus_q,
  18. input bus_done,
  19. input bus_ready,
  20. input wire clear, hold
  21. );
  22. reg [31:0] qreg = 32'd0;
  23. reg busy_reg = 1'b0;
  24. wire read_or_write = we || re;
  25. reg read_or_write_prev = 1'b0;
  26. wire read_or_write_edge = read_or_write && !read_or_write_prev;
  27. assign bus_addr = addr;
  28. assign bus_data = data;
  29. assign bus_we = we;
  30. assign bus_start = read_or_write_edge;
  31. assign busy = read_or_write && !bus_done;
  32. assign q = (bus_done) ? bus_q : qreg;
  33. // Clear and Hold are currently skipped because they are not used in the CPU
  34. always @(posedge clk)
  35. begin
  36. if (reset)
  37. begin
  38. read_or_write_prev <= 1'b0;
  39. qreg <= 32'd0;
  40. end
  41. else
  42. begin
  43. read_or_write_prev <= read_or_write;
  44. if (bus_done)
  45. begin
  46. qreg <= bus_q;
  47. end
  48. end
  49. end
  50. endmodule