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 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
| module lc3( input CLK , input [15:0] SW , input [4:0] BTN , output reg [7:0] SSEG_CA , output reg [7:0] SSEG_AN , output [15:0] LED );
reg [1:0] clk_25m_cnt; wire clk_25m;
always @(posedge CLK) begin clk_25m_cnt <= clk_25m_cnt + 1; end
assign clk_25m = clk_25m_cnt[1];
reg [15:0] PC; reg [15:0] IR; reg [2:0] CC; reg [15:0] CCR; wire BEN; reg [4:0] state;
reg [9:0] MAR; reg [15:0] MDR_in; wire [15:0] MDR_out; reg [15:0] MDR; reg M_WE;
reg [15:0] cur; reg [1:0] mod; wire [4:0] BTN_clean; reg [8:0] ins_cnt;
wire [2:0] DR; wire [2:0] SR1; wire [2:0] SR2;
assign DR = IR[11:9]; assign SR1 = IR[8:6]; assign SR2 = IR[2:0];
reg [15:0] R[7:0];
parameter memory_range = 1 << 10; dist_mem_gen_0 M( .a (MAR), .d (MDR_in), .spo (MDR_out), .clk (CLK), .we (M_WE));
assign BEN = (IR[11] & CC[2]) | (IR[10] & CC[1]) | (IR[9] & CC[0]); always @(*) begin if(CCR == 16'b0) CC <= 3'b010; else if(CCR[15] == 1'b1) CC <= 3'b100; else CC <= 3'b001; end
always @(posedge clk_25m) begin case(state) 5'd1 : begin M_WE <= 1'b0; ins_cnt <= ins_cnt + 1; state <= 5'd2; end 5'd2 : begin if(PC >= memory_range || mod == 2'b0) begin mod <= 2'd0; state <= 5'd0; end else begin MAR <= PC[9:0]; PC <= PC + 1; state <= 5'd3; end end 5'd3 : begin IR <= MDR_out; state <= 5'd4; end 5'd4 : begin case(IR[15:12]) 4'b0001: state <= 5'd5; 4'b0101: state <= 5'd6; 4'b1001: state <= 5'd7; 4'b1111: state <= 5'd8; 4'b1110: state <= 5'd11; 4'b0010: state <= 5'd12; 4'b0110: state <= 5'd13; 4'b1010: state <= 5'd18; 4'b1011: state <= 5'd19; 4'b0111: state <= 5'd22; 4'b0011: state <= 5'd23; 4'b0100: state <= 5'd29; 4'b1100: state <= 5'd28; 4'b0000: state <= 5'd26; endcase end 5'd5 : begin if(IR[5]) begin R[DR] <= R[SR1] + {IR[4]?11'b111_1111_1111:11'b0,IR[4:0]}; CCR <= R[SR1] + {IR[4]?11'b111_1111_1111:11'b0,IR[4:0]}; end else begin R[DR] <= R[SR1] + R[SR2]; CCR <= R[SR1] + R[SR2]; end state <= 5'd1; end 5'd6 : begin if(IR[5]) begin R[DR] <= R[SR1] & {IR[4]?11'b111_1111_1111:11'b0,IR[4:0]}; CCR <= R[SR1] & {IR[4]?11'b111_1111_1111:11'b0,IR[4:0]}; end else begin R[DR] <= R[SR1] & R[SR2]; CCR <= R[SR1] & R[SR2]; end state <= 5'd1; end 5'd7 : begin R[DR] <= ~R[SR1]; CCR <= ~R[SR1]; state <= 5'd1; end 5'd8 : begin MAR <= {IR[7]?2'b11:2'b0,IR[7:0]}; state <= 5'd9; end 5'd9 : begin R[7] <= PC; state <= 5'd10; end 5'd10 : begin PC <= MDR_out; state <= 5'd1; end 5'd11 : begin R[DR] <= PC + {IR[8]?7'b111_1111:7'b0,IR[8:0]}; CCR <= PC + {IR[8]?7'b111_1111:7'b0,IR[8:0]}; state <= 5'd1; end 5'd12 : begin MAR <= PC + {IR[8]?7'b111_1111:7'b0,IR[8:0]}; state <= 5'd14; end 5'd13 : begin MAR <= R[SR1] + {IR[5]?10'b11_1111_1111:10'b0,IR[5:0]}; state <= 5'd14; end 5'd14 : begin state <= 5'd15; end 5'd15 : begin R[DR] <= MDR_out; CCR <= MDR_out; state <= 5'd1; end 5'd16 : begin MAR <= MDR[9:0]; state <= 5'd14; end 5'd17 : begin MDR <= MDR_out; state <= 5'd16; end 5'd18 : begin MAR <= PC + {IR[8]?7'b111_1111:7'b0,IR[8:0]}; state <= 5'd17; end 5'd19 : begin MAR <= PC + {IR[8]?7'b111_1111:7'b0,IR[8:0]}; state <= 5'd20; end 5'd20 : begin MDR <= MDR_out; state <= 5'd21; end 5'd21 : begin MAR <= MDR[9:0]; state <= 5'd24; end 5'd22 : begin MAR <= R[SR1] + {IR[5]?10'b11_1111_1111:10'b0,IR[5:0]}; state <= 5'd24; end 5'd23 : begin MAR <= PC + {IR[5]?10'b11_1111_1111:10'b0,IR[8:0]}; state <= 5'd24; end 5'd24 : begin MDR_in <= R[DR]; state <= 5'd25; end 5'd25 : begin M_WE <= 1'b1; state <= 5'd1; end 5'd26 : begin if(BEN) state <= 5'd27; else state <= 5'd1; end 5'd27 : begin PC <= PC + {IR[8] ? 7'b111_1111 : 7'b0,IR[8:0]}; state <= 5'd1; end 5'd28 : begin PC <= R[SR1]; state <= 5'd1; end 5'd29 : begin R[7] <= PC; if(IR[11]) state <= 5'd30; else state <= 5'd31; end 5'd30 : begin PC <= PC + {IR[10]?5'b11111:5'b0,IR[10:0]}; state <= 5'd1; end 5'd31 : begin PC <= R[SR1]; state <= 5'd1; end default: begin MAR <= cur[9:0]; if(mod == 2'b01) begin PC <= SW; ins_cnt <= 0; state <= 5'd1; end else state <= 5'd0; if (mod == 2'b00 && BTN_clean[4]) begin M_WE <= 1'b1; MDR_in <= SW; end else M_WE <= 1'b0; end endcase
case (mod) 2'd0: begin if (BTN_clean[0]) begin if (cur != 0) cur <= cur - 1; end else if (BTN_clean[1]) begin mod <= 2'd2; end else if (BTN_clean[2]) begin mod <= 2'd1; end else if (BTN_clean[3]) begin if (cur != memory_range - 1) cur <= cur + 1; end end 2'd1: begin if (BTN_clean[1]) mod <= 2'd0; end 2'd2: begin if (BTN_clean[0]) begin if (cur != 0) cur <= cur - 1; end else if (BTN_clean[1]) mod <= 2'd0; else if (BTN_clean[2]) mod <= 2'd1; else if (BTN_clean[3]) begin if (cur != memory_range - 1) cur <= cur + 1; end else if (BTN_clean[4]) cur <= {6'b0,SW[9:0]}; end default: begin mod <= 2'd0; end endcase end
initial begin cur = 16'h200; state = 5'd0; mod = 2'd0; ins_cnt = 9'b0; PC = 10'h200; M_WE = 1'b0; R[0] = 16'b0; R[1] = 16'b0; R[2] = 16'b0; R[3] = 16'b0; R[4] = 16'b0; R[5] = 16'b0; R[6] = 16'b0; R[7] = 16'b0; end
assign LED = (mod == 2'b0) ? PC : {ins_cnt,state,mod};
clean_posedge cl_p_0(clk_25m,BTN[0],BTN_clean[0]); clean_posedge cl_p_1(clk_25m,BTN[1],BTN_clean[1]); clean_posedge cl_p_2(clk_25m,BTN[2],BTN_clean[2]); clean_posedge cl_p_3(clk_25m,BTN[3],BTN_clean[3]); clean_posedge cl_p_4(clk_25m,BTN[4],BTN_clean[4]);
wire [7:0] digit0,digit1,digit2,digit3,digit4,digit5,digit6,digit7; wire [3:0] num0,num1,num2,num3,num4,num5,num6,num7; reg [16:0] cnt;
always @(posedge CLK) begin cnt <= cnt + 1; case (cnt[16:14]) 3'b000: begin SSEG_AN <= 8'b01111111; SSEG_CA <= digit0; end 3'b001: begin SSEG_AN <= 8'b10111111; SSEG_CA <= digit1; end 3'b010: begin SSEG_AN <= 8'b11011111; SSEG_CA <= digit2; end 3'b011: begin SSEG_AN <= 8'b11101111; SSEG_CA <= digit3; end 3'b100: begin SSEG_AN <= 8'b11110111; SSEG_CA <= digit4; end 3'b101: begin SSEG_AN <= 8'b11111011; SSEG_CA <= digit5; end 3'b110: begin SSEG_AN <= 8'b11111101; SSEG_CA <= digit6; end 3'b111: begin SSEG_AN <= 8'b11111110; SSEG_CA <= digit7; end endcase end assign num0 = cur[15:12]; assign num1 = cur[11:8]; assign num2 = cur[7:4]; assign num3 = cur[3:0]; assign num4 = MDR_out[15:12]; assign num5 = MDR_out[11:8]; assign num6 = MDR_out[7:4]; assign num7 = MDR_out[3:0]; dist_mem_gen_1 dist_mem_gen_0( .a (num0), .spo (digit0)); dist_mem_gen_1 dist_mem_gen_1( .a (num1), .spo (digit1)); dist_mem_gen_1 dist_mem_gen_2( .a (num2), .spo (digit2)); dist_mem_gen_1 dist_mem_gen_3( .a (num3), .spo (digit3)); dist_mem_gen_1 dist_mem_gen_4( .a (num4), .spo (digit4)); dist_mem_gen_1 dist_mem_gen_5( .a (num5), .spo (digit5)); dist_mem_gen_1 dist_mem_gen_6( .a (num6), .spo (digit6)); dist_mem_gen_1 dist_mem_gen_7( .a (num7), .spo (digit7)); endmodule
|