diff --git a/main.S b/main.S index a8a4749..b15a677 100644 --- a/main.S +++ b/main.S @@ -4,63 +4,37 @@ .attribute unaligned_access, 0 .attribute stack_align, 16 .text + .globl GPIO_PIN + .section .srodata,"a" + .align 2 + .type GPIO_PIN, @object + .size GPIO_PIN, 4 +GPIO_PIN: + .word 1 + .globl counter + .section .sbss,"aw",@nobits + .align 2 + .type counter, @object + .size counter, 4 +counter: + .zero 4 + .text .align 1 - .type simpleDelay, @function -simpleDelay: - addi sp,sp,-32 - sd s0,24(sp) - addi s0,sp,32 - sw zero,-20(s0) - j .L2 -.L3: - #APP -# 16 "./main.c" 1 + .globl delay + .type delay, @function +delay: + addi sp,sp,-48 + sd s0,40(sp) + addi s0,sp,48 + mv a5,a0 + sw a5,-36(s0) nop -# 0 "" 2 - #NO_APP - lw a5,-20(s0) - addiw a5,a5,1 - sw a5,-20(s0) .L2: - lw a5,-20(s0) - sext.w a4,a5 - li a5,999424 - addi a5,a5,575 - ble a4,a5,.L3 - nop - nop - ld s0,24(sp) - addi sp,sp,32 - jr ra - .size simpleDelay, .-simpleDelay - .align 1 - .globl main - .type main, @function -main: - addi sp,sp,-16 - sd ra,8(sp) - sd s0,0(sp) - addi s0,sp,16 - li a5,268500992 - li a4,1 - sw a4,0(a5) -.L5: - li a5,268500992 + li a5,33603584 + addi a5,a5,-8 lw a5,0(a5) sext.w a4,a5 - li a5,268500992 - ori a4,a4,1 - sext.w a4,a4 - sw a4,0(a5) - call simpleDelay - li a5,268500992 - lw a5,0(a5) - sext.w a4,a5 - li a5,268500992 - andi a4,a4,-2 - sext.w a4,a4 - sw a4,0(a5) - call simpleDelay - j .L5 - .size main, .-main - .ident "GCC: (SiFive GCC-Metal 10.2.0-2020.12.8) 10.2.0" + lw a5,-20(s0) + subw a5,a4,a5 +# ... +# more contents omitted \ No newline at end of file diff --git a/main.c b/main.c index bfbff7b..95cfe4e 100644 --- a/main.c +++ b/main.c @@ -1,30 +1,42 @@ -#define SET_BITS(REG, BIT) ((REG) |= (BIT)) -#define CLEAR_BITS(REG, BIT) ((REG) &= ~(BIT)) +// some handy macros to do bit operations +#define SET_BITS(REG, BIT) ((REG) |= (BIT)) +#define CLEAR_BITS(REG, BIT) ((REG) &= !(BIT)) -#define GPIOA_BASE 0x10010000U +// peripheral MMIO addresses +#define GPIOA_OUTPUT_VAL 0x1001000CUL +#define GPIOA_OUTPUT_EN 0x10010008UL +#define CLINT_MTIME 0x0200BFF8UL -#define GPIO_PIN_0 0b0001U -#define GPIO_PIN_1 0b0001U -#define GPIO_PIN_2 0b0001U -#define GPIO_PIN_3 0b0001U +// the pin we are using +const unsigned int GPIO_PIN = 0x01; +// a global counter +volatile unsigned int counter; - - -static inline void simpleDelay() { - for (int i=0; i<1000000; i+=1) { - __asm__("nop"); - } +// A simple delay function. +void delay(unsigned int ticks) { + unsigned int mtime_start; + while ((*(volatile unsigned int *)CLINT_MTIME) - mtime_start < ticks) {} } -int main() { - *(unsigned int volatile *)(GPIOA_BASE + 0x00U) = GPIO_PIN_0; +void main() { + // enable GPIOA as output + SET_BITS(*(volatile unsigned int *)GPIOA_OUTPUT_EN, GPIO_PIN); while (1) { - SET_BITS(*(unsigned int volatile *)(GPIOA_BASE + 0x00U), GPIO_PIN_0); - simpleDelay(); + // if counter is even, turn on the LED, otherwise turn it off + if (counter % 2 == 0) { + SET_BITS(*(volatile unsigned int *)GPIOA_OUTPUT_VAL, GPIO_PIN); + } else { + CLEAR_BITS(*(volatile unsigned int *)GPIOA_OUTPUT_VAL, GPIO_PIN); + } + + // delay for 1 second + delay(1000); - CLEAR_BITS(*(unsigned int volatile *)(GPIOA_BASE + 0x00U), GPIO_PIN_0); - simpleDelay(); + // increment the counter + counter += 1; } -} + + // we won't reach here if everything is working +} \ No newline at end of file diff --git a/main.i b/main.i index 88a9ab1..3d34df1 100644 --- a/main.i +++ b/main.i @@ -2,21 +2,36 @@ # 1 "" # 1 "" # 1 "./main.c" -# 14 "./main.c" -static inline void simpleDelay() { - for (int i=0; i<1000000; i+=1) { - __asm__("nop"); - } +# 11 "./main.c" +const unsigned int GPIO_PIN = 0x01; + + +volatile unsigned int counter; + + +void delay(unsigned int ticks) { + unsigned int mtime_start; + while ((*(volatile unsigned int *)0x0200BFF8UL) - mtime_start < ticks) {} } -int main() { - *(unsigned int volatile *)(0x10010000U + 0x00U) = 0b0001U; +void main() { + + ((*(volatile unsigned int *)0x10010008UL) |= (GPIO_PIN)); while (1) { - ((*(unsigned int volatile *)(0x10010000U + 0x00U)) |= (0b0001U)); - simpleDelay(); - ((*(unsigned int volatile *)(0x10010000U + 0x00U)) &= ~(0b0001U)); - simpleDelay(); + if (counter % 2 == 0) { + ((*(volatile unsigned int *)0x1001000CUL) |= (GPIO_PIN)); + } else { + ((*(volatile unsigned int *)0x1001000CUL) &= !(GPIO_PIN)); + } + + + delay(1000); + + + counter += 1; } -} + + +} \ No newline at end of file