1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-12-11 02:49:22 +00:00
8bitworkshop/presets/verilog/racing_game_cpu.v

265 lines
5.5 KiB
Coq
Raw Normal View History

2018-02-12 20:02:55 +00:00
`include "hvsync_generator.v"
`include "sprite_bitmap.v"
2018-02-12 20:02:55 +00:00
`include "sprite_renderer.v"
`include "cpu8.v"
// uncomment to see scope view
//`define DEBUG
module racing_game_cpu_top(clk, reset, hsync, vsync, hpaddle, vpaddle,
2018-02-12 20:02:55 +00:00
address_bus, to_cpu, from_cpu, write_enable
`ifdef DEBUG
, output [7:0] A
, output [7:0] B
, output [7:0] IP
, output carry
, output zero
`else
, rgb
2018-02-12 20:02:55 +00:00
`endif
);
input clk, reset;
input hpaddle, vpaddle;
output hsync, vsync;
wire display_on;
wire [8:0] hpos;
wire [8:0] vpos;
`ifdef DEBUG
wire [2:0] rgb;
assign IP = cpu.IP;
assign A = cpu.A;
assign B = cpu.B;
assign carry = cpu.carry;
assign zero = cpu.zero;
`else
output [3:0] rgb;
2018-02-12 20:02:55 +00:00
`endif
parameter PADDLE_X = 0;
parameter PADDLE_Y = 1;
parameter PLAYER_X = 2;
parameter PLAYER_Y = 3;
parameter ENEMY_X = 4;
parameter ENEMY_Y = 5;
parameter ENEMY_DIR = 6;
parameter SPEED = 7;
parameter TRACKPOS_LO = 8;
parameter TRACKPOS_HI = 9;
parameter IN_HPOS = 8'h40;
parameter IN_VPOS = 8'h41;
parameter IN_FLAGS = 8'h42;
2018-02-12 20:02:55 +00:00
reg [7:0] ram[0:63];
2018-02-18 05:12:09 +00:00
reg [7:0] rom[0:127];
2018-02-12 20:02:55 +00:00
output wire [7:0] address_bus;
output reg [7:0] to_cpu;
output wire [7:0] from_cpu;
output wire write_enable;
CPU cpu(.clk(clk),
.reset(reset),
.address(address_bus),
.data_in(to_cpu),
.data_out(from_cpu),
.write(write_enable));
always @(posedge clk)
if (write_enable)
ram[address_bus[5:0]] <= from_cpu;
always @(*)
casez (address_bus)
// RAM
8'b00??????: to_cpu = ram[address_bus[5:0]];
// special read registers
IN_HPOS: to_cpu = hpos[7:0];
IN_VPOS: to_cpu = vpos[7:0];
IN_FLAGS: to_cpu = {2'b0, frame_collision,
vsync, hsync, vpaddle, hpaddle, display_on};
// ROM
2018-02-18 05:12:09 +00:00
8'b1???????: to_cpu = rom[address_bus[6:0]];
default: to_cpu = 8'bxxxxxxxx;
2018-02-12 20:02:55 +00:00
endcase
hvsync_generator hvsync_gen(
.clk(clk),
.reset(0),
.hsync(hsync),
.vsync(vsync),
.display_on(display_on),
.hpos(hpos),
.vpos(vpos)
);
wire player_vstart = {1'0,ram[PLAYER_Y]} == vpos;
wire player_hstart = {1'0,ram[PLAYER_X]} == hpos;
wire player_gfx;
wire player_is_drawing;
wire enemy_vstart = {1'0,ram[ENEMY_Y]} == vpos;
wire enemy_hstart = {1'0,ram[ENEMY_X]} == hpos;
wire enemy_gfx;
wire enemy_is_drawing;
wire [3:0] car_sprite_yofs;
wire [7:0] car_sprite_bits;
car_bitmap car(
.yofs(car_sprite_yofs),
.bits(car_sprite_bits));
sprite_renderer player_renderer(
.clk(clk),
.vstart(player_vstart),
.hstart(player_hstart),
.load(hpos == 256), //TODO?
.rom_addr(car_sprite_yofs),
.rom_bits(car_sprite_bits),
.gfx(player_gfx),
.in_progress(player_is_drawing));
sprite_renderer enemy_renderer(
.clk(clk),
.vstart(enemy_vstart),
.hstart(enemy_hstart),
.load(hpos == 260), //TODO?
.rom_addr(car_sprite_yofs),
.rom_bits(car_sprite_bits),
.gfx(enemy_gfx),
.in_progress(player_is_drawing));
reg frame_collision;
always @(posedge clk)
if (player_gfx && (enemy_gfx || track_gfx))
frame_collision <= 1;
else if (vpos==0)
frame_collision <= 0;
wire track_offside = (hpos[7:5]==0) || (hpos[7:5]==7);
wire track_shoulder = (hpos[7:3]==3) || (hpos[7:3]==28);
wire track_gfx = (vpos[5:1]!=ram[TRACKPOS_LO][5:1]) && track_offside;
wire r = display_on && (player_gfx || enemy_gfx || track_shoulder);
wire g = display_on && (player_gfx || track_gfx);
wire b = display_on && (enemy_gfx || track_shoulder);
assign rgb = {1'b0,b,g,r};
`ifdef EXT_INLINE_ASM
2018-02-12 20:02:55 +00:00
initial begin
rom = '{
2018-02-15 00:26:40 +00:00
__asm
.arch femto8
2018-02-15 00:26:40 +00:00
.org 128
2018-02-18 05:12:09 +00:00
.len 128
2018-02-15 00:26:40 +00:00
.define PADDLE_X 0
.define PADDLE_Y 1
.define PLAYER_X 2
.define PLAYER_Y 3
.define ENEMY_X 4
.define ENEMY_Y 5
.define ENEMY_DIR 6
.define SPEED 7
.define TRACKPOS_LO 8
.define TRACKPOS_HI 9
.define IN_HPOS $40
.define IN_VPOS $41
.define IN_FLAGS $42
.define F_DISPLAY 1
.define F_HPADDLE 2
.define F_VPADDLE 4
.define F_HSYNC 8
.define F_VSYNC 16
.define F_COLLIDE 32
Start:
2018-02-18 05:12:09 +00:00
lda #128
2018-02-15 00:26:40 +00:00
sta PLAYER_X
sta ENEMY_X
2018-02-18 05:12:09 +00:00
sta ENEMY_Y
lda #180
2018-02-15 00:26:40 +00:00
sta PLAYER_Y
zero A
sta SPEED
inc A
sta ENEMY_DIR
; test hpaddle flag
DisplayLoop:
2018-02-18 05:12:09 +00:00
lda #F_HPADDLE
ldb #IN_FLAGS
and none,[B]
2018-02-15 00:26:40 +00:00
bz DisplayLoop
; [vpos] -> paddle_x
2018-02-18 05:12:09 +00:00
ldb #IN_VPOS
mov A,[B]
2018-02-15 00:26:40 +00:00
sta PLAYER_X
; wait for vsync=1 then vsync=0
2018-02-18 05:12:09 +00:00
lda #F_VSYNC
ldb #IN_FLAGS
2018-02-15 00:26:40 +00:00
WaitForVsyncOn:
2018-02-18 05:12:09 +00:00
and none,[B]
2018-02-15 00:26:40 +00:00
bz WaitForVsyncOn
WaitForVsyncOff:
2018-02-18 05:12:09 +00:00
and none,[B]
2018-02-15 00:26:40 +00:00
bnz WaitForVsyncOff
; check collision
2018-02-18 05:12:09 +00:00
lda #F_COLLIDE
ldb #IN_FLAGS
and none,[B]
2018-02-15 00:26:40 +00:00
bz NoCollision
; load slow speed
2018-02-18 05:12:09 +00:00
lda #16
2018-02-15 00:26:40 +00:00
sta SPEED
NoCollision:
; update speed
2018-02-18 05:12:09 +00:00
ldb #SPEED
mov A,[B]
2018-02-15 00:26:40 +00:00
inc A
; don't store if == 0
bz MaxSpeed
sta SPEED
MaxSpeed:
2018-02-18 05:12:09 +00:00
mov A,[B]
2018-02-15 00:26:40 +00:00
lsr A
lsr A
lsr A
lsr A
; add to lo byte of track pos
2018-02-18 05:12:09 +00:00
ldb #TRACKPOS_LO
add B,[B]
2018-02-15 00:26:40 +00:00
swapab
sta TRACKPOS_LO
swapab
; update enemy vert pos
2018-02-18 05:12:09 +00:00
ldb #ENEMY_Y
add A,[B]
2018-02-15 00:26:40 +00:00
sta ENEMY_Y
; update enemy horiz pos
2018-02-18 05:12:09 +00:00
ldb #ENEMY_X
mov A,[B]
ldb #ENEMY_DIR
add A,[B]
2018-02-15 00:26:40 +00:00
sta ENEMY_X
2018-02-18 05:12:09 +00:00
sub A,#64
and A,#127
2018-02-15 00:26:40 +00:00
bnz SkipXReverse
; load ENEMY_DIR and negate
zero A
2018-02-18 05:12:09 +00:00
sub A,[B]
2018-02-15 00:26:40 +00:00
sta ENEMY_DIR
; back to display loop
SkipXReverse:
jmp DisplayLoop
__endasm
2018-02-12 20:02:55 +00:00
};
end
`endif
2018-02-12 20:02:55 +00:00
endmodule