summaryrefslogtreecommitdiffstats
path: root/simulator/include/CXXRegister.h
blob: 251a9b9989c1b5ac6f9df6bcd60cceee166262f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
////////////////////////////////////////////////////////////////////////////////
///
/// @file       CXXRegister.h
///
/// @project
///
/// @brief      C++ REgister wrapper code
///
////////////////////////////////////////////////////////////////////////////////
///
////////////////////////////////////////////////////////////////////////////////
///
/// @copyright Copyright (c) 2018, Evan Lojewski
/// @cond
///
/// All rights reserved.
///
/// Redistribution and use in source and binary forms, with or without
/// modification, are permitted provided that the following conditions are met:
/// 1. Redistributions of source code must retain the above copyright notice,
/// this list of conditions and the following disclaimer.
/// 2. Redistributions in binary form must reproduce the above copyright notice,
/// this list of conditions and the following disclaimer in the documentation
/// and/or other materials provided with the distribution.
/// 3. Neither the name of the copyright holder nor the
/// names of its contributors may be used to endorse or promote products
/// derived from this software without specific prior written permission.
///
////////////////////////////////////////////////////////////////////////////////
///
/// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
/// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
/// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
/// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
/// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
/// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
/// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
/// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
/// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
/// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
/// POSSIBILITY OF SUCH DAMAGE.
/// @endcond
////////////////////////////////////////////////////////////////////////////////
#ifndef CXX_REGISTER_H
#define CXX_REGISTER_H

#include <iomanip> // std::setw
#include <iostream>
#include <stdio.h>
#include <utility>
#include <vector>

class CXXRegisterBase
{
public:
    CXXRegisterBase(unsigned int offset, unsigned int width)
    {
        mComponentOffset = 0;
        mMask = 0;
        mBaseRegister = NULL;
        mBitWidth = width;
        mBitPosition = offset;
        for (unsigned int i = offset; i < offset + width; i++)
        {
            mMask |= 1u << i;
        }
    }

    virtual void setBaseRegister(CXXRegisterBase *base)
    {
        // assert(base != NULL, "Base must not be null");

        mBaseRegister = base;
        base->addRelatedRegister(this);
    }

    void setName(const char *name)
    {
        mName = name;
    }

    const char *getName(void)
    {
        return mName;
    }

    void setComponentOffset(unsigned int offset)
    {
        mComponentOffset = offset;
    }

    unsigned int getComponentOffset(void)
    {
        return mComponentOffset;
    }

    void print(unsigned int value, int indent = false)
    {
        unsigned int masked = value & mMask;
        if (indent)
        {
            std::cout << std::right << std::setw(35) << mName << ": 0x"
                      << std::hex << (masked >> mBitPosition) << std::endl;
        }
        else
        {
            std::cout << std::endl
                      << std::left << std::setw(36) << mName << " 0x"
                      << std::hex << (masked >> mBitPosition) << std::endl;
        }
    }

    void printAll(unsigned int value)
    {
        std::vector<CXXRegisterBase *>::iterator it;
        for (it = mRelatedRegisters.begin(); it != mRelatedRegisters.end();
             it++)
        {
            (*it)->print(value, true);
        }
    }

protected:
    unsigned int mComponentOffset;
    unsigned int mBitPosition;
    unsigned int mBitWidth;
    unsigned int mMask;
    const char *mName;

    std::vector<CXXRegisterBase *> mRelatedRegisters;

    // This is the main controller register
    CXXRegisterBase *mBaseRegister;

    virtual void addRelatedRegister(CXXRegisterBase *related)
    {
        mRelatedRegisters.push_back(related);
    }

    // Virtual, must be re-implemented by subclasses
    virtual void doWriteCallbacks(void) = 0;
    virtual void doReadCallbacks(void) = 0;

    virtual unsigned int getRawValue(void) = 0;
    virtual unsigned int getTempValue(void) = 0;
    virtual void setRawValue(unsigned int) = 0;
    virtual void setTempValue(unsigned int) = 0;

    void doRelatedWritesBase(CXXRegisterBase *source)
    {
        if (source->mMask != this->mMask)
        {
            // read latest value as we are only modifying some bits.
            doReadCallbacks();
            setRawValue(getTempValue());
        }

        // Update base temp value with latest write.
        unsigned int base = getRawValue();
        base &= ~(source->mMask);
        unsigned int tempValue = base | source->getRawValue();
        // printf("Updating base from %x & %x to %x (new write: %x)\n",
        // getRawValue(), ~source->mMask, tempValue, source->getRawValue());
        setTempValue(tempValue);

        // Call the write callbacks. This may update the raw value as needed.
        if (this != source)
        {
            doWriteCallbacks();
        }
    }

    void doRelatedWrites()
    {
        // printf("doRelatedWrites on %p\n", this);
        // Call doRelatedWrites on the base register.
        if (mBaseRegister)
        {
            mBaseRegister->doRelatedWritesBase(this);
        }
        else
        {
            // we are the base
            doRelatedWritesBase(this);
        }
    }

    void doRelatedReadsBase(CXXRegisterBase *source)
    {
        // Read the latest from the base register.
        doReadCallbacks();
        unsigned int readValue = getTempValue();

        // Update chained registers.
        std::vector<CXXRegisterBase *>::iterator it;
        for (it = mRelatedRegisters.begin(); it != mRelatedRegisters.end();
             it++)
        {
            // Update chained registers with latest data from base register.
            (*it)->setRawValue(readValue);
            // Update the temp value for all registers.
            (*it)->doReadCallbacks();

            // FIXME: handle updated from chained registers?
        }

        setRawValue(readValue);
    }

    void doRelatedReads()
    {
        // Call doRelatedReads on the base register.
        if (mBaseRegister)
        {
            mBaseRegister->doRelatedReadsBase(this);
        }
        else
        {
            // printf("Calling callbacks...\n");
            doReadCallbacks();
        }
    }
};

template<typename T, unsigned int OFFSET, unsigned int WIDTH>
class CXXRegister : public CXXRegisterBase
{
private:
    typedef T (*callback_t)(T val, unsigned int, void *);
    std::vector<std::pair<callback_t, void *>> mReadCallback;
    std::vector<std::pair<callback_t, void *>> mWriteCallback;

    T mValue;
    T mTempValue;

    virtual void doWriteCallbacks(void)
    {
        T val = mTempValue;
        // call callbacks
        typename std::vector<std::pair<callback_t, void *>>::iterator it;
        for (it = mWriteCallback.begin(); it != mWriteCallback.end(); it++)
        {
            callback_t callback;
            callback = (*it).first;
            if (callback)
            {
                val = callback(val, mComponentOffset, (*it).second);
            }
        }
        mValue = val;
    }

    virtual void doReadCallbacks(void)
    {
        // call callbacks
        T val = mValue;
        typename std::vector<std::pair<callback_t, void *>>::iterator it;
        for (it = mReadCallback.begin(); it != mReadCallback.end(); it++)
        {
            callback_t callback;
            callback = (*it).first;
            if (callback)
            {
                val = callback(val, mComponentOffset, (*it).second);
            }
        }

        mTempValue = val;
    }

    void doWrite(T val)
    {
        // printf("doWrite on %p with %x.\n", this, val);
        mTempValue = val;
        doWriteCallbacks();
        doRelatedWrites();
    }

    T doRead(void)
    {
        // printf("doRead on %p.\n", this);

        doRelatedReads();
        return mTempValue;
    }

    virtual unsigned int getRawValue(void)
    {
        // printf("Getting raw: 0x%x\n", (mValue << mBitPosition) & mMask);
        return (mValue << mBitPosition) & mMask;
    }

    virtual void setRawValue(unsigned int newVal)
    {
        // printf("Setting raw: 0x%x\n", (newVal & mMask) >> mBitPosition);
        mValue = (newVal & mMask) >> mBitPosition;
    }

    virtual unsigned int getTempValue(void)
    {
        // printf("Getting temp: 0x%x\n", (mTempValue << mBitPosition) & mMask);
        return (mTempValue << mBitPosition) & mMask;
    }

    virtual void setTempValue(unsigned int newVal)
    {
        // printf("Setting temp: 0x%x (%x)\n", (newVal & mMask) >> mBitPosition,
        // newVal);
        mTempValue = (newVal & mMask) >> mBitPosition;
    }

public:
    CXXRegister() : CXXRegisterBase(OFFSET, WIDTH)
    {
        mValue = 0;
    }

    CXXRegister(T val) : CXXRegisterBase(OFFSET, WIDTH)
    {
        // Manually instantiated. FIXME.
        mValue = val;
    }

    void installReadCallback(callback_t callback, void *args)
    {
        mReadCallback.push_back(std::make_pair(callback, args));
    }

    void installWriteCallback(callback_t callback, void *args)
    {
        mWriteCallback.push_back(std::make_pair(callback, args));
    }

    virtual ~CXXRegister()
    {
    }

    void print(void)
    {
        T value = doRead();
        CXXRegisterBase::print(value);
        CXXRegisterBase::printAll(value);
    }

    T getValue()
    {
        return mValue;
    }

    void setValue(T val)
    {
        mValue = val & mMask;
    }

    T operator=(T val)
    {
        //  Write
        doWrite(val);
        return mValue;
    }

    T operator=(CXXRegister<T, OFFSET, WIDTH> val)
    {
        //  Write
        doWrite((T)val);
        return mValue;
    }

    operator T()
    {
        // Read
        return doRead();
    }

    T operator+=(T val)
    {
        // Read - sub - Write
        return this->operator=(operator T() + val);
    }

    T operator++(void)
    {
        // Read - add - Write
        return this->operator=(operator T() + 1);
    }

    T operator-=(T val)
    {
        // Read - sub - Write
        return this->operator=(operator T() - val);
    }

    T operator--(void)
    {
        // Read - sub - Write
        return this->operator=(operator T() - 1);
    }

    T operator*=(T val)
    {
        // Read - mul - Write
        return this->operator=(operator T() * val);
    }

    T operator/=(T val)
    {
        // Read - div - Write
        return this->operator=(operator T() / val);
    }

    T operator<<=(T val)
    {
        // Read - left shift - Write
        return this->operator=(operator T() << val);
    }

    T operator>>=(T val)
    {
        // Read - right shift - Write
        return this->operator=(operator T() >> val);
    }

    T operator|=(T val)
    {
        // Read - or - Write
        return this->operator=(operator T() | val);
    }

    T operator&=(T val)
    {
        // Read - and - Write
        return this->operator=(operator T() & val);
    }

    T operator^=(T val)
    {
        // Read - xor - Write
        return this->operator=(operator T() ^ val);
    }

protected:
};

#endif /* CXX_REGISTER_H */
OpenPOWER on IntegriCloud