VirtualC64 v5.0 beta
Commodore 64 Emulator
Loading...
Searching...
No Matches
StrWriter.h
1// -----------------------------------------------------------------------------
2// This file is part of Peddle - A MOS 65xx CPU emulator
3//
4// Copyright (C) Dirk W. Hoffmann. www.dirkwhoffmann.de
5// Published under the terms of the MIT License
6// -----------------------------------------------------------------------------
7
8#pragma once
9
10#include "Peddle.h"
11#include <cmath>
12
13namespace vc64::peddle {
14
15//
16// Wrapper structures controlling the output format
17//
18
19// Mnemonics
20struct Ins { u8 raw; };
21
22// Addressing modes
23struct Imm { u8 raw; };
24struct Zp { u8 raw; };
25struct Zpx { u8 raw; };
26struct Zpy { u8 raw; };
27struct Abs { u16 raw; };
28struct Absx { u16 raw; };
29struct Absy { u16 raw; };
30struct Ind { u16 raw; };
31struct Indx { u8 raw; };
32struct Indy { u8 raw; };
33struct Rel { u16 raw; };
34struct Dir { u16 raw; };
35
36// Indentation
37struct Tab { };
38struct Sep { };
39
40// Finish
41struct Fin { };
42
43class StrWriter
44{
45
46public:
47
48 char *base; // Start address of the destination string
49 char *ptr; // Write pointer
50 const DasmStyle &style;
51
52public:
53
54 StrWriter(char *p, const DasmStyle &style) : style(style) {
55
56 base = ptr = p;
57 };
58
59 isize length() { return isize(ptr - base); }
60 void fill(isize tab) { while (ptr < base + tab) *ptr++ = ' '; }
61
62private:
63
64 isize decDigits(u64 value) { return value ? 1 + (isize)log10(value) : 1; }
65 isize binDigits(u64 value) { return value ? 1 + (isize)log2(value) : 1; }
66 isize hexDigits(u64 value) { return (binDigits(value) + 3) / 4; }
67
68 void sprintd(char *&s, u64 value, isize digits);
69 void sprintd(char *&s, u64 value);
70 void sprintx(char *&s, u64 value, isize digits);
71 void sprintx(char *&s, u64 value);
72
73public:
74
75 StrWriter& operator<<(char);
76 StrWriter& operator<<(const char *);
77 StrWriter& operator<<(u8);
78 StrWriter& operator<<(u16);
79
80 StrWriter& operator<<(Ins);
81 StrWriter& operator<<(Imm);
82 StrWriter& operator<<(Zp);
83 StrWriter& operator<<(Zpx);
84 StrWriter& operator<<(Zpy);
85 StrWriter& operator<<(Abs);
86 StrWriter& operator<<(Absx);
87 StrWriter& operator<<(Absy);
88 StrWriter& operator<<(Ind);
89 StrWriter& operator<<(Indx);
90 StrWriter& operator<<(Indy);
91 StrWriter& operator<<(Rel);
92 StrWriter& operator<<(Dir);
93
94 StrWriter& operator<<(Tab);
95 StrWriter& operator<<(Sep);
96
97 StrWriter& operator<<(Fin);
98};
99
100}