L2cache.v 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 = 32768; // cache size in words. 8129*4bytes = 32KiB
  28. parameter index_size = 15; // index size: log2(cache_size)
  29. parameter tag_size = 9; // mem_addr_bits-index_size = 24-13 = 11
  30. parameter cache_line_size = tag_size+32+1; // tag + word + valid bit
  31. reg [cache_line_size-1:0] cache [0:cache_size-1]; // cache memory
  32. /*
  33. integer i;
  34. // init cache to all zeros
  35. initial
  36. begin
  37. for (i = 0; i < cache_size; i = i + 1)
  38. begin
  39. cache[i] = 42'd0;
  40. end
  41. end
  42. */
  43. reg [index_size-1:0] cache_addr = 15'd0;
  44. reg [cache_line_size-1:0] cache_d = 42'd0;
  45. reg cache_we = 1'b0;
  46. reg [cache_line_size-1:0] cache_q = 42'd0;
  47. always @(posedge clk)
  48. begin
  49. cache_q <= cache[cache_addr];
  50. if (cache_we)
  51. begin
  52. cache[cache_addr] <= cache_d;
  53. $display("%d: wrote to l2 cache", $time);
  54. end
  55. end
  56. reg [31:0] l2_q_reg = 32'd0;
  57. reg l2_done_reg = 1'b0;
  58. reg [23:0] sdc_addr_reg = 24'd0;
  59. reg [31:0] sdc_data_reg = 32'd0;
  60. reg sdc_we_reg = 1'b0;
  61. reg sdc_start_reg = 1'b0;
  62. reg start_prev = 1'b0;
  63. // state machine
  64. reg [2:0] state = 3'd0; // 0-7 states limit
  65. parameter state_init = 3'd0;
  66. parameter state_idle = 3'd1;
  67. parameter state_writing = 3'd2;
  68. parameter state_check_cache = 3'd3;
  69. parameter state_miss_read_ram = 3'd4;
  70. parameter state_delay_cache = 3'd5;
  71. parameter state_done_high = 3'd6;
  72. parameter state_clear_cache = 3'd7;
  73. reg [31:0] addr_prev = 32'd0;
  74. reg [15:0] clear_cache_counter = 16'd0; // 64k max
  75. reg start_registered = 1'b0;
  76. always @(posedge clk)
  77. begin
  78. if (reset)
  79. begin
  80. l2_q_reg <= 32'd0;
  81. l2_done_reg <= 1'b0;
  82. sdc_addr_reg <= 24'd0;
  83. sdc_data_reg <= 32'd0;
  84. sdc_we_reg <= 1'b0;
  85. sdc_start_reg <= 1'b0;
  86. addr_prev <= 32'd0;
  87. // Make sure the next cycle a new request can be detected!
  88. start_prev <= 1'b0;
  89. state <= state_clear_cache;
  90. clear_cache_counter <= 16'd0;
  91. start_registered <= 1'b0;
  92. end
  93. else
  94. begin
  95. addr_prev <= l2_addr;
  96. start_prev <= l2_start;
  97. l2_done_reg <= 1'b0;
  98. cache_we <= 1'b0;
  99. // NOTE: make sure to use latched l2_addr from rising start to make sure all addresses are correct!
  100. // as l2_addr can change during a clear/skipresult (e.g. when jump or other pipeline flush)
  101. case(state)
  102. state_init:
  103. begin
  104. state <= state_clear_cache;
  105. end
  106. state_clear_cache:
  107. begin
  108. if (l2_addr < 27'h800000 && ( (l2_start && !start_prev) || addr_prev >= 27'h800000 && l2_start) )
  109. begin
  110. start_registered <= 1'b1;
  111. end
  112. if (clear_cache_counter == cache_size)
  113. begin
  114. clear_cache_counter <= 16'd0;
  115. state <= state_idle;
  116. end
  117. else
  118. begin
  119. clear_cache_counter <= clear_cache_counter + 1'b1;
  120. cache_we <= 1'b1;
  121. cache_d <= 42'd0;
  122. cache_addr <= clear_cache_counter;
  123. end
  124. end
  125. state_idle:
  126. begin
  127. if (l2_addr < 27'h800000)
  128. begin
  129. if ( ( (l2_start && !start_prev) || addr_prev >= 27'h800000 && l2_start) || start_registered)
  130. begin
  131. start_registered <= 1'b0;
  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 <= {1'b1, 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:
  160. begin
  161. if (sdc_done)
  162. begin
  163. state <= state_done_high;
  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;
  169. l2_done_reg <= 1'b1;
  170. end
  171. end
  172. state_check_cache:
  173. begin
  174. // check cache. if hit, return cached item
  175. if (cache_q[41] && sdc_addr_reg[23:index_size] == cache_q[40:32]) // valid and tag check
  176. begin
  177. state <= state_done_high;
  178. l2_done_reg <= 1'b1;
  179. l2_q_reg <= cache_q[31:0];
  180. end
  181. // if miss, read from ram, place in cache, return cached item
  182. else
  183. begin
  184. state <= state_miss_read_ram;
  185. sdc_start_reg <= 1'b1;
  186. end
  187. end
  188. state_miss_read_ram:
  189. begin
  190. if (sdc_done)
  191. begin
  192. state <= state_done_high;
  193. // we received item from ram, now write to cache and return
  194. sdc_addr_reg <= 24'd0;
  195. sdc_start_reg <= 1'b0;
  196. cache_we <= 1'b1;
  197. cache_d <= {1'b1, sdc_addr_reg[23:index_size], sdc_q}; // tag + data
  198. l2_done_reg <= 1'b1;
  199. l2_q_reg <= sdc_q;
  200. end
  201. end
  202. state_done_high:
  203. begin
  204. // keep done high for one cycle as we run on double clock speed from CPU
  205. l2_done_reg <= 1'b1;
  206. state <= state_idle;
  207. end
  208. endcase
  209. end
  210. end
  211. // passthrough when above SDRAM memory range
  212. assign sdc_addr = (l2_addr < 27'h800000) ? sdc_addr_reg : l2_addr;
  213. assign sdc_data = (l2_addr < 27'h800000) ? sdc_data_reg : l2_data;
  214. assign sdc_we = (l2_addr < 27'h800000) ? sdc_we_reg : l2_we;
  215. assign sdc_start = (l2_addr < 27'h800000) ? sdc_start_reg : l2_start;
  216. assign l2_q = (l2_addr < 27'h800000) ? l2_q_reg : sdc_q;
  217. assign l2_done = (l2_addr < 27'h800000) ? l2_done_reg : sdc_done;
  218. endmodule