From 0e21c36cafb3a5c96e05672530c9f171b2d22e8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergiusz=20=27q3k=27=20Baza=C5=84ski?= Date: Sat, 4 Jan 2014 00:35:11 +0100 Subject: [PATCH] First commit - some instructions implemented --- COPYING | 10 ++++ README | 3 ++ cpu.v | 163 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 176 insertions(+) create mode 100644 COPYING create mode 100644 README create mode 100644 cpu.v diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..ba7cbf4 --- /dev/null +++ b/COPYING @@ -0,0 +1,10 @@ +Copyright (c) 2014, Sergiusz 'q3k' Bazański +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README b/README new file mode 100644 index 0000000..b4dcbbb --- /dev/null +++ b/README @@ -0,0 +1,3 @@ +A WIP Chip8 Verilog CPU. + +May, in the future, contain other components (VGA, SRAM, PS2 and SD card) in order to synthesize this as a console on a Spartan devboard or something. diff --git a/cpu.v b/cpu.v new file mode 100644 index 0000000..f565257 --- /dev/null +++ b/cpu.v @@ -0,0 +1,163 @@ +// Copyright (c) 2014 Sergiusz 'q3k' Bazański +// Licensed under 2-clause BSD - see COPYING file from software distribution + +`timescale 1ns / 1ps + +module core ( + input reset, + input cpu_clock, + input timer_clock, + input [15:0] keypad + ); + + // 4k (12 bits) of 8-bit ram + reg [7:0] ram[0:4095]; + // 16 8-bit V registers + reg [7:0] v[0:15]; + // 16-bit I register + reg [15:0] i; + // 12-bit program counter + reg [11:0] pc; + // 8-bit stack pointer + reg [7:0] sp; + + // internal cycle counter (0 - fetch) + `define FETCH 0 + `define EXECUTE 1 + `define WRITEBACK 2 + reg [1:0] c; + + // Instruction register + reg [15:0] ins; + wire family = (ins >> 12); + + // ALU operation + wire [3:0] alu_op = ins & 4'hF; + // ALU source registers + wire [3:0] alu_x = (ins >> 8) & 4'hF; + wire [3:0] alu_y = (ins >> 4) & 4'hF; + // ALU immediate + wire [7:0] kk = ins & 8'hFF; + wire [11:0] nnn = ins & 12'hFFF; + + // Jump register + reg[15:0] jmp; + // Should jump on FETCH? + reg should_jmp; + + always @(cpu_clock) + begin + if (!reset) begin + i <= 0; + pc <= 0; + sp <= 0; + should_jmp <= 0; + end else begin + case(c) + `FETCH: begin + ins <= ram[pc] << 8 | ram[pc + 1]; + end + `EXECUTE: begin + case (family) + // Jump to address + 1: begin + should_jmp <= 1; + jmp <= nnn; + end + // Call address + 2: begin + should_jmp <= 1; + ram[sp] <= pc; + sp <= sp + 1; + jmp <= nnn; + end + // Skip next if Vx == kk (3xkk) + 3: begin + if (v[alu_x] == kk) begin + should_jmp <= 1; + jmp <= pc + 4; + end + end + // Skip next if Vx != kk (4xkk) + 4: begin + if (v[alu_x] != kk) begin + should_jmp <= 1; + jmp <= pc + 4; + end + end + // Skip next if Vx == Vy (5xy0) + 5: begin + if (v[alu_x] == v[alu_y]) begin + should_jmp <= 1; + jmp <= pc + 4; + end + end + // Vx <= kk + 6: v[alu_x] <= kk; + // Vx += kk + 7: v[alu_x] <= v[alu_x] + kk; + // ALU operation + 8: case(alu_op) + 0: v[alu_x] <= v[alu_y]; + 1: v[alu_x] <= v[alu_x] | v[alu_y]; + 2: v[alu_x] <= v[alu_x] & v[alu_y]; + 3: v[alu_x] <= v[alu_x] ^ v[alu_y]; + 4: { v[15], v[alu_x] } <= v[alu_x] + v[alu_y]; + 5: { v[15], v[alu_x] } <= (v[alu_x] - v[alu_y]) ^ (1 << 8); + 6: begin + v[15] <= v[alu_x][0]; + v[alu_x] <= v[alu_x] >> 1; + end + 7: { v[15], v[alu_x] } <= (v[alu_y] - v[alu_x]) ^ (1 << 8); + 15: begin + v[15] <= v[alu_x][7]; + v[alu_x] <= v[alu_x] << 1; + end + endcase + // Skip if Vx != Vy (9xy0) + 9: begin + if (v[alu_x] != v[alu_y]) begin + should_jmp <= 1; + jmp <= pc + 4; + end + end + // Set I to nnn + 10: i <= nnn; + // Jump to nnn + V0 + 11: begin + should_jmp <= 1; + jmp <= nnn + v[0]; + end + // Set Vx to random and kk + // TODO: undo fair dice roll + 12: v[alu_x] <= 42 & kk; + // TODO: draw sprites + 13: begin end + // Key functions + 14: begin + if (kk == 8'h9E) begin + if ((keypad >> alu_x) & 1) begin + should_jmp <= 1; + jmp <= pc + 4; + end + end else if (kk == 8'hA1) begin + if (((keypad >> alu_x) & 1) == 0) begin + should_jmp <= 1; + jmp <= pc + 4; + end + end + end + endcase + end + `WRITEBACK: begin + if (should_jmp) begin + pc <= jmp; + end else begin + pc <= pc + 2; + end + should_jmp <= 0; + end + endcase + end + end +endmodule