1
0

L2cache.v 7.7 KB

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