L1DcacheUnstable.v 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * L1 Cache
  3. * Sits between Datamem or Instrmem and arbiter
  4. * Current problem: crashes unless cache item is invalidated after a WRITE
  5. */
  6. module L1Dcache(
  7. // clock/reset inputs
  8. input clk,
  9. input reset,
  10. input cache_reset,
  11. // CPU bus
  12. input [31:0] l2_addr,
  13. input [31:0] l2_data,
  14. input l2_we,
  15. input l2_start,
  16. output [31:0] l2_q,
  17. output l2_done,
  18. // SDRAM controller bus
  19. output [31:0] sdc_addr,
  20. output [31:0] sdc_data,
  21. output sdc_we,
  22. output sdc_start,
  23. input [31:0] sdc_q,
  24. input sdc_done
  25. );
  26. parameter cache_size = 1024; // cache size in words. 8129*4bytes = 32KiB
  27. parameter index_size = 10; // index size: log2(cache_size)
  28. parameter tag_size = 14; // mem_add_bits-index_size = 24-13 = 11
  29. parameter cache_line_size = tag_size+32; // tag + word
  30. reg [cache_line_size-1:0] cache [0:cache_size-1]; // cache memory
  31. integer i;
  32. // init cache to all zeros
  33. initial
  34. begin
  35. for (i = 0; i < cache_size; i = i + 1)
  36. begin
  37. cache[i] = 46'd0;
  38. end
  39. end
  40. reg [index_size-1:0] cache_addr = 10'd0;
  41. reg [cache_line_size-1:0] cache_d = 46'd0;
  42. reg cache_we = 1'b0;
  43. reg [cache_line_size-1:0] cache_q = 46'd0;
  44. always @(posedge clk)
  45. begin
  46. cache_q <= cache[cache_addr];
  47. if (cache_we)
  48. begin
  49. cache[cache_addr] <= cache_d;
  50. $display("%d: wrote to l1 cache", $time);
  51. end
  52. end
  53. reg [31:0] l2_q_reg = 32'd0;
  54. reg l2_done_reg = 1'b0;
  55. reg [23:0] sdc_addr_reg = 24'd0;
  56. reg [31:0] sdc_data_reg = 32'd0;
  57. reg sdc_we_reg = 1'b0;
  58. reg sdc_start_reg = 1'b0;
  59. reg start_prev = 1'b0;
  60. // state machine
  61. reg [2:0] state = 3'd0; // 0-7 states limit
  62. parameter state_init = 3'd0;
  63. parameter state_idle = 3'd1;
  64. parameter state_writing = 3'd2;
  65. parameter state_check_cache = 3'd3;
  66. parameter state_miss_read_ram = 3'd4;
  67. parameter state_delay_cache = 3'd5;
  68. //wire cache_hit = valid_bits[cache_addr] && l2_addr[23:index_size] == cache_q[42:32];
  69. // uninferrable valid bit memory
  70. reg [cache_size-1:0] valid_bits = 1024'd0;
  71. reg [9:0] valid_a = 10'd0;
  72. reg valid_d = 1'b0;
  73. reg valid_q = 1'b0;
  74. reg valid_we = 1'b0;
  75. always @(posedge clk)
  76. begin
  77. if (reset | cache_reset)
  78. begin
  79. valid_bits <= 1024'd0;
  80. end
  81. else
  82. begin
  83. valid_q <= valid_bits[valid_a];
  84. if (valid_we)
  85. begin
  86. valid_bits[valid_a] <= valid_d;
  87. $display("%d: wrote valid bit l1", $time);
  88. end
  89. end
  90. end
  91. reg [31:0] addr_prev = 32'd0;
  92. always @(posedge clk)
  93. begin
  94. if (reset)
  95. begin
  96. valid_a <= 10'd0;
  97. valid_d <= 1'b0;
  98. valid_we <= 1'b0;
  99. l2_q_reg <= 32'd0;
  100. l2_done_reg <= 1'b0;
  101. sdc_addr_reg <= 24'd0;
  102. sdc_data_reg <= 32'd0;
  103. sdc_we_reg <= 1'b0;
  104. sdc_start_reg <= 1'b0;
  105. addr_prev <= 32'd0;
  106. // Make sure the next cycle a new request can be detected!
  107. start_prev <= 1'b0;
  108. state <= state_idle;
  109. end
  110. else
  111. begin
  112. addr_prev <= l2_addr;
  113. start_prev <= l2_start;
  114. l2_done_reg <= 1'b0;
  115. cache_we <= 1'b0;
  116. valid_d <= 1'b0;
  117. valid_we <= 1'b0;
  118. // NOTE: make sure to use latched l2_addr from rising start to make sure all addresses are correct!
  119. // as l2_addr can change during a clear/skipresult (e.g. when jump or other pipeline flush)
  120. case(state)
  121. state_init:
  122. begin
  123. state <= state_idle;
  124. end
  125. state_idle:
  126. begin
  127. valid_a <= l2_addr[index_size-1:0];
  128. if (l2_addr < 27'h800000)
  129. begin
  130. if ( (l2_start && !start_prev) || addr_prev >= 27'h800000 && l2_start)
  131. begin
  132. if (l2_we)
  133. begin
  134. // update cache and write SDRAM
  135. state <= state_writing;
  136. sdc_addr_reg <= l2_addr;
  137. sdc_we_reg <= 1'b1;
  138. sdc_start_reg <= 1'b1;
  139. sdc_data_reg <= l2_data;
  140. cache_d <= {l2_addr[23:index_size], l2_data}; // tag + data
  141. cache_addr <= l2_addr[index_size-1:0];
  142. end
  143. else
  144. begin
  145. // wait a cycle for cache to be read
  146. cache_addr <= l2_addr[index_size-1:0];
  147. state <= state_delay_cache;
  148. // just in case we have a cache miss in the next cycle, prepare address on sdram controller bus
  149. sdc_addr_reg <= l2_addr;
  150. sdc_we_reg <= 1'b0;
  151. end
  152. end
  153. end
  154. end
  155. state_delay_cache:
  156. begin
  157. state <= state_check_cache;
  158. end
  159. state_writing: // Currently disabled setting valid bit to 0. Otherwise causes crashes after a few seconds in L1D cache.
  160. begin
  161. if (sdc_done)
  162. begin
  163. state <= state_idle;
  164. sdc_addr_reg <= 24'd0;
  165. sdc_we_reg <= 1'b0;
  166. sdc_start_reg <= 1'b0;
  167. sdc_data_reg <= 32'd0;
  168. cache_we <= 1'b1; // as long as valid_d <= 0, this does not matter
  169. valid_d <= 1'b0;
  170. valid_we <= 1'b1;
  171. l2_done_reg <= 1'b1;
  172. end
  173. end
  174. state_check_cache:
  175. begin
  176. // check cache. if hit, return cached item
  177. if (valid_q && sdc_addr_reg[23:index_size] == cache_q[45:32]) // valid and tag check
  178. begin
  179. state <= state_idle;
  180. l2_done_reg <= 1'b1;
  181. l2_q_reg <= cache_q[31:0];
  182. end
  183. // if miss, read from ram, place in cache, return cached item
  184. else
  185. begin
  186. state <= state_miss_read_ram;
  187. sdc_start_reg <= 1'b1;
  188. end
  189. end
  190. state_miss_read_ram:
  191. begin
  192. if (sdc_done)
  193. begin
  194. state <= state_idle;
  195. // we received item from ram, now write to cache and return
  196. sdc_addr_reg <= 24'd0;
  197. sdc_start_reg <= 1'b0;
  198. cache_we <= 1'b1;
  199. cache_d <= {sdc_addr_reg[23:index_size], sdc_q}; // tag + data
  200. valid_d <= 1'b1;
  201. valid_we <= 1'b1;
  202. l2_done_reg <= 1'b1;
  203. l2_q_reg <= sdc_q;
  204. end
  205. end
  206. endcase
  207. end
  208. end
  209. // passthrough when above SDRAM memory range
  210. assign sdc_addr = (l2_addr < 27'h800000) ? sdc_addr_reg : l2_addr;
  211. assign sdc_data = (l2_addr < 27'h800000) ? sdc_data_reg : l2_data;
  212. assign sdc_we = (l2_addr < 27'h800000) ? sdc_we_reg : l2_we;
  213. assign sdc_start = (l2_addr < 27'h800000) ? sdc_start_reg : l2_start;
  214. assign l2_q = (l2_addr < 27'h800000) ? l2_q_reg : sdc_q;
  215. assign l2_done = (l2_addr < 27'h800000) ? l2_done_reg : sdc_done;
  216. endmodule