Guest User

Untitled

a guest
Jan 10th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.84 KB | None | 0 0
  1. `timescale 1ns / 1ps
  2. //////////////////////////////////////////////////////////////////////////////////
  3. // Company:
  4. // Engineer:
  5. //
  6. // Create Date: 20:14:45 11/26/2011
  7. // Design Name:
  8. // Module Name: uc
  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. module uc(
  22. clk,
  23. rst,
  24. ri,
  25. ind,
  26. regs_addr,
  27. regs_oe,
  28. regs_we,
  29. alu_oe,
  30. alu_carry,
  31. alu_opcode,
  32. ram_oe,
  33. ram_we,
  34. io_oe,
  35. io_we,
  36. cp_oe,
  37. cp_we,
  38. ind_sel,
  39. ind_oe,
  40. ind_we,
  41. am_oe,
  42. am_we,
  43. aie_oe,
  44. aie_we,
  45. t1_oe,
  46. t1_we,
  47. t2_oe,
  48. t2_we,
  49. ri_oe,
  50. ri_we,
  51. disp_state
  52. );
  53.  
  54. parameter word_width = 16;
  55. parameter state_width = 16;
  56.  
  57. `define ADC 0
  58. `define SBB1 1
  59. `define SBB2 2
  60. `define NOT 3
  61. `define AND 4
  62. `define OR 5
  63. `define XOR 6
  64. `define SHL 7
  65. `define SHR 8
  66. `define SAR 9
  67.  
  68. `define RA 0
  69. `define RB 1
  70. `define RC 2
  71. `define IS 3
  72. `define XA 4
  73. `define XB 5
  74. `define BA 6
  75. `define BB 7
  76.  
  77. input clk;
  78. input rst;
  79. input [word_width-1 : 0] ri;
  80. input [word_width-1 : 0] ind;
  81. output reg alu_oe;
  82. output reg alu_carry;
  83. output reg[3 : 0] alu_opcode;
  84. output reg ram_oe;
  85. output reg ram_we;
  86. output reg io_oe;
  87. output reg io_we;
  88. output reg[2 : 0] regs_addr;
  89. output reg regs_oe;
  90. output reg regs_we;
  91. output reg cp_oe;
  92. output reg cp_we;
  93. output reg ind_sel; // controls IND register input (0 = bus, 1 = alu flags)
  94. output reg ind_oe;
  95. output reg ind_we;
  96. output reg am_oe;
  97. output reg am_we;
  98. output reg aie_oe;
  99. output reg aie_we;
  100. output reg t1_oe;
  101. output reg t1_we;
  102. output reg t2_oe;
  103. output reg t2_we;
  104. output reg ri_oe; // controls RI register output which generates the offset for Jcond instructions
  105. output reg ri_we;
  106. output[state_width-1 : 0] disp_state;
  107.  
  108. wire [0:6] cop;
  109. wire d;
  110. wire [0:1] mod;
  111. wire [0:2] rg;
  112. wire [0:2] rm;
  113.  
  114. assign cop = {ri[0], ri[1], ri[2], ri[3], ri[4], ri[5], ri[6]};
  115. assign d = {ri[7]};
  116. assign mod = {ri[8], ri[9]};
  117. assign rg = {ri[10], ri[11], ri[12]};
  118. assign rm = {ri[13], ri[14], ri[15]};
  119.  
  120. `define reset 'h00 // reset state
  121. `define fetch 'h10 // load instruction to instruction register
  122. `define decode 'h20 // analyze loaded instruction
  123. `define addr_sum 'h30 // computes address of the form [By+Xz] with y,z in {A, B}
  124. `define addr_reg 'h34 // computes address of the form [yz] with y in {X, B} and z in {A, B}
  125. `define load_src_reg 'h40 // load source operand from register
  126. `define load_src_mem 'h44 // load source operand from memory
  127. `define load_dst_reg 'h50 // load destination operand from register
  128. `define load_dst_mem 'h54 // load destination operand from memory
  129. `define exec_1op 'h60 // execute 1 operand instructions
  130. `define exec_2op 'h64 // execute 2 operand instructions
  131. `define store_reg 'h70 // store result to register
  132. `define store_mem 'h74 // store result to memory
  133. `define inc_cp 'h80 // increment program counter
  134.  
  135. reg [state_width-1 : 0] state = `reset, state_next;
  136. reg [state_width-1 : 0] decoded_src, decoded_src_next; // stores decoded source operand load state
  137. reg [state_width-1 : 0] decoded_dst, decoded_dst_next; // stores decoded destination operand load state
  138. reg [state_width-1 : 0] decoded_exec, decoded_exec_next; // stores decoded execute state
  139. reg [state_width-1 : 0] decoded_store, decoded_store_next; // stores decoded store state
  140. reg decoded_d, decoded_d_next; // stores decoded direction bit
  141.  
  142. // FSM - sequential part
  143. always @(posedge clk) begin
  144. state <= `reset;
  145.  
  146. if(!rst) begin
  147. state <= state_next;
  148.  
  149. if(state == `decode) begin
  150. decoded_src <= decoded_src_next;
  151. decoded_dst <= decoded_dst_next;
  152. decoded_exec <= decoded_exec_next;
  153. decoded_store <= decoded_store_next;
  154. decoded_d <= decoded_d_next;
  155. end
  156. end
  157. end
  158.  
  159. // FSM - combinational part
  160. always @(*) begin
  161. state_next = `reset;
  162. decoded_src_next = `reset;
  163. decoded_dst_next = `reset;
  164. decoded_exec_next = `reset;
  165. decoded_store_next = `reset;
  166. decoded_d_next = 0;
  167. alu_oe = 0;
  168. alu_carry = 0;
  169. alu_opcode = 0;
  170. ram_oe = 0;
  171. ram_we = 0;
  172. io_oe = 0;
  173. io_we = 0;
  174. regs_addr = 0;
  175. regs_oe = 0;
  176. regs_we = 0;
  177. cp_oe = 0;
  178. cp_we = 0;
  179. ind_sel = 0;
  180. ind_oe = 0;
  181. ind_we = 0;
  182. am_oe = 0;
  183. am_we = 0;
  184. aie_oe = 0;
  185. aie_we = 0;
  186. t1_oe = 0;
  187. t1_we = 0;
  188. t2_oe = 0;
  189. t2_we = 0;
  190. ri_oe = 0;
  191. ri_we = 0;
  192.  
  193. case(state)
  194. `reset: begin
  195. state_next = `fetch;
  196. end
  197.  
  198. `fetch: begin
  199. cp_oe = 1;
  200. am_we = 1;
  201.  
  202. state_next = `fetch + 1;
  203. end
  204.  
  205. `fetch + 'd1: begin
  206. am_oe = 1;
  207.  
  208. state_next = `fetch + 2;
  209. end
  210.  
  211. `fetch + 'd2: begin
  212. ram_oe = 1;
  213. ri_we = 1;
  214.  
  215. state_next = `decode;
  216. end
  217.  
  218. `decode: begin
  219. // decode location of operands and operation
  220. if(cop[0:3] == 4'b0001) begin // one operand instructions
  221. decoded_d_next = 0;
  222. decoded_dst_next = mod == 2'b11 ? `load_dst_reg : `load_dst_mem;
  223. decoded_src_next = decoded_dst_next;
  224. decoded_exec_next = `exec_1op;
  225. decoded_store_next = mod == 2'b11 ? `store_reg : `store_mem;
  226. end
  227. else if(cop[0:3] == 4'b0000) begin
  228. decoded_d_next = d;
  229. decoded_dst_next = (mod == 2'b11) || (d == 1) ? `load_dst_reg : `load_dst_mem;
  230. decoded_src_next = (mod == 2'b11) || (d == 0) ? `load_src_reg : `load_src_mem;
  231. decoded_store_next = (mod == 2'b11) || (d == 1) ? `store_reg : `store_mem;
  232. end
  233. else if(cop[0:2] == 3'b010) begin // two operand instructions
  234. decoded_d_next = d;
  235. decoded_dst_next = (mod == 2'b11) || (d == 1) ? `load_dst_reg : `load_dst_mem;
  236. decoded_src_next = (mod == 2'b11) || (d == 0) ? `load_src_reg : `load_src_mem;
  237. decoded_exec_next = `exec_2op;
  238. decoded_store_next = !cop[3] ? `inc_cp : ((mod == 2'b11) || (d == 1) ? `store_reg : `store_mem);
  239. end
  240.  
  241. // decode address calculation mode
  242. case(mod)
  243. 2'b00: begin
  244. state_next = rm[0] ? `addr_reg : `addr_sum;
  245. end
  246.  
  247. 2'b11: begin
  248. state_next = decoded_src_next;
  249. end
  250. endcase
  251. end
  252.  
  253. `addr_sum: begin
  254. regs_addr = rm[1] ? `BB : `BA;
  255. regs_oe = 1;
  256. t1_we = 1;
  257.  
  258. state_next = `addr_sum + 1;
  259. end
  260.  
  261. `addr_sum + 'd1: begin
  262. regs_addr = rm[2] ? `XB : `XA;
  263. regs_oe = 1;
  264. t2_we = 1;
  265.  
  266. state_next = `addr_sum + 2;
  267. end
  268.  
  269. `addr_sum + 'd2: begin
  270. t1_oe = 1;
  271. t2_oe = 1;
  272. alu_carry = 0;
  273. alu_opcode = `ADC;
  274. alu_oe = 1;
  275. if(decoded_d)
  276. t2_we = 1;
  277. else
  278. t1_we = 1;
  279.  
  280. state_next = decoded_src;
  281. end
  282.  
  283. `addr_reg: begin
  284. regs_addr = rm;
  285. regs_oe = 1;
  286. if(decoded_d)
  287. t2_we = 1;
  288. else
  289. t1_we = 1;
  290.  
  291. state_next = decoded_src;
  292. end
  293.  
  294. `load_src_reg: begin
  295. regs_addr = decoded_d ? rm : rg;
  296. regs_oe = 1;
  297. if(cop[0:3] == 4'b0000) begin
  298. t1_we = 1;
  299. state_next = decoded_store;
  300. end else begin
  301. t2_we = 1;
  302. state_next = decoded_dst;
  303. end
  304. end
  305.  
  306. `load_src_mem: begin
  307. t1_oe = 0;
  308. t2_oe = 1;
  309. alu_opcode = `OR;
  310. alu_oe = 1;
  311. am_we = 1;
  312.  
  313. state_next = `load_src_mem + 1;
  314. end
  315.  
  316. `load_src_mem + 'd1: begin
  317. am_oe = 1;
  318.  
  319. state_next = `load_src_mem + 2;
  320. end
  321.  
  322. `load_src_mem + 'd2: begin
  323. ram_oe = 1;
  324. t2_we = 1;
  325.  
  326. state_next = decoded_dst;
  327. end
  328.  
  329. `load_dst_reg: begin
  330. regs_addr = decoded_d ? rg : rm;
  331. regs_oe = 1;
  332. t1_we = 1;
  333.  
  334. state_next = decoded_exec;
  335. end
  336.  
  337. `load_dst_mem: begin
  338. t1_oe = 1;
  339. t2_oe = 0;
  340. alu_opcode = `OR;
  341. alu_oe = 1;
  342. am_we = 1;
  343.  
  344. state_next = `load_dst_mem + 1;
  345. end
  346.  
  347. `load_dst_mem + 'd1: begin
  348. am_oe = 1;
  349.  
  350. state_next = `load_dst_mem + 2;
  351. end
  352.  
  353. `load_dst_mem + 'd2: begin
  354. ram_oe = 1;
  355. t1_we = 1;
  356.  
  357. state_next = decoded_exec;
  358. end
  359.  
  360. `exec_1op: begin
  361. t1_oe = 1;
  362. case(cop[4:6])
  363. 3'b000: begin // INC
  364. alu_carry = 1;
  365. alu_opcode = `ADC;
  366. end
  367. 3'b001: begin // DEC
  368. alu_carry = 1;
  369. alu_opcode = `SBB1;
  370. end
  371. 3'b010: begin // NEG
  372. alu_carry = 0;
  373. alu_opcode = `SBB2;
  374. end
  375. 3'b011: begin // NOT
  376. alu_opcode = `NOT;
  377. end
  378. 3'b100: alu_opcode = `SHL; // SHL/SAL
  379. 3'b101: alu_opcode = `SHR; // SHR
  380. 3'b110: alu_opcode = `SAR; // SAR
  381. endcase
  382. alu_oe = 1;
  383. t1_we = 1;
  384. ind_sel = 1;
  385. ind_we = 1;
  386.  
  387. state_next = decoded_store;
  388. end
  389.  
  390. `exec_2op: begin
  391. t1_oe = 1;
  392. t2_oe = 1;
  393. case(cop[4:6])
  394. 3'b000: begin // ADD
  395. alu_carry = 0;
  396. alu_opcode = `ADC;
  397. end
  398. 3'b001: begin // ADC
  399. alu_carry = ind[0];
  400. alu_opcode = `ADC;
  401. end
  402. 3'b010: begin // SUB/CMP
  403. alu_carry = 0;
  404. alu_opcode = `SBB1;
  405. end
  406. 3'b011: begin // SBB
  407. alu_carry = ind[0];
  408. alu_opcode = `SBB1;
  409. end
  410. 3'b100: alu_opcode = `AND; // AND/TEST
  411. 3'b101: alu_opcode = `OR; // OR
  412. 3'b110: alu_opcode = `XOR; // XOR
  413. endcase
  414. alu_oe = 1;
  415. t1_we = 1;
  416. ind_sel = 1;
  417. ind_we = 1;
  418.  
  419. state_next = decoded_store;
  420. end
  421.  
  422. `store_reg: begin
  423. t1_oe = 1;
  424. t2_oe = 0;
  425. alu_opcode = `OR;
  426. alu_oe = 1;
  427. regs_addr = decoded_d ? rg : rm;
  428. regs_we = 1;
  429.  
  430. state_next = `inc_cp;
  431. end
  432.  
  433. `store_mem: begin
  434. t1_oe = 1;
  435. t2_oe = 0;
  436. alu_opcode = `OR;
  437. alu_oe = 1;
  438. am_oe = 1;
  439. ram_we = 1;
  440.  
  441. state_next = `store_mem + 1;
  442. end
  443.  
  444. `store_mem + 'd1: begin
  445. state_next = `inc_cp;
  446. end
  447.  
  448. `inc_cp: begin
  449. cp_oe = 1;
  450. t1_we = 1;
  451.  
  452. state_next = `inc_cp + 1;
  453. end
  454.  
  455. `inc_cp + 'd1: begin
  456. t1_oe = 1;
  457. cp_we = 1;
  458. alu_oe = 1;
  459. alu_carry = 1;
  460. alu_opcode = `ADC;
  461.  
  462. state_next = `fetch;
  463. end
  464.  
  465. default: ;
  466. endcase
  467. end
  468.  
  469. assign disp_state = state;
  470.  
  471. endmodule
Advertisement
Add Comment
Please, Sign In to add comment