|
|
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:
65: // 4バイト書き込んでポインタを進める。addr はアラインしてなくてよい
1.1.1.5 ! root 66: void Write4(uint32 data)
1.1 root 67: {
1.1.1.5 ! root 68: WriteN(BusAddr::Size4, data);
1.1 root 69: }
70:
1.1.1.4 root 71: // LE で2バイト書き込んでポインタを進める。addr はアラインしてなくてよい
1.1.1.5 ! root 72: void Write2LE(uint32 data)
1.1.1.4 root 73: {
1.1.1.5 ! root 74: Write1(data & 0xff);
! 75: Write1(data >> 8);
1.1.1.4 root 76: }
77:
78: // 文字列を書き込んでポインタを進める。
79: void WriteString(const char *p)
80: {
81: while (*p) {
1.1.1.5 ! root 82: Write1(*p++);
1.1.1.4 root 83: }
1.1.1.5 ! root 84: Write1('\0');
1.1.1.4 root 85: }
86:
1.1.1.5 ! root 87: // ホストの src から len バイトを書き込んでポインタを進める。
! 88: void WriteMem(const void *src, uint len);
! 89:
1.1 root 90: // アドレスも指定する版。ポインタは同様に進める
1.1.1.5 ! root 91: uint64 Read1(uint32 offset_) {
! 92: SetOffset(offset_);
! 93: return Read1();
! 94: }
! 95: uint64 Read2(uint32 offset_) {
! 96: SetOffset(offset_);
! 97: return Read2();
! 98: }
! 99: uint64 Read4(uint32 offset_) {
! 100: SetOffset(offset_);
! 101: return Read4();
! 102: }
! 103: void Write1(uint32 offset_, uint32 data) {
! 104: SetOffset(offset_);
! 105: Write1(data);
! 106: }
! 107: void Write2(uint32 offset_, uint32 data) {
! 108: SetOffset(offset_);
! 109: Write2(data);
! 110: }
! 111: void Write4(uint32 offset_, uint32 data) {
! 112: SetOffset(offset_);
! 113: Write4(data);
1.1 root 114: }
115:
116: private:
1.1.1.5 ! root 117: busdata ReadN(busaddr size);
! 118: busdata WriteN(busaddr size, uint32 data);
! 119:
1.1.1.3 root 120: IODevice *dev {};
1.1.1.5 ! root 121: uint32 offset {};
1.1 root 122: };
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.