1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
| `define NOP 5'b00000 `define HALT 5'b00001 `define LOAD 5'b00010 `define STORE 5'b00011 `define ADD 5'b01000 `define CMP 5'b01100 `define BZ 5'b11010 `define BN 5'b11100
`define SUB 5'b01010 `define SUBC 5'b10010 `define ADDC 5'b10001 `define ADDI 5'b01001 `define SUBI 5'b01011 `define LDIH 5'b10000
`define AND 5'b01101 `define OR 5'b01110 `define XOR 5'b01111 `define SLL 5'b00100 `define SRL 5'b00101 `define SLA 5'b00110 `define SRA 5'b00111
`define JUMP 5'b11000 `define JMPR 5'b11001 `define BNZ 5'b11011 `define BNN 5'b11101 `define BC 5'b11110 `define BNC 5'b11111
`define LWC_CLEAR 5'b10011 `define LWC_CA 5'b10100 `define LWC_ENCRYPT 5'b10101 `define LWC_INTERRUPT 5'b10110 `define LWC_CALLBACK 5'b10111
`define idle 2'b00 `define exec 2'b01 `define interrupt 2'b10
module pcpu (reset, clock, enable, start, i_addr, i_datain, i_datain_interrupt, d_addr, d_datain, d_dataout, d_we, select_y, y);
input reset, clock, enable, start; input [15:0] i_datain; input [15:0] i_datain_interrupt; output [7:0] i_addr; output [7:0] d_addr; input [15:0] d_datain; output [15:0] d_dataout; output d_we;
input [3:0] select_y; output [15:0] y;
reg [7:0] pc ; reg [15:0] id_ir, ex_ir, mem_ir, wb_ir; reg [15:0] gr [0:7]; reg [15:0] reg_A, reg_B, reg_C, reg_C1; reg [15:0] smdr, smdr1; reg zf, nf, dw;
reg cf; reg[7:0] pc_interrupt; reg inter_flag;
reg[1:0] state;
reg [15:0] ALUo; reg [15:0] y; reg[1:0] next_state;
assign i_addr = pc; assign d_we = dw; assign d_addr = reg_C[7:0]; assign d_dataout = smdr1;
always @(posedge clock or negedge reset) begin if (!reset) state <= `idle; else state <= next_state; end
always @(state or enable or start or wb_ir[15:11]) begin case (state) `idle : if ((enable == 1'b1) && (start == 1'b1)) next_state <= `exec; else next_state <= `idle; `exec : if ((enable == 1'b0) || (wb_ir[15:11] == `HALT)) next_state <= `idle; else if ((enable == 1'b0) || (id_ir[15:11] == `LWC_INTERRUPT)) next_state <= `interrupt; else next_state <= `exec; `interrupt: if (id_ir[15:11] == `LWC_CALLBACK) next_state <= `exec; else next_state <= `interrupt; endcase end
always @(posedge clock or negedge reset) begin if (!reset) begin id_ir <= 16'b0000000000000000; pc <= 8'b00000000; pc_interrupt <= 8'b00000000; inter_flag <= 0; end else if(state==`exec||state==`interrupt) begin id_ir <= (state == `interrupt) ? i_datain_interrupt : i_datain; if ( ((mem_ir[15:11] == `BZ) && (zf == 1'b1)) || ((mem_ir[15:11] == `BN) && (nf == 1'b1)) || (mem_ir[15:11] == `JMPR) || ((mem_ir[15:11] == `BNZ) && (zf == 1'b0)) || ((mem_ir[15:11] == `BNN) && (nf == 1'b0)) || ((mem_ir[15:11] == `BC) && (cf == 1'b1)) || ((mem_ir[15:11] == `BNC) && (cf == 1'b0)) ) pc <= reg_C[7:0]; else if (id_ir[15:11] == `JUMP) pc <= id_ir[7:0]; else if (id_ir[15:11] == `LWC_INTERRUPT) begin pc <= 0; if(inter_flag == 0) begin pc_interrupt <= pc; inter_flag <= 1; end end else if (id_ir[15:11] == `LWC_CALLBACK) begin pc <= pc_interrupt; end else pc <= pc + 1; end else if(state==`idle) pc <= pc; else ; end
always @(posedge clock or negedge reset) begin if (!reset) begin ex_ir <= 16'b0000000000000000; reg_A <= 16'b0000000000000000; reg_B <= 16'b0000000000000000; smdr <= 16'b0000000000000000; end else if(state==`exec||state==`interrupt) begin ex_ir <= id_ir; if (id_ir[15:11] == `LWC_CA) begin gr[0] = 16'b0000000000000000; gr[1] = 16'b0000000000000000; gr[2] = 16'b0000000000000000; gr[3] = 16'b0000000000000000; gr[4] = 16'b0000000000000000; gr[5] = 16'b0000000000000000; gr[6] = 16'b0000000000000000; gr[7] = 16'b0000000000000000; end else if (id_ir[15:11] == `LWC_CLEAR) begin gr[id_ir[3:0]] = 16'b0000000000000000; end else ;
if ((id_ir[15:11] == `ADDI) || (id_ir[15:11] == `SUBI) || (id_ir[15:11] == `LDIH) || (id_ir[15:11] == `BZ) || (id_ir[15:11] == `BN) || (id_ir[15:11] == `JMPR) || (id_ir[15:11] == `BNZ) || (id_ir[15:11] == `BNN) || (id_ir[15:11] == `BC) || (id_ir[15:11] == `BNC)) begin if ((id_ir[10:8] == ex_ir[10:8]) && (ex_ir[15:11]!=`NOP) && (ex_ir[15:11]!=`CMP) && (ex_ir[15:11]!=`JUMP) && (ex_ir[15:11]!=`LOAD) && (ex_ir[15:11]!=`HALT)) reg_A <= ((id_ir[15:11] == `BZ) || (id_ir[15:11] == `BN) || (id_ir[15:11] == `JMPR) || (id_ir[15:11] == `BNZ) || (id_ir[15:11] == `BNN) || (id_ir[15:11] == `BC) || (id_ir[15:11] == `BNC)) ? gr[(id_ir[10:8])]: ALUo; else if ((id_ir[10:8] == mem_ir[10:8]) && (mem_ir[15:11]!=`NOP) && (mem_ir[15:11]!=`CMP) && (mem_ir[15:11]!=`JUMP) && (mem_ir[15:11]!=`HALT)) reg_A <= (mem_ir[15:11] == `LOAD) ? d_datain : reg_C; else if ((id_ir[10:8] == wb_ir[10:8]) && (wb_ir[15:11]!=`NOP) && (wb_ir[15:11]!=`CMP) && (wb_ir[15:11]!=`JUMP) && (wb_ir[15:11]!=`HALT)) reg_A <= reg_C1; else reg_A <= gr[(id_ir[10:8])]; end else if ((id_ir[15:11] == `LOAD) || (id_ir[15:11] == `STORE) || (id_ir[15:11] == `ADD) || (id_ir[15:11] == `CMP) || (id_ir[15:11] == `ADDC) || (id_ir[15:11] == `SUB) || (id_ir[15:11] == `SUBC) || (id_ir[15:11] == `AND) || (id_ir[15:11] == `OR) || (id_ir[15:11] == `XOR) || (id_ir[15:11] == `SLL) || (id_ir[15:11] == `SRL) || (id_ir[15:11] == `SLA) || (id_ir[15:11] == `SRA) || (id_ir[15:11] == `LWC_ENCRYPT)) begin if ((id_ir[6:4] == ex_ir[10:8]) && (ex_ir[15:11]!=`NOP) && (ex_ir[15:11]!=`CMP) && (ex_ir[15:11]!=`JUMP) && (ex_ir[15:11]!=`LOAD) && (ex_ir[15:11]!=`HALT)) reg_A <= ALUo; else if ((id_ir[6:4] == mem_ir[10:8]) && (mem_ir[15:11]!=`NOP) && (mem_ir[15:11]!=`CMP) && (mem_ir[15:11]!=`JUMP) && (mem_ir[15:11]!=`HALT)) reg_A <= (mem_ir[15:11] == `LOAD) ? d_datain : reg_C; else if ((id_ir[6:4] == wb_ir[10:8]) && (wb_ir[15:11]!=`NOP) && (wb_ir[15:11]!=`CMP) && (wb_ir[15:11]!=`JUMP) && (wb_ir[15:11]!=`HALT)) reg_A <= reg_C1; else reg_A <= gr[(id_ir[6:4])]; end else if (((mem_ir[15:11] == `BZ) && (zf == 1'b1)) || ((mem_ir[15:11] == `BN) && (nf == 1'b1)) || ((mem_ir[15:11] == `BNZ) && (zf == 1'b0))|| ((mem_ir[15:11] == `BNN) && (nf == 1'b0))|| ((mem_ir[15:11] == `BC) && (cf == 1'b1)) || ((mem_ir[15:11] == `BNC) && (cf == 1'b0))|| mem_ir[15:11] == `JMPR) reg_A <= 16'b0000_0000_0000_0000; else ;
if (id_ir[15:11] == `LOAD) reg_B <= {12'b000000000000, id_ir[3:0]}; else if (id_ir[15:11] == `STORE) begin reg_B <= {12'b000000000000, id_ir[3:0]}; if ((id_ir[10:8] == ex_ir[10:8]) && (ex_ir[15:11]!=`NOP) && (ex_ir[15:11]!=`CMP) && (ex_ir[15:11]!=`JUMP) && (ex_ir[15:11]!=`LOAD) && (ex_ir[15:11]!=`HALT)) smdr <= ALUo; else if ((id_ir[10:8] == mem_ir[10:8]) && (mem_ir[15:11]!=`NOP) && (mem_ir[15:11]!=`CMP) && (mem_ir[15:11]!=`JUMP) && (mem_ir[15:11]!=`HALT)) smdr <= (mem_ir[15:11] == `LOAD) ? d_datain : reg_C; else if ((id_ir[10:8] == wb_ir[10:8]) && (wb_ir[15:11]!=`NOP) && (wb_ir[15:11]!=`CMP) && (wb_ir[15:11]!=`JUMP) && (wb_ir[15:11]!=`HALT)) smdr <= reg_C1; else smdr <= gr[id_ir[10:8]]; end else if ((id_ir[15:11] == `SLL) || (id_ir[15:11] == `SRL) || (id_ir[15:11] == `SLA) || (id_ir[15:11] == `SRA)) reg_B <= {12'b000000000000, id_ir[3:0]}; else if ((id_ir[15:11] == `ADDI) || (id_ir[15:11] == `SUBI) || (id_ir[15:11] == `BZ) || (id_ir[15:11] == `BN) || (id_ir[15:11] == `JMPR) || (id_ir[15:11] == `BNZ) || (id_ir[15:11] == `BNN) || (id_ir[15:11] == `BC) || (id_ir[15:11] == `BNC)) reg_B <= {8'b00000000, id_ir[7:0]}; else if (id_ir[15:11] == `LDIH) reg_B <= {id_ir[7:0], 8'b00000000}; else if ((id_ir[15:11] == `ADD) || (id_ir[15:11] == `CMP) || (id_ir[15:11] == `ADDC) || (id_ir[15:11] == `SUB) || (id_ir[15:11] == `SUBC) || (id_ir[15:11] == `AND) || (id_ir[15:11] == `OR) || (id_ir[15:11] == `XOR) || (id_ir[15:11] == `LWC_ENCRYPT)) begin if ((id_ir[2:0] == ex_ir[10:8]) && (ex_ir[15:11]!=`NOP) && (ex_ir[15:11]!=`CMP) && (ex_ir[15:11]!=`JUMP) && (ex_ir[15:11]!=`LOAD) && (ex_ir[15:11]!=`HALT)) reg_B <= ALUo; else if ((id_ir[2:0] == mem_ir[10:8]) && (mem_ir[15:11]!=`NOP) && (mem_ir[15:11]!=`CMP) && (mem_ir[15:11]!=`JUMP) && (mem_ir[15:11]!=`HALT)) reg_B <= (mem_ir[15:11] == `LOAD) ? d_datain : reg_C; else if ((id_ir[2:0] == wb_ir[10:8]) && (wb_ir[15:11]!=`NOP) && (wb_ir[15:11]!=`CMP) && (wb_ir[15:11]!=`JUMP) && (wb_ir[15:11]!=`HALT)) reg_B <= reg_C1; else reg_B <= gr[id_ir[2:0]]; end else if (((mem_ir[15:11] == `BZ) && (zf == 1'b1)) || ((mem_ir[15:11] == `BN) && (nf == 1'b1))|| ((mem_ir[15:11] == `BNZ) && (zf == 1'b0)) || ((mem_ir[15:11] == `BNN) && (nf == 1'b0))|| ((mem_ir[15:11] == `BC) && (cf == 1'b1)) || ((mem_ir[15:11] == `BNC) && (cf == 1'b0))|| mem_ir[15:11] == `JMPR) reg_B <= 16'b0000_0000_0000_0000; else ; end end
always @(posedge clock or negedge reset) begin if (!reset) begin mem_ir <= 16'b0000000000000000; reg_C <= 16'b0000000000000000; smdr1 <= 16'b0000000000000000; zf <= 1'b0 ; nf <= 1'b0 ; dw <= 1'b0 ; end else if(state==`exec||state==`interrupt) begin mem_ir <= ex_ir; reg_C <= ALUo; smdr1 <= smdr; if ((ex_ir[15:11] == `ADDI) || (ex_ir[15:11] == `SUBI) || (ex_ir[15:11] == `LDIH) || (ex_ir[15:11] == `ADD) || (ex_ir[15:11] == `CMP) || (ex_ir[15:11] == `ADDC) || (ex_ir[15:11] == `SUB) || (ex_ir[15:11] == `SUBC) || (ex_ir[15:11] == `AND) || (ex_ir[15:11] == `OR) || (ex_ir[15:11] == `XOR) || (ex_ir[15:11] == `SLL) || (ex_ir[15:11] == `SRL) || (ex_ir[15:11] == `SLA) || (ex_ir[15:11] == `SRA)) begin if (ALUo == 16'b0000000000000000) zf <= 1'b1; else zf <= 1'b0; if (ALUo [15] == 1'b1) nf <= 1'b1; else nf <= 1'b0; end
if (ex_ir[15:11] == `STORE) begin dw <= 1'b1; end else dw <= 1'b0; end end
always @(posedge clock or negedge reset) begin if (!reset) begin wb_ir <= 16'b0000000000000000; reg_C1 <= 16'b0000000000000000; end else if(state==`exec||state == `interrupt) begin wb_ir <= mem_ir; if (mem_ir[15:11] == `LOAD) reg_C1 <= d_datain; else reg_C1 <= reg_C; end end
always @(posedge clock or negedge reset) begin if (!reset) begin gr[0] <= 16'b0000000000000000; gr[1] <= 16'b0000000000000000; gr[2] <= 16'b0000000000000000; gr[3] <= 16'b0000000000000000; gr[4] <= 16'b0000000000000000; gr[5] <= 16'b0000000000000000; gr[6] <= 16'b0000000000000000; gr[7] <= 16'b0000000000000000; end else if(state==`exec||state==`interrupt) begin if (wb_ir[10:8] != 3'b000) if ((wb_ir[15:11] == `LOAD) || (wb_ir[15:11] == `ADDI) || (wb_ir[15:11] == `SUBI) || (wb_ir[15:11] == `LDIH) || (wb_ir[15:11] == `ADD) || (wb_ir[15:11] == `CMP) || (wb_ir[15:11] == `ADDC) || (wb_ir[15:11] == `SUB) || (wb_ir[15:11] == `SUBC) || (wb_ir[15:11] == `AND) || (wb_ir[15:11] == `OR) || (wb_ir[15:11] == `XOR) || (wb_ir[15:11] == `SLL) || (wb_ir[15:11] == `SRL) || (wb_ir[15:11] == `SLA) || (wb_ir[15:11] == `SRA) || (wb_ir[15:11] == `LWC_ENCRYPT)) gr[wb_ir[10:8]] <= reg_C1; else gr[wb_ir[10:8]] <= gr[wb_ir[10:8]]; end end
always @(reg_A or reg_B or ex_ir[15:11]) case (ex_ir[15:11]) `LOAD : ALUo <= reg_A + reg_B; `STORE : ALUo <= reg_A + reg_B; `ADD : {cf,ALUo} <= reg_A + reg_B; `CMP : {cf,ALUo} <= reg_A - reg_B; `BZ : ALUo <= reg_A + reg_B; `BN : ALUo <= reg_A + reg_B; `ADDC : {cf,ALUo} <= reg_A + reg_B + cf; `SUB : {cf,ALUo} <= reg_A - reg_B; `SUBC : {cf,ALUo} <= reg_A - reg_B - cf; `ADDI : {cf,ALUo} <= reg_A + reg_B; `SUBI : {cf,ALUo} <= reg_A - reg_B; `LDIH : {cf,ALUo} <= reg_A + reg_B; `AND : begin ALUo <= reg_A & reg_B; cf <= 1'b0; end `OR : begin ALUo <= reg_A | reg_B; cf <= 1'b0; end `XOR : begin ALUo <= reg_A ^ reg_B; cf <= 1'b0; end `SLL : begin ALUo <= reg_A << reg_B[3:0]; cf <= 1'b0; end `SRL : begin ALUo <= reg_A >> reg_B[3:0]; cf <= 1'b0; end `SLA : begin if(reg_A[15] == 1'b1) ALUo <= 16'h8000 | (reg_A << reg_B[3:0]); else if(reg_A[15] == 1'b0) ALUo <= 16'h7FFF & (reg_A << reg_B[3:0]); else ALUo <= 16'bx; end `SRA : begin if(reg_A[15] == 1'b1) ALUo <= ({16{1'b1}} << (16 - reg_B[3:0])) | (reg_A >> reg_B[3:0]); else if(reg_A[15] == 1'b0) ALUo <= reg_A >> reg_B[3:0]; else ALUo <= 16'bx; end `LWC_ENCRYPT: ALUo <= {reg_A[15:8]^reg_B[7:0], reg_A[7:4]^reg_B[11:8], reg_A[3:0]^reg_B[15:12]}; `JMPR : ALUo <= reg_A + reg_B; `BNZ : ALUo <= reg_A + reg_B; `BNN : ALUo <= reg_A + reg_B; `BC : ALUo <= reg_A + reg_B; `BNC : ALUo <= reg_A + reg_B; default : begin ALUo <= ALUo; cf <= cf; end endcase
always @(select_y or gr[1] or gr[2] or gr[3] or gr[4] or gr[5] or gr[6] or gr[7] or reg_A or reg_B or reg_C or reg_C1 or smdr or id_ir or dw or zf or nf or pc) begin case (select_y) 4'b0000 : y = {3'b000, dw, 2'b00, zf, nf, pc}; 4'b0001 : y = gr[1]; 4'b0010 : y = gr[2]; 4'b0011 : y = gr[3]; 4'b0100 : y = gr[4]; 4'b0101 : y = gr[5]; 4'b0110 : y = gr[6]; 4'b0111 : y = gr[7]; 4'b1000 : y = reg_A; 4'b1001 : y = reg_B; 4'b1011 : y = reg_C; 4'b1100 : y = reg_C1; 4'b1101 : y = smdr; 4'b1110 : y = id_ir; default : y = 16'bXXXXXXXXXXXXXXXX ; endcase end
endmodule
|