|
| 1 | +#include <stdio.h> |
| 2 | +#include <stdlib.h> |
| 3 | +#include <stdint.h> |
| 4 | +#include <signal.h> |
| 5 | + |
| 6 | +extern int ghdl_main (int argc, char **argv); |
| 7 | + |
| 8 | +uint8_t *D[1]; |
| 9 | +char is_allocated[1] = {0}; |
| 10 | +const uint32_t length = 5; |
| 11 | + |
| 12 | +void set_string_ptr(uint8_t id, uint8_t *p) { |
| 13 | + //printf("C set_string_ptr(%d, %p)\n", id, p); |
| 14 | + D[id] = p; |
| 15 | + is_allocated[id] = 1; |
| 16 | +} |
| 17 | + |
| 18 | +uintptr_t get_string_ptr(uint8_t id) { |
| 19 | + //printf("C get_string_ptr(%d): %p\n", id, D[id]); |
| 20 | + return (uintptr_t)D[id]; |
| 21 | +} |
| 22 | + |
| 23 | +void write_char(uint8_t id, uint32_t i, uint8_t v ) { |
| 24 | + //printf("C write_char(%d, %d): %d\n", id, i, v); |
| 25 | + D[id][i] = v; |
| 26 | +} |
| 27 | + |
| 28 | +uint8_t read_char(uint8_t id, uint32_t i) { |
| 29 | + //printf("C read_char(%d, %d): %d\n", id, i, D[id][i]); |
| 30 | + return D[id][i]; |
| 31 | +} |
| 32 | + |
| 33 | +void sigabrtHandler(int sig_num) |
| 34 | +{ |
| 35 | + /* Reset handler to catch SIGINT next time. Refer http://en.cppreference.com/w/c/program/signal */ |
| 36 | + signal(SIGABRT, sigabrtHandler); |
| 37 | + printf("\nSIGABRT caught!\n"); |
| 38 | + fflush(stdout); |
| 39 | +} |
| 40 | + |
| 41 | +static void exit_handler(void) { |
| 42 | + if (is_allocated[0] == 0) { |
| 43 | + int i; |
| 44 | + for(i=0; i<3*length; i++) { |
| 45 | + printf("%d: %d\n", i, D[0][i]); |
| 46 | + } |
| 47 | + free(D[0]); |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +int main(int argc, char **argv) { |
| 52 | + if (is_allocated[0] == 0) { |
| 53 | + D[0] = (uint8_t *) malloc(3*length*sizeof(uint8_t)); |
| 54 | + if ( D[0] == NULL ) { |
| 55 | + perror("execution of malloc() failed!\n"); |
| 56 | + return -1; |
| 57 | + } |
| 58 | + int i; |
| 59 | + for(i=0; i<length; i++) { |
| 60 | + D[0][i] = (i+1)*11; |
| 61 | + } |
| 62 | + for(i=0; i<3*length; i++) { |
| 63 | + printf("%d: %d\n", i, D[0][i]); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + signal(SIGABRT, sigabrtHandler); |
| 68 | + |
| 69 | + atexit(exit_handler); |
| 70 | + return ghdl_main(argc, argv); |
| 71 | +} |
0 commit comments