1 #ifndef AVR_REGISTERS_H
2 #define AVR_REGISTERS_H
17 * Represents general purpose registers and IORegisters
22 Register(word address
, const std::string
& name
);
27 * Initialize the register
28 * @param address the address of the register (IO address not mem)
29 * @param name the name of the register
31 void init(word address
, const std::string
& name
);
34 * Changes the register value to \e val.
36 unsigned char operator =(unsigned char val
);
39 * Returns the register value.
41 operator unsigned char() const;
44 * Changes the register value to the value of register \reg.
46 void operator =(const Register
& reg
);
49 void set(unsigned char val
);
50 unsigned char get() const;
54 * Returns the IO address of the register
56 word
getAddress() const { return address
; }
59 * Returns the name of the register
61 const std::string
& getName() const { return name
; }
72 * @brief An IOregister
74 * Adds functionality to Register for IORegisters.
75 * Hardware can be bound to an IORegister and it will be
76 * notified on changes.
78 * Some registers share an I/O location with another register.
79 * When doing a write access to this I/O location, the high bit
80 * controls which one of the two registers will be written.
82 * Read access is more complex: the read access is controlled by a
83 * timed sequence. Reading the I/O location once returns the first
84 * register content. If the register location was read in the
85 * previous clock cycle, reading the register in the current cycle
86 * will return the second register content.
88 * Note that the timed sequence for reading the shared location is
89 * an atomic operation. Interrupts must therefore be controlled
90 * (e.g., by disabling interrupts globally) during the read operation.
92 * These type of locations have to be taken into account elsewhere!
94 class IORegister
: public Register
{
96 IORegister(word address
, const std::string
& name
,
97 unsigned char initial
= 0);
101 * Reset the register to its initial value.
106 * Returns the register value.
108 operator unsigned char();
111 * Changes the register value to \e val
113 unsigned char operator =(unsigned char val
);
115 void operator |=(unsigned char val
);
116 void operator &=(unsigned char val
);
120 * Bind an internal device to this register.
121 * The hardware will be notified on register value changes.
123 void registerHW(Hardware
*hw
);
126 unsigned char initial
;
133 * @brief All IORegisters
135 * Contains all IORegisters and provides an interface for the MMU.
139 IORegisters(unsigned int count
);
143 unsigned int size() const { return regs
.size(); }
146 * Reset all registers to their initial value.
151 * Reads a single bytes of raw data from the register memory
152 * at offset \e offset.
154 byte
readByte(unsigned int offset
) const;
157 * Writes a single bytes of raw data to the register memory
158 * at offset \e offset.
160 void writeByte(unsigned int offset
, byte val
);
163 void addReg( word addr
, IORegister
*reg
);
164 IORegister
& getIoreg(unsigned int offset
) const;
165 IORegister
*getIoreg(const std::string
& name
, bool required
= true) const;
168 std::vector
<IORegister
*> regs
;
171 inline Register::Register(word address
, const std::string
& name
) {
172 init( address
, name
);
175 inline void Register::init(word address
, const std::string
& name
) {
176 this->address
= address
;
180 inline IORegister::IORegister(word address
,
181 const std::string
& name
, unsigned char initial
)
182 : Register(address
, name
), initial(initial
), hw(0) {
185 inline void IORegister::reset() {
189 inline void Register::set(unsigned char val
) {
193 inline unsigned char Register::get() const {
197 inline void Register::operator =(const Register
& reg
) {
198 *this = (unsigned char)reg
;
201 inline byte
IORegisters::readByte(unsigned int offset
) const {
202 if( regs
[offset
] != 0 )
203 return (byte
)*regs
[offset
];
208 inline unsigned char Register::operator =(unsigned char val
) {
209 unsigned char ret
= this->val
;
214 inline Register::operator unsigned char() const {
218 inline void IORegisters::writeByte(unsigned int offset
, byte val
) {
219 if( regs
[offset
] != 0 )
220 (*regs
[offset
]) = val
;
223 inline IORegister
& IORegisters::getIoreg(unsigned int offset
) const {
224 return *regs
[offset
];
227 inline void IORegister::registerHW(Hardware
*hw
) {
233 #endif /*AVR_REGISTERS_H*/