1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-11-28 23:49:20 +00:00
8bitworkshop/presets/verilog/ram1.v

100 lines
2.3 KiB
Verilog

`include "hvsync_generator.v"
`include "digits10.v"
module RAM(clk, addr, din, dout, we);
parameter A = 10; // # of address bits
parameter D = 8; // # of data bits
input clk; // clock
input [A-1:0] addr; // 10-bit address
input [D-1:0] din; // 8-bit data input
output [D-1:0] dout; // 8-bit data output
input we; // write enable
reg [D-1:0] mem [0:(1<<A)-1]; // 1024x8 bit memory
always @(posedge clk) begin
if (we) // if write enabled
mem[addr] <= din; // write memory from din
dout <= mem[addr]; // read memory to dout
end
endmodule
module test_ram1_top(clk, reset, hsync, vsync, rgb);
input clk, reset;
output hsync, vsync;
output [2:0] rgb;
wire display_on;
wire [8:0] hpos;
wire [8:0] vpos;
wire [9:0] ram_addr;
wire [7:0] ram_read;
reg [7:0] ram_write;
reg ram_writeenable = 0;
// RAM to hold 32x32 array of bytes
RAM ram(
.clk(clk),
.dout(ram_read),
.din(ram_write),
.addr(ram_addr),
.we(ram_writeenable)
);
hvsync_generator hvsync_gen(
.clk(clk),
.reset(reset),
.hsync(hsync),
.vsync(vsync),
.display_on(display_on),
.hpos(hpos),
.vpos(vpos)
);
wire [4:0] row = vpos[7:3]; // 5-bit row, vpos / 8
wire [4:0] col = hpos[7:3]; // 5-bit column, hpos / 8
wire [2:0] rom_yofs = vpos[2:0]; // scanline of cell
wire [4:0] rom_bits; // 5 pixels per scanline
wire [3:0] digit = ram_read[3:0]; // read digit from RAM
wire [2:0] xofs = hpos[2:0]; // which pixel to draw (0-7)
assign ram_addr = {row,col}; // 10-bit RAM address
// digits ROM
digits10_case numbers(
.digit(digit),
.yofs(rom_yofs),
.bits(rom_bits)
);
// extract bit from ROM output
wire r = display_on && 0;
wire g = display_on && rom_bits[~xofs];
wire b = display_on && 0;
assign rgb = {b,g,r};
// increment the current RAM cell
always @(posedge clk)
case (hpos[2:0])
// on 7th pixel of cell
6: begin
// increment RAM cell
ram_write <= (ram_read + 1);
// only enable write on last scanline of cell
ram_writeenable <= (vpos[2:0] == 7);
end
// on 8th pixel of cell
7: begin
// disable write
ram_writeenable <= 0;
end
endcase
endmodule