]> git.sesse.net Git - pistorm/blob - platforms/shared/common.c
Merge branch 'wip-crap' into main
[pistorm] / platforms / shared / common.c
1 // SPDX-License-Identifier: MIT
2
3 #include <time.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <stdint.h>
7 #include <endian.h>
8 #include "platforms/platforms.h"
9 #include "gpio/ps_protocol.h"
10
11 void dump_range_to_file(uint32_t addr, uint32_t size, char *filename) {
12     FILE *out = fopen(filename, "wb+");
13     if (out == NULL) {
14         printf ("[SHARED-DUMP_RANGE_TO_FILE] Failed to open %s for writing.\n", filename);
15         printf ("[SHARED-DUMP_RANGE_TO_FILE] Memory range has not been dumped to file.\n");
16         return;
17     }
18
19     for (uint32_t i = 0; i < size; i += 2) {
20         uint16_t in = be16toh(read16(addr + i));
21         fwrite(&in, 2, 1, out);
22     }
23
24     fclose(out);
25     printf ("[SHARED-DUMP_RANGE_TO_FILE] Memory range dumped to file %s.\n", filename);
26 }
27
28 uint8_t *dump_range_to_memory(uint32_t addr, uint32_t size) {
29     uint8_t *mem = calloc(size, 1);
30
31     if (mem == NULL) {
32         printf ("[SHARED-DUMP_RANGE_TO_MEMORY] Failed to allocate memory for dumped range.\n");
33         return NULL;
34     }
35
36     for (uint32_t i = 0; i < size; i += 2) {
37         *(uint16_t *)&mem[i] = (uint16_t)be16toh(read16(addr + i));
38     }
39
40     printf ("[SHARED-DUMP_RANGE_TO_FILE] Memory range copied to RAM.\n");
41     return mem;
42 }