gmod-lcpu/test-gmod/main.c

131 lines
2.5 KiB
C
Raw Normal View History

// a simple test program - this version talks to a device
// that is implemented in GLua thanks to the LCPU native addon
2023-07-25 06:46:52 -04:00
#include <stdarg.h>
#include <stdint.h>
2023-07-25 06:46:52 -04:00
uint32_t strlen(const char* str) {
if(!str)
return 0;
const char* c = str;
while(*c++)
;
return c - str;
}
#define GLUA_DEVICE_BASE 0x11300000 // base address of the lua test device
#define GLUA_DEVICE_WORLDTIME *(volatile uint32_t*)GLUA_DEVICE_BASE // world time register (read only)
#define GLUA_DEVICE_LUAREG *(volatile uint32_t*)(GLUA_DEVICE_BASE + 4) // lua number register (read/write)
2023-07-25 06:46:52 -04:00
#define SYSCON *(volatile uint32_t*)0x11100000
#define UART_BASE 0x10000000
#define UART_DATA *(volatile uint32_t*)UART_BASE
#define UART_STATUS UART_DATA
void putc(char c) {
UART_DATA = (uint32_t)c;
}
__attribute__((noinline)) void puts(const char* str) {
const uint32_t length = strlen(str);
for(uint32_t i = 0; i < length; ++i)
putc(str[i]);
}
int itoa(int value, char* sp, int radix) {
char tmp[16];
char* tp = tmp;
int i;
unsigned v;
int sign = (radix == 10 && value < 0);
if(sign)
v = -value;
else
v = (unsigned)value;
while(v || tp == tmp) {
i = v % radix;
v /= radix;
if(i < 10)
*tp++ = i + '0';
else
*tp++ = i + 'a' - 10;
}
int len = tp - tmp;
if(sign) {
*sp++ = '-';
len++;
}
while(tp > tmp)
*sp++ = *--tp;
*sp = '\0';
return len;
}
void vprintf(const char* format, va_list val) {
const int fl = strlen(format);
for(int i = 0; i < fl; ++i) {
switch(format[i]) {
case '%':
if(format[i + 1] == '%')
2023-07-25 06:46:52 -04:00
putc('%');
if(format[i+1] == '\0')
return;
switch(format[i + 1]) {
2023-07-25 06:46:52 -04:00
case 'i':
case 'd': {
char a[32];
itoa(va_arg(val, uint32_t), &a[0], 10);
const int al = strlen(a);
for(int j = 0; j < al; ++j)
putc(a[j]);
i++;
2023-07-25 06:46:52 -04:00
} break;
case 's': {
char* p = va_arg(val, char*);
if(!p)
puts("(null)");
else
puts(p);
i++;
2023-07-25 06:46:52 -04:00
};
default: putc(' '); break;
2023-07-25 06:46:52 -04:00
}
break;
case '\0': // band-aid fix.
return;
default: putc(format[i]); break;
2023-07-25 06:46:52 -04:00
}
}
}
void printf(const char* format, ...) {
va_list val;
va_start(val, format);
vprintf(format, val);
2023-07-25 06:46:52 -04:00
va_end(val);
}
void main() {
puts("fuck you garry I win\n");
2023-07-25 06:46:52 -04:00
for(int i = 0; i < 8; ++i)
printf("GLUA_DEVICE_WORLDTIME reading says -> %d\n", GLUA_DEVICE_WORLDTIME);
// try writing to it
GLUA_DEVICE_LUAREG = 0x1234;
for(int i = 0; i < 8; ++i)
printf("GLUA_DEVICE_LUAREG reading says -> %d\n", GLUA_DEVICE_LUAREG);
2023-07-25 06:46:52 -04:00
SYSCON = 0x5555;
}