You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

728 lines
27 KiB

  1. `timescale 1ns / 1ns
  2. //////////////////////////////////////////////////////////////////////////////////
  3. // Company:
  4. // Engineer:
  5. //
  6. // Create Date: 02/24/2021 08:03:07 PM
  7. // Design Name:
  8. // Module Name: sc_decoder_fsm
  9. // Project Name:
  10. // Target Devices:
  11. // Tool Versions:
  12. // Description:
  13. //
  14. // Dependencies:
  15. //
  16. // Revision:
  17. // Revision 0.01 - File Created
  18. // Additional Comments:
  19. //
  20. //////////////////////////////////////////////////////////////////////////////////
  21. `define BITS 8
  22. module sc_decoder_fsm #(parameter BITS=8, N=11'd16)(
  23. input clk, rst,
  24. input in_valid,
  25. input signed [N-1:0][BITS-1:0]y,
  26. input [N-1:0]f,
  27. output logic [N-1:0]u_cap,
  28. output logic [N-1:0]v_final,
  29. output logic out_valid
  30. );
  31. //function for fminsum calculation
  32. function void fminsum_calc;
  33. input signed [`BITS-1:0] a;
  34. input signed [`BITS-1:0] b;
  35. output signed [`BITS-1:0] c;
  36. logic [`BITS-2:0] abs_a;
  37. logic [`BITS-2:0] abs_b;
  38. logic [`BITS-2:0] abs_c;
  39. abs_a = (a[`BITS-1] == 1) ? ~a[`BITS-2:0] + 1 : a[`BITS-2:0];
  40. abs_b = (b[`BITS-1] == 1) ? ~b[`BITS-2:0] + 1 : b[`BITS-2:0];
  41. c[`BITS-1] = a[`BITS-1] ^ b[`BITS-1];
  42. abs_c = (abs_b < abs_a) ? abs_b : abs_a;
  43. c[`BITS-2:0] = (c[`BITS-1] == 1) ? ~abs_c + 1 : abs_c;
  44. endfunction
  45. //function for g-value calculation
  46. function void g_calc;
  47. input signed [`BITS-1:0] a;
  48. input signed [`BITS-1:0] b;
  49. input u;
  50. output signed [`BITS:0] c;
  51. c = (u == 0) ? (b + a) : (b + (~a+1));
  52. endfunction
  53. //parameters and signals declarations
  54. localparam d=$clog2(N); //N=4, d=2(0 & 1)
  55. localparam n=2*N-1; //(2**d)-1;
  56. localparam cmax=0;
  57. logic [N-1:0]u;
  58. logic [d:0]temp_index_f,temp_index_g;
  59. reg signed [BITS-1:0] LRU[2];
  60. reg [N-1:0]v;
  61. logic [N-1:0][BITS-1:0]L_in, L_out;
  62. logic [N-1:0]v_in, v_out;
  63. logic ena_v,enb_v,wea_v;
  64. logic ena_L,enb_L,wea_L;
  65. logic [1:0]counter,counter_reg;
  66. logic [11:0]jL1,jL2,jR1,jR2,jR3,jU1,jU2;
  67. logic [4:0] c_state, n_state;
  68. wire [N-1:0]u_cap_1;
  69. wire [N-1:0]v_final_1;
  70. wire out_valid_1;
  71. //Auxiliary registers declarations
  72. logic [d:0] depth,depth_reg;
  73. logic [d:0] node,node_reg;
  74. logic [11:0]tmp_L,tmp_L_reg, tmp_R,tmp_R_reg,tmp_U, tmp_U_reg;
  75. //FSM States
  76. localparam idle=5'd0, root=5'd1, wait_L_logic=5'd2, wait_L=5'd3, state_L=5'd4, wait_R_logic=5'd5, wait_R=5'd6, state_R=5'd7;
  77. localparam wait_U_logic=5'd8, wait_U=5'd9, state_U=5'd10,wait_LRU_logic=5'd11, wait_LRU=5'd12, state_LRU=5'd13;
  78. localparam wait_lnode_logic=5'd14, wait_lnode=5'd15, state_lnode=5'd16,wait_lstate_logic=5'd17, wait_lstate=5'd18, state_last=5'd19;
  79. //BlockRAM Instantiations
  80. bram_v #(.ADDR_WIDTH(d-1),.DATA_WIDTH(N),.DEPTH(2**(d-1))) bram_v_i (
  81. .clk(clk),.ena(ena_v),.enb(enb_v),
  82. .addra(depth_reg-1'b1),
  83. .addrb(depth_reg),
  84. .wea(wea_v),
  85. .dia(v_in),
  86. .dob(v_out)
  87. );
  88. bram_L #(.ADDR_WIDTH(d-1),.DATA_WIDTH(N*BITS),.DEPTH(2**(d-1)),.N(N)) bram_L_i (
  89. .clk(clk),.ena(ena_L),.enb(enb_L),
  90. .addra(depth_reg),
  91. .addrb(depth_reg-1'b1),
  92. .wea(wea_L),
  93. .dia(L_in),
  94. .dob(L_out)
  95. );
  96. //output assignment
  97. for(genvar i=0; i<N; i++)
  98. begin
  99. assign u_cap_1[i] = u[i];
  100. end
  101. assign v_final_1 = v;
  102. assign out_valid_1 = (n_state == state_lnode) ? 1'b1 : 1'b0;
  103. //assign out_valid=(n_state==state_last)?1'b1:1'b0;
  104. // Sequential Logic - FSM State and Data Registers
  105. always_ff@(posedge clk)
  106. begin
  107. if(rst==1)
  108. begin
  109. u_cap <= 'b0;
  110. v_final <= 'b0;
  111. out_valid <= 'b0;
  112. c_state <= idle;
  113. depth_reg<=0;
  114. node_reg<=0;
  115. counter_reg<=0;
  116. tmp_L_reg<=0;
  117. tmp_R_reg<=0;
  118. tmp_U_reg<=0;
  119. end
  120. else
  121. begin
  122. out_valid <= out_valid_1;
  123. v_final <= v_final_1;
  124. if(out_valid)
  125. begin
  126. u_cap <= u;
  127. end
  128. else
  129. begin
  130. u_cap <= u_cap;
  131. end
  132. c_state <= n_state;
  133. depth_reg<=depth;
  134. node_reg<=node;
  135. counter_reg<=counter;
  136. tmp_L_reg<=tmp_L;
  137. tmp_R_reg<=tmp_R;
  138. tmp_U_reg<=tmp_U;
  139. end
  140. end
  141. //Combinational Logic - FSM Next State Logic
  142. always_comb
  143. begin
  144. // depth=0; node=0;
  145. // ena_L=0;wea_L=0;enb_L=0;
  146. tmp_L=0; tmp_R=0; tmp_U=0;
  147. counter=0; ena_v=0;wea_v=0; enb_v=0; n_state = 0;
  148. if(in_valid==1)
  149. case(c_state)
  150. idle: begin
  151. depth=0; node=0; counter=0; tmp_L=0; tmp_R=0; tmp_U=0;
  152. ena_L=0;wea_L=0;enb_L=0;
  153. ena_v=0;wea_v=0; enb_v=0;
  154. if(out_valid==1)
  155. n_state=idle;
  156. else
  157. n_state=root;
  158. end
  159. root: begin
  160. depth=depth_reg;node=node_reg;
  161. ena_L=1;wea_L=1;enb_L=0;
  162. ena_v=0;wea_v=0; enb_v=0;
  163. for(int k=0; k<N; k++)
  164. L_in[k]=y[k];
  165. n_state=wait_L_logic;
  166. end
  167. wait_L_logic:
  168. begin
  169. depth=depth_reg+1; node=((2*node_reg)+1); tmp_L=0;
  170. ena_L=0;wea_L=0;enb_L=1;
  171. ena_v=0;wea_v=0; enb_v=0;
  172. n_state=wait_L;
  173. end
  174. wait_L: begin
  175. if(counter==cmax) begin
  176. counter=counter_reg-cmax;
  177. n_state=state_L;
  178. end
  179. else
  180. counter=counter_reg+1;
  181. end
  182. state_L: begin
  183. ena_L=1;wea_L=1;enb_L=0;
  184. ena_v=0;wea_v=0; enb_v=0;
  185. tmp_L=tmp_L_reg+1;
  186. temp_index_f=((N/(2**(depth+1)))*((2*(node)+1)-((2**(depth+1))-1)));
  187. jL1=(tmp_L_reg)+temp_index_f;
  188. jL2=(tmp_L_reg)+temp_index_f+(N/(2**depth));
  189. fminsum_calc(L_out[jL1],L_out[jL2],L_in[jL1]);
  190. if(tmp_L< (N/(2**depth)))
  191. n_state=state_L;
  192. else if(depth<d)
  193. n_state=wait_L_logic;
  194. else
  195. n_state=wait_LRU_logic;
  196. end
  197. wait_R_logic: begin
  198. depth=depth_reg-1; node=node_reg+1; tmp_R=0;
  199. n_state=wait_R;
  200. end
  201. wait_R: begin
  202. ena_L=0;wea_L=0;enb_L=1;
  203. ena_v=0;wea_v=0; enb_v=1;
  204. if(counter==cmax) begin
  205. counter=counter_reg-cmax;
  206. n_state=state_R;
  207. end
  208. else
  209. counter=counter_reg+1;
  210. end
  211. state_R: begin
  212. ena_L=1;wea_L=1;enb_L=0;
  213. ena_v=0;wea_v=0; enb_v=0;
  214. tmp_R=tmp_R_reg+1;
  215. temp_index_f=((N/(2**(depth+1)))*((2*(node)+1)-((2**(depth+1))-1)));
  216. // temp_index_f=((N>>(2**(depth+1)))*((2*(node)+1)-((2**(depth+1))-1)));
  217. temp_index_g=((N/(2**(depth+1)))*((2*(node-1)+1)-((2**(depth+1))-1)));
  218. // temp_index_g=((N>>(2**(depth+1)))*((2*(node-1)+1)-((2**(depth+1))-1)));
  219. jR1=(tmp_R_reg)+temp_index_g;
  220. jR2=(tmp_R_reg)+temp_index_g+(N/(2**depth));
  221. // jR3=(tmp_R_reg)+temp_index_f;
  222. g_calc(L_out[jR1],L_out[jR2],v_out[jR1],L_in[jR3]);
  223. if(tmp_R< (N/(2**depth)))
  224. // if(tmp_R< (N>>(2**depth)))
  225. n_state=state_R;
  226. else if(node==((2**d)-2))
  227. n_state=wait_lnode_logic;
  228. else if(depth==d)
  229. n_state=wait_LRU_logic;
  230. else
  231. n_state = wait_L_logic;
  232. end
  233. wait_U_logic: begin
  234. depth=depth_reg-1; node=(node_reg-2)/2; tmp_U=0;
  235. // depth=depth_reg-1; node=(node_reg-2)>>1; tmp_U=0;
  236. n_state=wait_U;
  237. end
  238. wait_U: begin
  239. ena_L=0;wea_L=0;enb_L=0;
  240. ena_v=0;wea_v=0; enb_v=1;
  241. if(counter==cmax) begin
  242. counter=counter_reg-cmax;
  243. n_state=state_U;
  244. end
  245. else
  246. counter=counter_reg+1;
  247. end
  248. state_U: begin
  249. ena_L=0;wea_L=0;enb_L=0;
  250. ena_v=1;wea_v=1; enb_v=0;
  251. tmp_U=tmp_U_reg+1;
  252. temp_index_f=((N/(2**(depth)))*((2*node+1)-((2**(depth))-1)));
  253. // temp_index_f=((N/(2**(depth)))*(((node+1)<< 1)-((2**(depth))-1)));
  254. jU1=(tmp_U_reg)+temp_index_f;
  255. jU2=(tmp_U_reg)+temp_index_f+(N/(2**(depth)));
  256. v_in[jU1] = v_out[jU1] ^ v_out[jU2];
  257. v_in[jU2] = v_out[jU2];
  258. if(tmp_U<(N/(2**(depth))))
  259. n_state=state_U;
  260. // else if(depth>0 && node%2==0)
  261. else if(depth>0 && node[0]==0)
  262. n_state = wait_U_logic;
  263. else if(depth>0 && node!=0)
  264. n_state=wait_R_logic;
  265. else
  266. n_state=wait_lstate_logic;
  267. end
  268. wait_LRU_logic: begin
  269. // depth=depth_reg; node=(node_reg-1)/2;
  270. depth=depth_reg; node=(node_reg-1)>>1;
  271. n_state=wait_LRU;
  272. end
  273. wait_LRU: begin
  274. ena_L=0;wea_L=0;enb_L=1;
  275. ena_v=0;wea_v=0; enb_v=0;
  276. if(counter==cmax) begin
  277. counter=counter_reg-cmax;
  278. n_state=state_LRU;
  279. end
  280. else
  281. counter=counter_reg+1;
  282. end
  283. state_LRU: begin
  284. ena_L=0;wea_L=0;enb_L=0;
  285. ena_v=1;wea_v=1; enb_v=0;
  286. temp_index_f=((N/(2**(depth)))*((2*node+1)-((2**(depth))-1)));
  287. fminsum_calc(L_out[temp_index_f],L_out[temp_index_f+1],LRU[0]);
  288. // u[(2*node)+2-N]=(f[(2*node)+2-N]==1) ? 0 : ((LRU[0][BITS-1] == 1) ? 1 : 0);
  289. u[(2*node)+2-N]=(f[(2*node)+2-N]) ? 0 : ((LRU[0][BITS-1]) ? 1 : 0);
  290. g_calc(L_out[temp_index_f],L_out[temp_index_f+1],u[(2*node)+2-N],LRU[1]);
  291. // u[(2*node)+3-N]=(f[(2*node)+3-N]==1) ? 0 : ((LRU[1][BITS-1] == 1) ? 1 : 0);
  292. u[(2*node)+3-N]=(f[(2*node)+3-N]) ? 0 : ((LRU[1][BITS-1]) ? 1 : 0);
  293. v_in[temp_index_f]=u[(2*node)+2-N] ^ u[(2*node)+3-N];
  294. v_in[temp_index_f+1]=u[(2*node)+3-N];
  295. // if(node%2==1)
  296. if(node[0])
  297. n_state = wait_R_logic;
  298. else
  299. n_state=wait_U_logic;
  300. end
  301. wait_lnode_logic: begin
  302. depth=depth_reg+1; node=node_reg;
  303. n_state=wait_lnode;
  304. end
  305. wait_lnode: begin
  306. ena_L=0;wea_L=0;enb_L=1;
  307. ena_v=0;wea_v=0; enb_v=0;
  308. if(counter==cmax) begin
  309. counter=counter_reg-cmax;
  310. n_state=state_lnode;
  311. end
  312. else
  313. counter=counter_reg+1;
  314. end
  315. state_lnode: begin
  316. ena_L=0;wea_L=0;enb_L=0;
  317. ena_v=1;wea_v=1; enb_v=0;
  318. temp_index_f=((N/(2**(depth)))*((2*node+1)-((2**(depth))-1)));
  319. fminsum_calc(L_out[temp_index_f],L_out[temp_index_f+1],LRU[0]);
  320. // u[(2*node)+2-N]=(f[(2*node)+2-N]==1) ? 0 : ((LRU[0][BITS-1] == 1) ? 1 : 0);
  321. u[(2*node)+2-N]=(f[(2*node)+2-N]) ? 0 : ((LRU[0][BITS-1]) ? 1 : 0);
  322. g_calc(L_out[temp_index_f],L_out[temp_index_f+1],u[(2*node)+2-N],LRU[1]);
  323. // u[(2*node)+3-N]=(f[(2*node)+3-N]==1) ? 0 : ((LRU[1][BITS-1] == 1) ? 1 : 0);
  324. u[(2*node)+3-N]=(f[(2*node)+3-N]) ? 0 : ((LRU[1][BITS-1]) ? 1 : 0);
  325. v_in[temp_index_f]=u[(2*node)+2-N] ^ u[(2*node)+3-N];
  326. v_in[temp_index_f+1]=u[(2*node)+3-N];
  327. n_state = wait_U_logic;
  328. end
  329. wait_lstate_logic: begin
  330. depth=depth_reg; node=node_reg;
  331. n_state=wait_lstate;
  332. end
  333. wait_lstate: begin
  334. ena_L=0;wea_L=0;enb_L=1;
  335. ena_v=0;wea_v=0; enb_v=0;
  336. if(counter==cmax) begin
  337. counter=counter_reg-cmax;
  338. n_state=state_last;
  339. end
  340. else
  341. counter=counter_reg+1;
  342. end
  343. state_last: begin
  344. ena_L=0;wea_L=0;enb_L=0;
  345. ena_v=1;wea_v=1; enb_v=0;
  346. v=v_out;
  347. n_state=idle;
  348. end
  349. endcase
  350. else n_state = idle;
  351. end
  352. endmodule
  353. /*
  354. `timescale 1ns / 1ns
  355. //////////////////////////////////////////////////////////////////////////////////
  356. // Company:
  357. // Engineer:
  358. //
  359. // Create Date: 02/24/2021 08:03:07 PM
  360. // Design Name:
  361. // Module Name: sc_decoder_fsm
  362. // Project Name:
  363. // Target Devices:
  364. // Tool Versions:
  365. // Description:
  366. //
  367. // Dependencies:
  368. //
  369. // Revision:
  370. // Revision 0.01 - File Created
  371. // Additional Comments:
  372. //
  373. //////////////////////////////////////////////////////////////////////////////////
  374. `define BITS 8
  375. module sc_decoder_fsm #(parameter BITS=8, N=11'd16)(
  376. input clk, rst,
  377. input in_valid,
  378. input signed [N-1:0][BITS-1:0] y,
  379. input [N-1:0]f,
  380. output wire [N-1:0]u_cap,
  381. output wire [N-1:0]v_final,
  382. output wire out_valid
  383. );
  384. //function for fminsum calculation
  385. function void fminsum_calc;
  386. input signed [`BITS-1:0] a;
  387. input signed [`BITS-1:0] b;
  388. output signed [`BITS-1:0] c;
  389. logic [`BITS-2:0] abs_a;
  390. logic [`BITS-2:0] abs_b;
  391. logic [`BITS-2:0] abs_c;
  392. abs_a = (a[`BITS-1] == 1) ? ~a[`BITS-2:0] + 1 : a[`BITS-2:0];
  393. abs_b = (b[`BITS-1] == 1) ? ~b[`BITS-2:0] + 1 : b[`BITS-2:0];
  394. c[`BITS-1] = a[`BITS-1] ^ b[`BITS-1];
  395. abs_c = (abs_b < abs_a) ? abs_b : abs_a;
  396. c[`BITS-2:0] = (c[`BITS-1] == 1) ? ~abs_c + 1 : abs_c;
  397. endfunction
  398. //function for g-value calculation
  399. function void g_calc;
  400. input signed [`BITS-1:0] a;
  401. input signed [`BITS-1:0] b;
  402. input u;
  403. output signed [`BITS:0] c;
  404. c = (u == 0) ? (b + a) : (b + (~a+1));
  405. endfunction
  406. //parameters and signals declarations
  407. localparam d=$clog2(N); //N=4, d=2(0 & 1)
  408. localparam n=2*N-1; //(2**d)-1;
  409. localparam cmax=0;
  410. logic u[N];
  411. logic [d:0]temp_index_f,temp_index_g;
  412. reg signed [BITS-1:0] LRU[2];
  413. reg [N-1:0]v;
  414. logic [N-1:0][BITS-1:0]L_in, L_out;
  415. logic [N-1:0]v_in, v_out;
  416. logic ena_v,enb_v,wea_v;
  417. logic ena_L,enb_L,wea_L;
  418. logic [1:0]counter,counter_reg;
  419. logic [11:0]jL1,jL2,jR1,jR2,jR3,jU1,jU2;
  420. logic [4:0] c_state, n_state;
  421. //Auxiliary registers declarations
  422. logic [d:0] depth,depth_reg;
  423. logic [d:0] node,node_reg;
  424. logic [11:0]tmp_L,tmp_L_reg, tmp_R,tmp_R_reg,tmp_U, tmp_U_reg;
  425. //FSM States
  426. localparam idle=5'd0, root=5'd1, wait_L_logic=5'd2, wait_L=5'd3, state_L=5'd4, wait_R_logic=5'd5, wait_R=5'd6, state_R=5'd7;
  427. localparam wait_U_logic=5'd8, wait_U=5'd9, state_U=5'd10,wait_LRU_logic=5'd11, wait_LRU=5'd12, state_LRU=5'd13;
  428. localparam wait_lnode_logic=5'd14, wait_lnode=5'd15, state_lnode=5'd16,wait_lstate_logic=5'd17, wait_lstate=5'd18, state_last=5'd19;
  429. //BlockRAM Instantiations
  430. bram_v #(.ADDR_WIDTH(d-1),.DATA_WIDTH(N),.DEPTH(2**(d-1))) bram_v_i (
  431. .clk(clk),.ena(ena_v),.enb(enb_v),
  432. .addra(depth_reg-1),
  433. .addrb(depth_reg),
  434. .wea(wea_v),
  435. .dia(v_in),
  436. .dob(v_out)
  437. );
  438. bram_L #(.ADDR_WIDTH(d-1),.DATA_WIDTH(N*BITS),.DEPTH(2**(d-1)),.N(N)) bram_L_i (
  439. .clk(clk),.ena(ena_L),.enb(enb_L),
  440. .addra(depth_reg),
  441. .addrb(depth_reg-1),
  442. .wea(wea_L),
  443. .dia(L_in),
  444. .dob(L_out)
  445. );
  446. //output assignment
  447. for(genvar i=0; i<N; i++)
  448. begin
  449. assign u_cap[i] = u[i];
  450. end
  451. assign v_final=v;
  452. assign out_valid=(n_state==state_last)?1'b1:1'b0;
  453. // Sequential Logic - FSM State and Data Registers
  454. always_ff@(posedge clk)
  455. begin
  456. if(rst==1)
  457. begin
  458. c_state <= idle;
  459. depth_reg<=0;
  460. node_reg<=0;
  461. counter_reg<=0;
  462. tmp_L_reg<=0;
  463. tmp_R_reg<=0;
  464. tmp_U_reg<=0;
  465. end
  466. else
  467. begin
  468. c_state <= n_state;
  469. depth_reg<=depth;
  470. node_reg<=node;
  471. counter_reg<=counter;
  472. tmp_L_reg<=tmp_L;
  473. tmp_R_reg<=tmp_R;
  474. tmp_U_reg<=tmp_U;
  475. end
  476. end
  477. //Combinational Logic - FSM Next State Logic
  478. always_comb
  479. begin
  480. if(in_valid==1)
  481. case(c_state)
  482. idle: begin
  483. depth=0; node=0; counter=0; tmp_L=0; tmp_R=0; tmp_U=0;
  484. ena_L=0;wea_L=0;enb_L=0;
  485. ena_v=0;wea_v=0; enb_v=0;
  486. if(out_valid==1)
  487. n_state=idle;
  488. else
  489. n_state=root;
  490. end
  491. root: begin
  492. depth=depth_reg;node=node_reg;
  493. ena_L=1;wea_L=1;enb_L=0;
  494. ena_v=0;wea_v=0; enb_v=0;
  495. for(int k=0; k<N; k++)
  496. L_in[k]=y[k];
  497. n_state=wait_L_logic;
  498. end
  499. wait_L_logic:
  500. begin
  501. depth=depth_reg+1; node=((2*node_reg)+1); tmp_L=0;
  502. ena_L=0;wea_L=0;enb_L=1;
  503. ena_v=0;wea_v=0; enb_v=0;
  504. n_state=wait_L;
  505. end
  506. wait_L: begin
  507. if(counter==cmax) begin
  508. counter=counter_reg-cmax;
  509. n_state=state_L;
  510. end
  511. else
  512. counter=counter_reg+1;
  513. end
  514. state_L: begin
  515. ena_L=1;wea_L=1;enb_L=0;
  516. ena_v=0;wea_v=0; enb_v=0;
  517. tmp_L=tmp_L_reg+1;
  518. temp_index_f=((N/(2**(depth+1)))*((2*(node)+1)-((2**(depth+1))-1)));
  519. jL1=(tmp_L_reg)+temp_index_f;
  520. jL2=(tmp_L_reg)+temp_index_f+(N/(2**depth));
  521. fminsum_calc(L_out[jL1],L_out[jL2],L_in[jL1]);
  522. if(tmp_L< (N/(2**depth)))
  523. n_state=state_L;
  524. else if(depth<d)
  525. n_state=wait_L_logic;
  526. else
  527. n_state=wait_LRU_logic;
  528. end
  529. wait_R_logic: begin
  530. depth=depth_reg-1; node=node_reg+1; tmp_R=0;
  531. n_state=wait_R;
  532. end
  533. wait_R: begin
  534. ena_L=0;wea_L=0;enb_L=1;
  535. ena_v=0;wea_v=0; enb_v=1;
  536. if(counter==cmax) begin
  537. counter=counter_reg-cmax;
  538. n_state=state_R;
  539. end
  540. else
  541. counter=counter_reg+1;
  542. end
  543. state_R: begin
  544. ena_L=1;wea_L=1;enb_L=0;
  545. ena_v=0;wea_v=0; enb_v=0;
  546. tmp_R=tmp_R_reg+1;
  547. temp_index_f=((N/(2**(depth+1)))*((2*(node)+1)-((2**(depth+1))-1)));
  548. temp_index_g=((N/(2**(depth+1)))*((2*(node-1)+1)-((2**(depth+1))-1)));
  549. jR1=(tmp_R_reg)+temp_index_g;
  550. jR2=(tmp_R_reg)+temp_index_g+(N/(2**depth));
  551. jR3=(tmp_R_reg)+temp_index_f;
  552. g_calc(L_out[jR1],L_out[jR2],v_out[jR1],L_in[jR3]);
  553. if(tmp_R< (N/(2**depth)))
  554. n_state=state_R;
  555. else if(node==((2**d)-2))
  556. n_state=wait_lnode_logic;
  557. else if(depth==d)
  558. n_state=wait_LRU_logic;
  559. else
  560. n_state = wait_L_logic;
  561. end
  562. wait_U_logic: begin
  563. depth=depth_reg-1; node=(node_reg-2)/2; tmp_U=0;
  564. n_state=wait_U;
  565. end
  566. wait_U: begin
  567. ena_L=0;wea_L=0;enb_L=0;
  568. ena_v=0;wea_v=0; enb_v=1;
  569. if(counter==cmax) begin
  570. counter=counter_reg-cmax;
  571. n_state=state_U;
  572. end
  573. else
  574. counter=counter_reg+1;
  575. end
  576. state_U: begin
  577. ena_L=0;wea_L=0;enb_L=0;
  578. ena_v=1;wea_v=1; enb_v=0;
  579. tmp_U=tmp_U_reg+1;
  580. temp_index_f=((N/(2**(depth)))*((2*node+1)-((2**(depth))-1)));
  581. jU1=(tmp_U_reg)+temp_index_f;
  582. jU2=(tmp_U_reg)+temp_index_f+(N/(2**(depth)));
  583. v_in[jU1] = v_out[jU1] ^ v_out[jU2];
  584. v_in[jU2] = v_out[jU2];
  585. if(tmp_U<(N/(2**(depth))))
  586. n_state=state_U;
  587. else if(depth>0 && node%2==0)
  588. n_state = wait_U_logic;
  589. else if(depth>0 && node!=0)
  590. n_state=wait_R_logic;
  591. else
  592. n_state=wait_lstate_logic;
  593. end
  594. wait_LRU_logic: begin
  595. depth=depth_reg; node=(node_reg-1)/2;
  596. n_state=wait_LRU;
  597. end
  598. wait_LRU: begin
  599. ena_L=0;wea_L=0;enb_L=1;
  600. ena_v=0;wea_v=0; enb_v=0;
  601. if(counter==cmax) begin
  602. counter=counter_reg-cmax;
  603. n_state=state_LRU;
  604. end
  605. else
  606. counter=counter_reg+1;
  607. end
  608. state_LRU: begin
  609. ena_L=0;wea_L=0;enb_L=0;
  610. ena_v=1;wea_v=1; enb_v=0;
  611. temp_index_f=((N/(2**(depth)))*((2*node+1)-((2**(depth))-1)));
  612. fminsum_calc(L_out[temp_index_f],L_out[temp_index_f+1],LRU[0]);
  613. u[(2*node)+2-N]=(f[(2*node)+2-N]==1) ? 0 : ((LRU[0][BITS-1] == 1) ? 1 : 0);
  614. g_calc(L_out[temp_index_f],L_out[temp_index_f+1],u[(2*node)+2-N],LRU[1]);
  615. u[(2*node)+3-N]=(f[(2*node)+3-N]==1) ? 0 : ((LRU[1][BITS-1] == 1) ? 1 : 0);
  616. v_in[temp_index_f]=u[(2*node)+2-N] ^ u[(2*node)+3-N];
  617. v_in[temp_index_f+1]=u[(2*node)+3-N];
  618. if(node%2==1)
  619. n_state = wait_R_logic;
  620. else
  621. n_state=wait_U_logic;
  622. end
  623. wait_lnode_logic: begin
  624. depth=depth_reg+1; node=node_reg;
  625. n_state=wait_lnode;
  626. end
  627. wait_lnode: begin
  628. ena_L=0;wea_L=0;enb_L=1;
  629. ena_v=0;wea_v=0; enb_v=0;
  630. if(counter==cmax) begin
  631. counter=counter_reg-cmax;
  632. n_state=state_lnode;
  633. end
  634. else
  635. counter=counter_reg+1;
  636. end
  637. state_lnode: begin
  638. ena_L=0;wea_L=0;enb_L=0;
  639. ena_v=1;wea_v=1; enb_v=0;
  640. temp_index_f=((N/(2**(depth)))*((2*node+1)-((2**(depth))-1)));
  641. fminsum_calc(L_out[temp_index_f],L_out[temp_index_f+1],LRU[0]);
  642. u[(2*node)+2-N]=(f[(2*node)+2-N]==1) ? 0 : ((LRU[0][BITS-1] == 1) ? 1 : 0);
  643. g_calc(L_out[temp_index_f],L_out[temp_index_f+1],u[(2*node)+2-N],LRU[1]);
  644. u[(2*node)+3-N]=(f[(2*node)+3-N]==1) ? 0 : ((LRU[1][BITS-1] == 1) ? 1 : 0);
  645. v_in[temp_index_f]=u[(2*node)+2-N] ^ u[(2*node)+3-N];
  646. v_in[temp_index_f+1]=u[(2*node)+3-N];
  647. n_state = wait_U_logic;
  648. end
  649. wait_lstate_logic: begin
  650. depth=depth_reg; node=node_reg;
  651. n_state=wait_lstate;
  652. end
  653. wait_lstate: begin
  654. ena_L=0;wea_L=0;enb_L=1;
  655. ena_v=0;wea_v=0; enb_v=0;
  656. if(counter==cmax) begin
  657. counter=counter_reg-cmax;
  658. n_state=state_last;
  659. end
  660. else
  661. counter=counter_reg+1;
  662. end
  663. state_last: begin
  664. ena_L=0;wea_L=0;enb_L=0;
  665. ena_v=1;wea_v=1; enb_v=0;
  666. v=v_out;
  667. n_state=idle;
  668. end
  669. endcase
  670. else n_state = idle;
  671. end
  672. endmodule
  673. */