From d5d3b5d4e53cc0c8b6eb6287dd1137a832d011a6 Mon Sep 17 00:00:00 2001 From: "Cheng Ho Ming, Eric" Date: Fri, 23 Aug 2024 16:41:44 +0800 Subject: [PATCH] Improve RV UI --- rv-asm/funconfig.h | 1 - rv-asm/rv-asm.c | 193 +++++++++++++++++++++++++++++++++------------ 2 files changed, 144 insertions(+), 50 deletions(-) diff --git a/rv-asm/funconfig.h b/rv-asm/funconfig.h index bfaed1d..b927055 100644 --- a/rv-asm/funconfig.h +++ b/rv-asm/funconfig.h @@ -8,7 +8,6 @@ #define horizontalButtons 8 #define verticalButtons 8 #define NUM_LEDS (horizontalButtons * verticalButtons) -#define NUM_BUTTONS 64 // #define CH32V003J4M6_USE_PD6_AS_UART_TX #define FUNCONF_SYSTICK_USE_HCLK 1 diff --git a/rv-asm/rv-asm.c b/rv-asm/rv-asm.c index 70ae5b3..5e66aff 100644 --- a/rv-asm/rv-asm.c +++ b/rv-asm/rv-asm.c @@ -9,16 +9,27 @@ #define RAM_BASE 0x80000000 #define RAM_SIZE 0x400 -#define LED_PINS GPIOC, 1 +#define LED_PINS GPIOA, 2 -rv_res bus_cb(void * user, rv_u32 addr, rv_u8 * data, rv_u32 is_store, rv_u32 width) { - rv_u8 * mem = (rv_u8 *)user + addr - RAM_BASE; - if (addr < RAM_BASE || addr + width >= RAM_BASE + RAM_SIZE) - return RV_BAD; - memcpy(is_store ? mem : data, is_store ? data : mem, width); - return RV_OK; +rv_res bus_cb(void * user, rv_u32 addr, rv_u8 * data, rv_u32 is_store, rv_u32 width); + +// Just a printf wrapper +void display_all_registers(rv * cpu); + +#define num_of_instructions 24 +#define led_page_size_show 2 +#define num_of_led_page_show (num_of_instructions / led_page_size_show) + +void add_no_page_ontop(uint16_t current_no_page) { + if (current_no_page < 0 || current_no_page > num_of_led_page_show) { + return; + } + for (uint16_t i = NUM_LEDS - 1; i >= NUM_LEDS - 1 - current_no_page; i--) { + set_color(i, (color_t){30, 30, 30}); + } } -rv_u16 program[100] = { + +rv_u16 program[num_of_instructions] = { // smile mouth // c.addi x10, 4 0x0511, @@ -44,62 +55,119 @@ rv_u16 program[100] = { // c.slli x11, 8 0x05a2, // ecall - 0x0073}; + 0x0073, + // 13-20: filler with zeros. + 0x0000, + 0x0000, + 0x0000, + 0x0000, + 0x0000, + 0x0000, + 0x0000, + 0x0000, +}; -void display_all_registers(rv * cpu) { - for (int i = 0; i < 32; i++) { - if (cpu->r[i] != 0) - printf("r%d: %ld ", i, cpu->r[i]); +#define instruction_size 16 +typedef enum _which_focus { + focus_instruction_0 = 0, + focus_instruction_1 = 1, + none = 2, +} which_focus; + +void instructionDisplay(int16_t index, which_focus focus) { + if (index < 0) { + return; + } + const uint16_t starting_index = index * led_page_size_show; + +#define default_code_color (color_t){.g = 255, .r = 0, .b = 0} + color_t code1_color = default_code_color; + if (focus == focus_instruction_0) { + code1_color = (color_t){.b = 255, .r = 0, .g = 0}; + } + color_t code2_color = default_code_color; + if (focus == focus_instruction_1) { + code2_color = (color_t){.b = 255, .r = 0, .g = 0}; } - printf("\n"); -} -void instructionDisplay(void) { // display 2 16bit instructions in led 0-15, 16-31 - for (int bit = 15; bit >= 0; bit--) { - if (program[0] & (1 << bit)) { - set_color(bit, (color_t){255, 0, 0}); + for (int8_t bit = instruction_size - 1; bit >= 0; bit--) { + if (program[starting_index] & (1 << bit)) { + set_color(bit, code1_color); } } - for (int bit = 15; bit >= 0; bit--) { - if (program[1] & (1 << bit)) { - set_color(bit + 16, (color_t){255, 0, 0}); + for (int8_t bit = instruction_size - 1; bit >= 0; bit--) { + if (program[starting_index + 1] & (1 << bit)) { + set_color(bit + instruction_size, code2_color); } } } +const color_t white = {255, 255, 255}; + void inputProgram(void) { // input 2 16bit instructions in led 0-15, 16-31 + int16_t page_of_instructions = 0; + clear(); + instructionDisplay(page_of_instructions, none); + add_no_page_ontop(page_of_instructions); + WS2812BSimpleSend(LED_PINS, (uint8_t *)led_array, NUM_LEDS * 3); while (1) { - int8_t input = matrix_pressed(ADC_read_smallboard); - Delay_Ms(100); + int8_t input = matrix_pressed_two(); + Delay_Ms(120); if (input == no_button_pressed) { - continue; + if (JOY_down_pressed()) { + page_of_instructions++; + if (page_of_instructions > num_of_led_page_show) { + page_of_instructions = 0; + } + printf("page: %d\n", page_of_instructions); + } + else if (JOY_up_pressed()) { + page_of_instructions--; + if (page_of_instructions < 0) { + page_of_instructions = num_of_led_page_show; + } + printf("page: %d\n", page_of_instructions); + } + else if (JOY_Y_pressed()) { + break; + } + else { + continue; + } } - if (input == 32) { - break; + else { + int16_t instruction_index = (led_page_size_show * page_of_instructions) + + (input / instruction_size); + int8_t bit_index = input % instruction_size; + printf("instruction: %d, ", instruction_index); + printf("bit: %d, ", bit_index); + printf("program: %04x, ", program[instruction_index]); + program[instruction_index] ^= (1 << bit_index); + printf("new program: %04X\n", program[instruction_index]); } - printf("program: %lX\n", program[input / 16]); - printf("bit: %d\n", input % 16); - program[input / 16] ^= (1 << (input % 16)); - printf("program: %lX\n", program[input / 16]); + + clear(); - set_color(32, (color_t){255, 255, 255}); - instructionDisplay(); + instructionDisplay(page_of_instructions, none); + add_no_page_ontop(page_of_instructions); WS2812BSimpleSend(LED_PINS, (uint8_t *)led_array, NUM_LEDS * 3); + } } +#define register_r10_cpu_r_index 10 +#define register_r11_cpu_r_index 11 + +#define register_r10_color (color_t){0, 255, 0} +#define register_r11_color (color_t){0, 0, 255} + int main(void) { SystemInit(); ADC_init(); printf("Hello, World!\n"); - clear(); - set_color(32, (color_t){255, 255, 255}); - instructionDisplay(); - WS2812BSimpleSend(LED_PINS, (uint8_t *)led_array, NUM_LEDS * 3); - inputProgram(); printf("Matrix Pressed\n"); @@ -111,30 +179,57 @@ int main(void) { while (1) { rv_u32 trap = rv_step(&cpu); + clear(); - uint8_t next_pos = (cpu.pc - RAM_BASE) / sizeof(program[0]); - for (int bit = 15; bit >= 0; bit--) { + for (int8_t bit = instruction_size - 1; bit >= 0; bit--) { // display register r10, r11 value at led 32-47, 48-63 - if (cpu.r[10] & (1 << bit)) { - set_color(bit + 32, (color_t){0, 255, 0}); + if (cpu.r[register_r10_cpu_r_index] & (1 << bit)) { + set_color(bit + 32, register_r10_color); } - if (cpu.r[11] & (1 << bit)) { - set_color(bit + 48, (color_t){0, 0, 255}); + if (cpu.r[register_r11_cpu_r_index] & (1 << bit)) { + set_color(bit + 48, register_r11_color); } } - instructionDisplay(); + if (trap == RV_EMECALL) { + break; + } + uint8_t next_pos = (cpu.pc - RAM_BASE) / sizeof(program[0]); + + instructionDisplay((next_pos - 1) / led_page_size_show, + ((next_pos - 1) % led_page_size_show) == 0 ? focus_instruction_0 : focus_instruction_1); WS2812BSimpleSend(LED_PINS, (uint8_t *)led_array, NUM_LEDS * 3); + printf("PC: %lX, ", cpu.pc); printf("Trap: %lX\n", trap); printf("Opcode: %X, ", program[next_pos]); + printf("Next_pos: %d\n", next_pos); + display_all_registers(&cpu); - if (trap == RV_EMECALL) - break; - Delay_Ms(300); + + Delay_Ms(900); } + printf("Environment call @ %lX\n", cpu.csr.mepc); display_all_registers(&cpu); - while (!JOY_act_pressed()) + + while (!JOY_Y_pressed()) ; + Delay_Ms(1000); NVIC_SystemReset(); } + +rv_res bus_cb(void * user, rv_u32 addr, rv_u8 * data, rv_u32 is_store, rv_u32 width) { + rv_u8 * mem = (rv_u8 *)user + addr - RAM_BASE; + if (addr < RAM_BASE || addr + width >= RAM_BASE + RAM_SIZE) + return RV_BAD; + memcpy(is_store ? mem : data, is_store ? data : mem, width); + return RV_OK; +} + +void display_all_registers(rv * cpu) { + for (int i = 0; i < 32; i++) { + if (cpu->r[i] != 0) + printf("r%d: %ld ", i, cpu->r[i]); + } + printf("\n"); +}