sim_ioreg.h Source File

yasimavr: sim_ioreg.h Source File
yasimavr
Loading...
Searching...
No Matches
sim_ioreg.h
Go to the documentation of this file.
1/*
2 * sim_ioreg.h
3 *
4 * Copyright 2021-2026 Clement Savergne <csavergne@yahoo.com>
5
6 This file is part of yasim-avr.
7
8 yasim-avr is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 yasim-avr is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with yasim-avr. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22//=======================================================================================
23
24#ifndef __YASIMAVR_IOREG_H__
25#define __YASIMAVR_IOREG_H__
26
27#include "sim_types.h"
28#include <vector>
29
31
32
33//=======================================================================================
34
40 uint8_t value;
41 uint8_t old;
42 //indicates bits transiting from '0' to '1'
43 inline uint8_t posedge() const
44 {
45 return value & ~old;
46 }
47
48 //indicates bits transiting from '1' to '0'
49 inline uint8_t negedge() const
50 {
51 return old & ~value;
52 }
53
54 //indicates changed bits
55 inline uint8_t anyedge() const
56 {
57 return value ^ old;
58 }
59
60};
61
62
63//=======================================================================================
70
71public:
72
73 virtual ~IORegHandler() = default;
74
82 virtual uint8_t ioreg_read_handler(reg_addr_t addr, uint8_t value) = 0;
83
91 virtual uint8_t ioreg_peek_handler(reg_addr_t addr, uint8_t value) = 0;
92
100 virtual void ioreg_write_handler(reg_addr_t addr, const ioreg_write_t& data) = 0;
101
102};
103
104
105//=======================================================================================
117
118public:
119
120 enum BitMode {
124 };
125
126 explicit IORegister(bitmask_t initial_mask = bitmask_t(), BitMode initial_mode = RW);
127 ~IORegister();
128
129 uint8_t value() const;
130 void set(uint8_t value);
131
132 void add_bits(bitmask_t mask, BitMode mode);
133
134 void add_handler(IORegHandler& handler);
135
136 uint8_t cpu_read(reg_addr_t addr);
137 void cpu_write(reg_addr_t addr, uint8_t value);
138
139 uint8_t ioctl_read(reg_addr_t addr);
140 void ioctl_write(reg_addr_t addr, uint8_t value);
141
142 uint8_t dbg_peek(reg_addr_t addr);
143
144 IORegister(const IORegister&) = delete;
145 IORegister& operator=(const IORegister&) = delete;
146
147private:
148
149 //Contains the current 8-bits value of this register
150 uint8_t m_value;
151 //Pointer to the register handler, which is called notified when the register is accessed by the CPU
152 IORegHandler *m_handler;
153 //Flag set
154 uint8_t m_flags;
155 //Mask indicating which bits of the register are used
156 bitmask_t m_use_mask;
157 //Masks indicating which bits of the register are read-only or strobe for the CPU
158 bitmask_t m_ro_mask;
159 bitmask_t m_strobe_mask;
160
161};
162
164inline uint8_t IORegister::value() const
165{
166 return m_value;
167}
168
170inline void IORegister::set(uint8_t value)
171{
172 m_value = value;
173}
174
175
177
178#endif //__YASIMAVR_IOREG_H__
Definition sim_ioreg.h:69
virtual uint8_t ioreg_read_handler(reg_addr_t addr, uint8_t value)=0
virtual uint8_t ioreg_peek_handler(reg_addr_t addr, uint8_t value)=0
virtual ~IORegHandler()=default
virtual void ioreg_write_handler(reg_addr_t addr, const ioreg_write_t &data)=0
Definition sim_ioreg.h:116
BitMode
Definition sim_ioreg.h:120
@ RW
Read&Write mode.
Definition sim_ioreg.h:121
@ RO
Read only mode.
Definition sim_ioreg.h:122
@ Strobe
Strobe mode.
Definition sim_ioreg.h:123
uint8_t value() const
Simple inline interface to access the value.
Definition sim_ioreg.h:164
IORegister(const IORegister &)=delete
IORegister & operator=(const IORegister &)=delete
void set(uint8_t value)
Simple inline interface to access the value.
Definition sim_ioreg.h:170
Representation of a I/O register address, with validity state.
Definition sim_types.h:60
#define YASIMAVR_BEGIN_NAMESPACE
Definition sim_globals.h:58
#define AVR_CORE_PUBLIC_API
Definition sim_globals.h:46
#define YASIMAVR_END_NAMESPACE
Definition sim_globals.h:59
Bit mask structure for bitwise operations on 8-bits registers.
Definition sim_types.h:86
Definition sim_ioreg.h:39
uint8_t old
previous value of the register
Definition sim_ioreg.h:41
uint8_t posedge() const
Definition sim_ioreg.h:43
uint8_t value
new value of the register
Definition sim_ioreg.h:40
uint8_t negedge() const
Definition sim_ioreg.h:49
uint8_t anyedge() const
Definition sim_ioreg.h:55