]> cloudbase.mooo.com Git - z180-stamp.git/blame_incremental - avr/print-utils.c
env.c needs getopt-min.h
[z180-stamp.git] / avr / print-utils.c
... / ...
CommitLineData
1/*
2 * (C) Copyright 2014 Leo C. <erbl259-lmu@yahoo.de>
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7#include "common.h"
8#include <stdint.h>
9#include <stdio.h>
10#include <ctype.h>
11#include "con-utils.h"
12#include "print-utils.h"
13
14void print_blanks(uint_fast8_t count)
15{
16 while(count--)
17 putchar(' ');
18}
19
20
21int eeprom_read_buf(uint8_t *buf, uint32_t addr, uint8_t count)
22{
23 eeprom_read_block((void *) buf, (const void *) (size_t) addr, count);
24 return 0;
25}
26
27int ram_read_buf(uint8_t *buf, uint32_t addr, uint8_t count)
28{
29 while (count--)
30 *buf++ = *(uint8_t *) (size_t) addr++;
31 return 0;
32}
33
34int flash_read_buf(uint8_t *buf, uint32_t addr, uint8_t count)
35{
36 while (count--)
37 *buf++ = *(const __memx uint8_t *) (__uint24) addr++;
38 return 0;
39}
40
41int dump_mem(uint32_t address, uint32_t offset, uint32_t len,
42 int (*readfkt)(uint8_t *, uint32_t, uint8_t), char *title)
43{
44 uint8_t buf[16];
45 char *indent = NULL;
46 uint8_t llen = 16;
47 uint8_t pre = offset % 16;
48 offset = offset & ~0x0f;
49 len += pre;
50 uint8_t i;
51
52 if (title && *title) {
53 printf_P(PSTR("%s\n"),title);
54 indent = " ";
55 }
56
57 while (len) {
58 if (len < 16)
59 llen = len;
60 if (readfkt(buf, address, llen - pre) != 0)
61 return -2; /* TODO: Error codes */
62
63 printf_P(PSTR("%s%.5lx:"),indent, offset);
64 for (i = 0; i < llen; i++) {
65 if ((i % 8) == 0)
66 putchar(' ');
67 if (i < pre)
68 printf_P(PSTR(".. "));
69 else
70 printf_P(PSTR("%.2x "), buf[i-pre]);
71 }
72 /* fill line with whitespace for nice ASCII print */
73 print_blanks(3 * (16u - i) + (16u-i)/8 + 1 + pre);
74 /* Print data in ASCII characters */
75 for (i = pre; i < llen; i++)
76 printf_P(PSTR("%c"), isprint(buf[i-pre]) ? buf[i-pre] : '.');
77 putchar('\n');
78
79 address += llen - pre;
80 offset += 16;
81 pre = 0;
82 len -= llen;
83
84 if (ctrlc())
85 return -1;
86 }
87 return 0;
88}
89
90void dump_eep(uint32_t addr, unsigned int len, char *title)
91{
92 dump_mem(addr, addr, len, eeprom_read_buf, title);
93}
94
95void dump_ram(uint8_t *addr, size_t offset, unsigned int len, char *title)
96{
97 dump_mem((uint32_t) (size_t) addr, offset, len, ram_read_buf, title);
98}