diff --git a/resources/ping-pong-mips32/game/.gitignore b/resources/ping-pong-mips32/game/.gitignore new file mode 100644 index 0000000..2788ff2 --- /dev/null +++ b/resources/ping-pong-mips32/game/.gitignore @@ -0,0 +1,4 @@ +obj/ +game.s +game.bin +game.elf diff --git a/resources/ping-pong-mips32/game/Makefile b/resources/ping-pong-mips32/game/Makefile index 396de7d..844fa01 100644 --- a/resources/ping-pong-mips32/game/Makefile +++ b/resources/ping-pong-mips32/game/Makefile @@ -11,15 +11,15 @@ MODULE := init OBJDIR := obj INCLUDE := include SRCDIR := game -LDSCRIPT := game/game.ld +LDSCRIPT := $(SRCDIR)/game.ld SRC := $(foreach sdir, $(SRCDIR), $(wildcard $(sdir)/*.S)) OBJ := $(patsubst $(SRCDIR)/%.S, $(OBJDIR)/%.o, $(SRC)) TARGET := game.elf ASFLAG := -D__ASSEMBLY__ -EL -g -mips32r2 -mno-abicalls -mno-shared -.PHONY: all clean checkdirs sim +.PHONY: all clean checkdirs generate sim -all: $(TARGET) game.bin +all: $(TARGET) game.bin generate $(TARGET): checkdirs $(OBJ) $(LDSCRIPT) $(LD) $(OBJ) -T$(LDSCRIPT) @@ -36,8 +36,15 @@ game.bin: $(TARGET) @$(GCCPREFIX)objcopy -j .text -j .rodata -j .data -O binary -v $< game.bin @$(GCCPREFIX)objdump -lD $< > game.s +generate: game.bin convert + ./convert game.bin $(OBJDIR)/ + +convert: convert.c + gcc -o convert convert.c + sim: $(TARGET) $(QEMU) -M mipssim -m 8M -kernel $< -nographic -monitor none -serial tcp::6666,server -s clean: -$(RM) -r $(OBJDIR) + -$(RM) game.bin game.elf game.s game.coe convert diff --git a/resources/ping-pong-mips32/game/convert b/resources/ping-pong-mips32/game/convert new file mode 100755 index 0000000..8928be0 Binary files /dev/null and b/resources/ping-pong-mips32/game/convert differ diff --git a/resources/ping-pong-mips32/game/convert.c b/resources/ping-pong-mips32/game/convert.c new file mode 100644 index 0000000..ab7a1f4 --- /dev/null +++ b/resources/ping-pong-mips32/game/convert.c @@ -0,0 +1,84 @@ +#include +#include +#include + +void binary_out(FILE* out,unsigned char* mem) +{ + char tmp; + unsigned char num[8]; + num[0] = 1; + num[1] = 2; + num[2] = 4; + num[3] = 8; + num[4] = 16; + num[5] = 32; + num[6] = 64; + num[7] = 128; + for(int i=3;i>=0;i--) + { + for(int j=7;j>=0;j--) + { + if( (mem[i] & num[j] ) != 0) + tmp = '1'; + else + tmp = '0'; + fprintf(out,"%c",tmp); + } + } + fprintf(out,"\n"); + return; +} + +int main(int argc, char** argv) +{ + FILE *in; + FILE *out; + + if(argc < 3){ + fprintf(stderr, "Usage: convert main.bin main.data directory\n"); + return 1; + } + + char str_bin[256]; + char str_coe[256], str_mif[256]; + strncpy(str_bin, argv[2], 256); + strncpy(str_coe, argv[2], 256); + strncpy(str_mif, argv[2], 256); + strncat(str_bin, argv[1], 256); + strncat(str_coe, "axi_ram.coe", 256); + strncat(str_mif, "axi_ram.mif", 256); + //printf("%s\n%s\n%s\n%s\n%s\n%s\n", str_bin, str_data, str_inst_coe, str_inst_mif, str_data_coe, str_data_mif); + + int i,j,k; + unsigned char mem[32]; + + in = fopen(str_bin, "rb"); + out = fopen(str_coe,"w"); + + fprintf(out, "memory_initialization_radix = 16;\n"); + fprintf(out, "memory_initialization_vector =\n"); + while(!feof(in)) { + if(fread(mem,1,4,in)!=4) { + fprintf(out, "%02x%02x%02x%02x\n", mem[3], mem[2], mem[1], mem[0]); + break; + } + fprintf(out, "%02x%02x%02x%02x\n", mem[3], mem[2], mem[1],mem[0]); + } + fclose(in); + fclose(out); + + in = fopen(str_bin, "rb"); + out = fopen(str_mif,"w"); + + while(!feof(in)) { + if(fread(mem,1,4,in)!=4) { + binary_out(out,mem); + break; + } + binary_out(out,mem); + } + fclose(in); + fclose(out); + + return 0; +} diff --git a/resources/ping-pong-mips32/game/game/evec.S b/resources/ping-pong-mips32/game/game/evec.S index 41b104c..58203ed 100644 --- a/resources/ping-pong-mips32/game/game/evec.S +++ b/resources/ping-pong-mips32/game/game/evec.S @@ -18,15 +18,15 @@ la t6, _data_begin la t7, _data_end la t8, _data_begin - ori t0, t0, k0 - ori t1, t1, k0 - ori t2, t2, k1 - ori t3, t3, k0 - ori t4, t4, k0 - ori t5, t5, k1 - ori t6, t6, k0 - ori t7, t7, k0 - ori t8, t8, k1 + or t0, t0, k0 + or t1, t1, k0 + or t2, t2, k1 + or t3, t3, k0 + or t4, t4, k0 + or t5, t5, k1 + or t6, t6, k0 + or t7, t7, k0 + or t8, t8, k1 la t9, START /* copy .text.ebase */ @@ -60,6 +60,7 @@ 4: jr t9 + nop .set reorder diff --git a/resources/ping-pong-mips32/game/game/game.S b/resources/ping-pong-mips32/game/game/game.S index fd4152e..6f6f518 100644 --- a/resources/ping-pong-mips32/game/game/game.S +++ b/resources/ping-pong-mips32/game/game/game.S @@ -6,6 +6,10 @@ .set noat + .section .bss + .p2align 2 + + .section .text .p2align 2 @@ -27,11 +31,37 @@ GAME_TIMER: j RESTART nop - .global GAME_KB -GAME_KB: + .global GAME_KB0 +GAME_KB0: j RESTART nop + .global GAME_KB1 +GAME_KB1: + addi sp, sp, -16 + sw ra, 12(sp) + sw a0, 8(sp) + sw v1, 4(sp) + sw v0, 0(sp) + la k0, RESTART + + la a0, UART1 +0: + jal TRYREADSERIAL + nop + beqz v1, 1f + nop + + j 0b + nop +1: + lw v0, 0(sp) + lw v1, 4(sp) + lw a0, 8(sp) + lw ra, 12(sp) + jr k0 + addi sp, sp, 16 + .set reorder .set at diff --git a/resources/ping-pong-mips32/game/game/game.ld b/resources/ping-pong-mips32/game/game/game.ld index 7c9472e..5720461 100644 --- a/resources/ping-pong-mips32/game/game/game.ld +++ b/resources/ping-pong-mips32/game/game/game.ld @@ -1,5 +1,5 @@ ENTRY(INITLOCATE) -OUTPUT("kernel.elf") +OUTPUT("game.elf") OUTPUT_ARCH("mips:isa32r2") OUTPUT_FORMAT("elf32-tradlittlemips") diff --git a/resources/ping-pong-mips32/game/game/trap.S b/resources/ping-pong-mips32/game/game/trap.S index 24913e6..44acba6 100644 --- a/resources/ping-pong-mips32/game/game/trap.S +++ b/resources/ping-pong-mips32/game/game/trap.S @@ -22,7 +22,7 @@ andi k1, k0, 0x00FC bnez k1, SYSCALL nop - mfc0, k1, CP0_STATUS + mfc0 k1, CP0_STATUS and k0, k0, k1 7: andi k1, k0, CAUSEF_IP7 @@ -32,6 +32,7 @@ lw k0, %lo(current_thread)(k0) lw k1, TF_INTHDL7(k0) jr k1 + nop 6: andi k1, k0, CAUSEF_IP6 beqz k1, 5f @@ -40,6 +41,7 @@ lw k0, %lo(current_thread)(k0) lw k1, TF_INTHDL6(k0) jr k1 + nop 5: andi k1, k0, CAUSEF_IP5 beqz k1, 4f @@ -48,6 +50,7 @@ lw k0, %lo(current_thread)(k0) lw k1, TF_INTHDL5(k0) jr k1 + nop 4: andi k1, k0, CAUSEF_IP4 beqz k1, 3f @@ -56,6 +59,7 @@ lw k0, %lo(current_thread)(k0) lw k1, TF_INTHDL4(k0) jr k1 + nop 3: andi k1, k0, CAUSEF_IP3 beqz k1, 2f @@ -64,6 +68,7 @@ lw k0, %lo(current_thread)(k0) lw k1, TF_INTHDL3(k0) jr k1 + nop 2: andi k1, k0, CAUSEF_IP2 beqz k1, 1f @@ -72,6 +77,7 @@ lw k0, %lo(current_thread)(k0) lw k1, TF_INTHDL2(k0) jr k1 + nop 1: andi k1, k0, CAUSEF_IP1 beqz k1, 0f @@ -80,19 +86,21 @@ lw k0, %lo(current_thread)(k0) lw k1, TF_INTHDL1(k0) jr k1 + nop 0: lui k0, %hi(current_thread) lw k0, %lo(current_thread)(k0) lw k1, TF_INTHDL0(k0) jr k1 + nop SYSCALL: li k1, EX_SYS bne k0, k1, DEFAULT_INT_HANDLER nop - mfc0 k1, C0_EPC + mfc0 k1, CP0_EPC addiu k1, k1, 0x4 - mtc0 k1, C0_EPC + mtc0 k1, CP0_EPC .global DEFAULT_INT_HANDLER DEFAULT_INT_HANDLER: diff --git a/resources/ping-pong-mips32/game/game/utils.S b/resources/ping-pong-mips32/game/game/utils.S index 5521b3b..e79d195 100644 --- a/resources/ping-pong-mips32/game/game/utils.S +++ b/resources/ping-pong-mips32/game/game/utils.S @@ -9,28 +9,35 @@ .global WRITESERIAL WRITESERIAL: -.TESTW: +1: lb t0, 5(a0) andi t0, t0, 0x20 - beqz t0, .TESTW + beqz t0, 1b nop -.WSERIAL: - sb a1, 0(a0) - jr ra + sb a1, 0(a0) + jr ra nop .global READSERIAL READSERIAL: -.TESTR: +1: lb t0, 5(a0) andi t0, t0, 0x01 - bnez t0, .RSERIAL + beqz t0, 1b nop - j .TESTR + lb v0, 0(a0) + jr ra nop -.RSERIAL: - lb v0, 0(a0) - jr ra + + .global TRYREADSERIAL +TRYREADSERIAL: + lb v1, 5(a0) + andi v1, v1, 0x01 + beqz v1, 1f + nop + lb v0, 0(a0) +1: + jr ra nop