|
|
1.1 root 1: //
2: // nono
1.1.1.2 root 3: // Copyright (C) 2020 nono project
4: // Licensed under nono-license.txt
1.1 root 5: //
6:
7: #pragma once
8:
9: #include "device.h"
10:
11: // IODevice クラスへの Read/Write をストリームっぽく扱う。
12: // Read/Write とも受け渡しはホストバイトオーダの値で行い、
13: // デバイスへのアクセスはビッグエンディアンで行う。
14: // アクセス中にバスエラーが起きることは考慮していない。
15: class IODeviceStream
16: {
17: public:
18: // コンストラクタ
1.1.1.5 root 19: explicit IODeviceStream(IODevice *dev_) {
20: dev = dev_;
1.1 root 21: }
1.1.1.5 root 22: IODeviceStream(IODevice *dev_, uint32 offset_) {
23: dev = dev_;
24: offset = offset_;
1.1 root 25: }
1.1.1.5 root 26: ~IODeviceStream();
1.1 root 27:
28: // アドレスを設定する
1.1.1.5 root 29: void SetOffset(uint32 offset_) {
30: offset = offset_;
1.1 root 31: }
32: // アドレスを取得する
1.1.1.5 root 33: uint32 GetOffset() const {
34: return offset;
1.1 root 35: }
36:
37: // 1バイト読み込んでポインタを進める
1.1.1.5 root 38: uint64 Read1() {
39: return ReadN(BusAddr::Size1);
1.1 root 40: }
41:
42: // 2バイト読み込んでポインタを進める。addr はアラインしなくてよい
1.1.1.5 root 43: uint64 Read2() {
44: return ReadN(BusAddr::Size2);
1.1 root 45: }
46:
47: // 4バイト読み込んでポインタを進める。addr はアラインしなくてよい
48: // データはホストエンディアンで返される。
1.1.1.5 root 49: uint64 Read4() {
50: return ReadN(BusAddr::Size4);
1.1 root 51: }
52:
53: // 1バイト書き込んでポインタを進める
1.1.1.5 root 54: void Write1(uint32 data)
1.1 root 55: {
1.1.1.5 root 56: WriteN(BusAddr::Size1, data);
1.1 root 57: }
58:
59: // 2バイト書き込んでポインタを進める。addr はアラインしてなくてよい
1.1.1.5 root 60: void Write2(uint32 data)
1.1 root 61: {
1.1.1.5 root 62: WriteN(BusAddr::Size2, data);
1.1 root 63: }
64:
1.1.1.6 ! root 65: // 3バイト書き込んでポインタを進める。addr はアラインしてなくてよい
! 66: void Write3(uint32 data)
! 67: {
! 68: WriteN(BusAddr::Size3, data);
! 69: }
! 70:
1.1 root 71: // 4バイト書き込んでポインタを進める。addr はアラインしてなくてよい
1.1.1.5 root 72: void Write4(uint32 data)
1.1 root 73: {
1.1.1.5 root 74: WriteN(BusAddr::Size4, data);
1.1 root 75: }
76:
1.1.1.4 root 77: // LE で2バイト書き込んでポインタを進める。addr はアラインしてなくてよい
1.1.1.5 root 78: void Write2LE(uint32 data)
1.1.1.4 root 79: {
1.1.1.5 root 80: Write1(data & 0xff);
81: Write1(data >> 8);
1.1.1.4 root 82: }
83:
84: // 文字列を書き込んでポインタを進める。
85: void WriteString(const char *p)
86: {
87: while (*p) {
1.1.1.5 root 88: Write1(*p++);
1.1.1.4 root 89: }
1.1.1.5 root 90: Write1('\0');
1.1.1.4 root 91: }
92:
1.1.1.5 root 93: // ホストの src から len バイトを書き込んでポインタを進める。
94: void WriteMem(const void *src, uint len);
95:
1.1 root 96: // アドレスも指定する版。ポインタは同様に進める
1.1.1.5 root 97: uint64 Read1(uint32 offset_) {
98: SetOffset(offset_);
99: return Read1();
100: }
101: uint64 Read2(uint32 offset_) {
102: SetOffset(offset_);
103: return Read2();
104: }
105: uint64 Read4(uint32 offset_) {
106: SetOffset(offset_);
107: return Read4();
108: }
109: void Write1(uint32 offset_, uint32 data) {
110: SetOffset(offset_);
111: Write1(data);
112: }
113: void Write2(uint32 offset_, uint32 data) {
114: SetOffset(offset_);
115: Write2(data);
116: }
117: void Write4(uint32 offset_, uint32 data) {
118: SetOffset(offset_);
119: Write4(data);
1.1 root 120: }
121:
122: private:
1.1.1.5 root 123: busdata ReadN(busaddr size);
124: busdata WriteN(busaddr size, uint32 data);
125:
1.1.1.3 root 126: IODevice *dev {};
1.1.1.5 root 127: uint32 offset {};
1.1 root 128: };
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.