Skip to content

Commit ec83153

Browse files
add 3d game
1 parent 9cb4948 commit ec83153

File tree

9 files changed

+1305
-0
lines changed

9 files changed

+1305
-0
lines changed

include/console.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1+
#pragma once
2+
3+
#include <stdbool.h>
14
#include <stddef.h>
25

6+
#ifdef __cplusplus
7+
extern "C"
8+
{
9+
#endif
10+
311
void console_init(void);
412
void console_set_irq_en(bool rx_irq, bool tx_irq);
513
int getchar(void);
14+
bool console_havechar(void);
615
int putchar(int c);
716
int puts(const char *str);
817

918
#ifndef __USE_LIBC
1019
size_t strlen(const char *s);
1120
#endif
21+
22+
#ifdef __cplusplus
23+
}
24+
#endif

include/liteuart_console.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
#pragma once
22

3+
#include <stdbool.h>
4+
5+
#ifdef __cplusplus
6+
extern "C"
7+
{
8+
#endif
9+
310
int usb_getchar(void);
411
bool usb_havechar(void);
512
int usb_putchar(int c);
613
int usb_puts(const char *str);
714
void usb_console_init(void);
15+
16+
#ifdef __cplusplus
17+
}
18+
#endif

lib/console.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ int getchar(void)
148148
}
149149
}
150150

151+
bool console_havechar(void) {
152+
if (uart_is_std)
153+
return !std_uart_rx_empty();
154+
else
155+
return !potato_uart_rx_empty();
156+
}
157+
151158
int putchar(int c)
152159
{
153160
if (uart_is_std) {

usb_3d_game/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
usb_3d_game_emu
2+
*.o
3+
*.elf
4+
*.hex
5+
*.bin
6+

usb_3d_game/Makefile

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
ARCH = $(shell uname -m)
2+
ifneq ("$(ARCH)", "ppc64")
3+
ifneq ("$(ARCH)", "ppc64le")
4+
CROSS_COMPILE ?= powerpc64le-linux-gnu-
5+
endif
6+
endif
7+
8+
CC = $(CROSS_COMPILE)gcc
9+
CXX = $(CROSS_COMPILE)g++
10+
LD = $(CROSS_COMPILE)ld
11+
OBJCOPY = $(CROSS_COMPILE)objcopy
12+
13+
COMMON_FLAGS = -Os -g -Wall -msoft-float -mno-string -mno-multiple -mno-vsx -mno-altivec -mlittle-endian -fno-stack-protector -mstrict-align -ffreestanding -fdata-sections -ffunction-sections -I../include
14+
COMMON_FLAGS += -Werror -Wextra
15+
CXXFLAGS = $(COMMON_FLAGS) -std=c++14 -fno-exceptions
16+
CFLAGS = $(COMMON_FLAGS) -std=c99
17+
ASFLAGS = $(CFLAGS)
18+
LDFLAGS = -T powerpc.lds
19+
20+
all: usb_3d_game.hex
21+
22+
console.o: ../lib/console.c
23+
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
24+
25+
liteuart_console.o: ../lib/liteuart_console.c
26+
$(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@
27+
28+
usb_3d_game.elf: usb_3d_game.o head.o console.o liteuart_console.o
29+
$(LD) $(LDFLAGS) -o $@ $^
30+
31+
usb_3d_game.bin: usb_3d_game.elf
32+
$(OBJCOPY) -O binary $^ $@
33+
34+
usb_3d_game.hex: usb_3d_game.bin
35+
../scripts/bin2hex.py $^ > $@
36+
37+
usb_3d_game_emu: usb_3d_game.cpp
38+
c++ -g -Wall -std=c++14 -Werror -Wextra -o usb_3d_game_emu usb_3d_game.cpp -DEMULATE_TARGET
39+
40+
clean:
41+
@rm -f *.o usb_3d_game.elf usb_3d_game.bin usb_3d_game.hex usb_3d_game_emu
42+
distclean: clean
43+
rm -f *~
44+

usb_3d_game/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# 3D Maze Game
2+
3+
Based on: <https://github.com/programmerjake/rv32/tree/v0.1.0.1-alpha/software>
4+
5+
# Run without FPGA/hardware-simulation
6+
7+
Resize your terminal to be at least 100x76.
8+
9+
Building:
10+
```bash
11+
cd usb_3d_game
12+
make usb_3d_game_emu
13+
```
14+
15+
Running:
16+
```bash
17+
./usb_3d_game_emu
18+
```
19+
20+
# Run on OrangeCrab v0.2.1
21+
22+
Set the OrangeCrab into firmware upload mode by plugging it in to USB while the button is pressed, then run the following commands:
23+
24+
Building/Flashing:
25+
```bash
26+
(cd usb_3d_game; make)
27+
sudo make FPGA_TARGET=ORANGE-CRAB-0.21 dfuprog DOCKER=1 LITEDRAM_GHDL_ARG=-gUSE_LITEDRAM=false RAM_INIT_FILE=usb_3d_game/usb_3d_game.hex MEMORY_SIZE=$((1<<18))
28+
```
29+
30+
Then, in a separate terminal that you've resized to be at least 100x76, run (replacing ttyACM0 with whatever serial device the OrangeCrab is):
31+
```bash
32+
sudo tio /dev/ttyACM0
33+
```
34+
35+
# Controls
36+
37+
Use WASD or the Arrow keys to move around. Press Ctrl+C to quit or restart.
38+
39+
The goal is a set of flashing blocks, nothing special yet happens when you reach them though.

usb_3d_game/head.S

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/* Copyright 2013-2014 IBM Corp.
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12+
* implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#define STACK_TOP 0x20000
18+
19+
#define FIXUP_ENDIAN \
20+
tdi 0,0,0x48; /* Reverse endian of b . + 8 */ \
21+
b 191f; /* Skip trampoline if endian is good */ \
22+
.long 0xa600607d; /* mfmsr r11 */ \
23+
.long 0x01006b69; /* xori r11,r11,1 */ \
24+
.long 0x05009f42; /* bcl 20,31,$+4 */ \
25+
.long 0xa602487d; /* mflr r10 */ \
26+
.long 0x14004a39; /* addi r10,r10,20 */ \
27+
.long 0xa64b5a7d; /* mthsrr0 r10 */ \
28+
.long 0xa64b7b7d; /* mthsrr1 r11 */ \
29+
.long 0x2402004c; /* hrfid */ \
30+
191:
31+
32+
33+
/* Load an immediate 64-bit value into a register */
34+
#define LOAD_IMM64(r, e) \
35+
lis r,(e)@highest; \
36+
ori r,r,(e)@higher; \
37+
rldicr r,r, 32, 31; \
38+
oris r,r, (e)@h; \
39+
ori r,r, (e)@l;
40+
41+
.section ".head","ax"
42+
43+
/*
44+
* Microwatt currently enters in LE mode at 0x0, so we don't need to
45+
* do any endian fix ups>
46+
*/
47+
. = 0
48+
.global _start
49+
_start:
50+
b boot_entry
51+
52+
/* QEMU enters at 0x10 */
53+
. = 0x10
54+
FIXUP_ENDIAN
55+
b boot_entry
56+
57+
. = 0x100
58+
FIXUP_ENDIAN
59+
b boot_entry
60+
61+
.global boot_entry
62+
boot_entry:
63+
/* setup stack */
64+
LOAD_IMM64(%r1, STACK_TOP - 0x100)
65+
LOAD_IMM64(%r12, main)
66+
mtctr %r12,
67+
bctrl
68+
b .
69+
70+
#define EXCEPTION(nr) \
71+
.= nr ;\
72+
b .
73+
74+
/* More exception stubs */
75+
EXCEPTION(0x300)
76+
EXCEPTION(0x380)
77+
EXCEPTION(0x400)
78+
EXCEPTION(0x480)
79+
EXCEPTION(0x500)
80+
EXCEPTION(0x600)
81+
EXCEPTION(0x700)
82+
EXCEPTION(0x800)
83+
EXCEPTION(0x900)
84+
EXCEPTION(0x980)
85+
EXCEPTION(0xa00)
86+
EXCEPTION(0xb00)
87+
EXCEPTION(0xc00)
88+
EXCEPTION(0xd00)
89+
EXCEPTION(0xe00)
90+
EXCEPTION(0xe20)
91+
EXCEPTION(0xe40)
92+
EXCEPTION(0xe60)
93+
EXCEPTION(0xe80)
94+
EXCEPTION(0xf00)
95+
EXCEPTION(0xf20)
96+
EXCEPTION(0xf40)
97+
EXCEPTION(0xf60)
98+
EXCEPTION(0xf80)
99+
#if 0
100+
EXCEPTION(0x1000)
101+
EXCEPTION(0x1100)
102+
EXCEPTION(0x1200)
103+
EXCEPTION(0x1300)
104+
EXCEPTION(0x1400)
105+
EXCEPTION(0x1500)
106+
EXCEPTION(0x1600)
107+
#endif

usb_3d_game/powerpc.lds

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
SECTIONS
2+
{
3+
. = 0;
4+
.head : {
5+
KEEP(*(.head))
6+
}
7+
. = 0x1000;
8+
.text : { *(.text) }
9+
. = 0x2a000;
10+
.data : { *(.data) }
11+
.rodata : { *(.rodata) }
12+
.bss : { *(.bss) }
13+
}

0 commit comments

Comments
 (0)