Skip to content

Commit 3e98d05

Browse files
committed
add cpu_io
Signed-off-by: Zone.NiuZH <[email protected]>
1 parent eaeb31c commit 3e98d05

File tree

2 files changed

+43
-59
lines changed

2 files changed

+43
-59
lines changed

include/x86_64/cpu.hpp

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ static __always_inline auto In(const uint32_t port) -> T {
5151
} else if constexpr (std::is_same_v<T, uint32_t>) {
5252
__asm__ volatile("inl %1, %0" : "=a"(data) : "dN"(port));
5353
} else {
54-
static_assert(false, "No Type\n");
55-
throw;
54+
static_assert(sizeof(T) == 0);
5655
}
5756
return data;
5857
}
@@ -72,8 +71,7 @@ static __always_inline void Out(const uint32_t port, const T data) {
7271
} else if constexpr (std::is_same_v<T, uint32_t>) {
7372
__asm__ volatile("outl %1, %0" : : "dN"(port), "a"(data));
7473
} else {
75-
static_assert(false, "No Type\n");
76-
throw;
74+
static_assert(sizeof(T) == 0);
7775
}
7876
}
7977

@@ -184,27 +182,27 @@ class Pic {
184182
explicit Pic(uint8_t offset1, uint8_t offset2)
185183
: offset1_(offset1), offset2_(offset2) {
186184
// 0001 0001
187-
Out<uint8_t>(kMasterCommand, kIcw1Init | kIcw1Icw4);
185+
Out<uint8_t>(kMasterCommandPort, kIcw1Init | kIcw1Icw4);
188186
// 设置主片 IRQ 从 offset1_ 号中断开始
189-
Out<uint8_t>(kMasterData, offset1_);
187+
Out<uint8_t>(kMasterDataPort, offset1_);
190188
// 设置主片 IR2 引脚连接从片
191189
// 4: 0000 0100
192-
Out<uint8_t>(kMasterData, 4);
190+
Out<uint8_t>(kMasterDataPort, 4);
193191
// 设置主片按照 8086 的方式工作
194-
Out<uint8_t>(kMasterData, kIcw48086);
192+
Out<uint8_t>(kMasterDataPort, kIcw48086);
195193

196-
Out<uint8_t>(kSlaveCommand, kIcw1Init | kIcw1Icw4);
194+
Out<uint8_t>(kSlaveCommandPort, kIcw1Init | kIcw1Icw4);
197195
// 设置从片 IRQ 从 offset2_ 号中断开始
198-
Out<uint8_t>(kPic2Data, offset2_);
196+
Out<uint8_t>(kPic2DataPort, offset2_);
199197
// 告诉从片输出引脚和主片 IR2 号相连
200198
// 2: 0000 0010
201-
Out<uint8_t>(kPic2Data, 2);
199+
Out<uint8_t>(kPic2DataPort, 2);
202200
// 设置从片按照 8086 的方式工作
203-
Out<uint8_t>(kPic2Data, kIcw48086);
201+
Out<uint8_t>(kPic2DataPort, kIcw48086);
204202

205203
// 关闭所有中断
206-
Out<uint8_t>(kMasterData, 0xFF);
207-
Out<uint8_t>(kPic2Data, 0xFF);
204+
Out<uint8_t>(kMasterDataPort, 0xFF);
205+
Out<uint8_t>(kPic2DataPort, 0xFF);
208206
}
209207

210208
/// @name 构造/析构函数
@@ -221,69 +219,69 @@ class Pic {
221219
* 开启 pic 的 no 中断
222220
* @param no 中断号
223221
*/
224-
void Enable(uint8_t no) {
222+
void Enable(uint8_t no) const {
225223
uint8_t mask = 0;
226224
if (no >= offset2_) {
227-
mask = ((In<uint8_t>(kPic2Data)) & (~(1 << (no % 8))));
228-
Out<uint8_t>(kPic2Data, mask);
225+
mask = ((In<uint8_t>(kPic2DataPort)) & (~(1 << (no % 8))));
226+
Out<uint8_t>(kPic2DataPort, mask);
229227
} else {
230-
mask = ((In<uint8_t>(kMasterData)) & (~(1 << (no % 8))));
231-
Out<uint8_t>(kMasterData, mask);
228+
mask = ((In<uint8_t>(kMasterDataPort)) & (~(1 << (no % 8))));
229+
Out<uint8_t>(kMasterDataPort, mask);
232230
}
233231
}
234232

235233
/**
236234
* 关闭 8259A 芯片的所有中断
237235
*/
238-
void Disable() {
236+
static void Disable() {
239237
// 屏蔽所有中断
240-
Out<uint8_t>(kMasterData, 0xFF);
241-
Out<uint8_t>(kPic2Data, 0xFF);
238+
Out<uint8_t>(kMasterDataPort, 0xFF);
239+
Out<uint8_t>(kPic2DataPort, 0xFF);
242240
}
243241

244242
/**
245243
* 关闭 pic 的 no 中断
246244
* @param no 中断号
247245
*/
248-
void Disable(uint8_t no) {
246+
void Disable(uint8_t no) const {
249247
uint8_t mask = 0;
250248
if (no >= offset2_) {
251-
mask = ((In<uint8_t>(kPic2Data)) | (1 << (no % 8)));
252-
Out<uint8_t>(kPic2Data, mask);
249+
mask = ((In<uint8_t>(kPic2DataPort)) | (1 << (no % 8)));
250+
Out<uint8_t>(kPic2DataPort, mask);
253251
} else {
254-
mask = ((In<uint8_t>(kMasterData)) | (1 << (no % 8)));
255-
Out<uint8_t>(kMasterData, mask);
252+
mask = ((In<uint8_t>(kMasterDataPort)) | (1 << (no % 8)));
253+
Out<uint8_t>(kMasterDataPort, mask);
256254
}
257255
}
258256

259257
/**
260258
* 通知 pic no 中断处理完毕
261259
* @param no 中断号
262260
*/
263-
void Clear(uint8_t no) {
261+
void Clear(uint8_t no) const {
264262
// 按照我们的设置,从 offset1_ 号中断起为用户自定义中断
265263
// 因为单片的 Intel 8259A 芯片只能处理 8 级中断
266264
// 故大于等于 offset2_ 的中断号是由从片处理的
267265
if (no >= offset2_) {
268266
// 发送重设信号给从片
269-
Out<uint8_t>(kSlaveCommand, kEoi);
267+
Out<uint8_t>(kSlaveCommandPort, kEoi);
270268
} else {
271269
// 发送重设信号给主片
272-
Out<uint8_t>(kMasterCommand, kEoi);
270+
Out<uint8_t>(kMasterCommandPort, kEoi);
273271
}
274272
}
275273

276274
/**
277275
* Returns the combined value of the cascaded PICs irq request register
278276
* @return uint16_t 值
279277
*/
280-
uint16_t GetIrr() { return GetIrqReg(kOcw3ReadIrr); }
278+
static auto GetIrr() -> uint16_t { return GetIrqReg(kOcw3ReadIrr); }
281279

282280
/**
283281
* Returns the combined value of the cascaded PICs in-service register
284282
* @return uint16_t 值
285283
*/
286-
uint16_t GetIsr() { return GetIrqReg(kOcw3ReadIsr); }
284+
static auto GetIsr() -> uint16_t { return GetIrqReg(kOcw3ReadIsr); }
287285

288286
private:
289287
uint8_t offset1_;
@@ -293,10 +291,10 @@ class Pic {
293291
static constexpr const uint8_t kMaster = 0x20;
294292
/// Slave (IRQs 8-15)
295293
static constexpr const uint8_t kSlave = 0xA0;
296-
static constexpr const uint8_t kMasterCommand = kMaster;
297-
static constexpr const uint8_t kMasterData = kMaster + 1;
298-
static constexpr const uint8_t kSlaveCommand = kSlave;
299-
static constexpr const uint8_t kPic2Data = kSlave + 1;
294+
static constexpr const uint8_t kMasterCommandPort = kMaster;
295+
static constexpr const uint8_t kMasterDataPort = kMaster + 1;
296+
static constexpr const uint8_t kSlaveCommandPort = kSlave;
297+
static constexpr const uint8_t kPic2DataPort = kSlave + 1;
300298
/// End-of-interrupt command code
301299
static constexpr const uint8_t kEoi = 0x20;
302300

@@ -334,10 +332,11 @@ class Pic {
334332
* @param ocw3 OCW3
335333
* @return uint16_t 值
336334
*/
337-
uint16_t GetIrqReg(uint8_t ocw3) {
338-
Out<uint8_t>(kMasterCommand, ocw3);
339-
Out<uint8_t>(kSlaveCommand, ocw3);
340-
return (In<uint8_t>(kSlaveCommand) << 8) | In<uint8_t>(kMasterCommand);
335+
static auto GetIrqReg(uint8_t ocw3) -> uint16_t {
336+
Out<uint8_t>(kMasterCommandPort, ocw3);
337+
Out<uint8_t>(kSlaveCommandPort, ocw3);
338+
return (In<uint8_t>(kSlaveCommandPort) << 8) |
339+
In<uint8_t>(kMasterCommandPort);
341340
}
342341
};
343342

@@ -356,8 +355,9 @@ class Pit {
356355
uint16_t divisor = kMaxFrequency / frequency;
357356

358357
// 设置 8253/8254 芯片工作在模式 3 下
359-
Out<uint8_t>(kCommand, (uint8_t)kChannel0 | (uint8_t)kHighAndLow |
360-
(uint8_t)kSquareWaveGenerator);
358+
Out<uint8_t>(kCommand, static_cast<uint8_t>(kChannel0) |
359+
static_cast<uint8_t>(kHighAndLow) |
360+
static_cast<uint8_t>(kSquareWaveGenerator));
361361

362362
// 分别写入低字节和高字节
363363
Out<uint8_t>(kChannel0Data, divisor & 0xFF);
@@ -383,7 +383,7 @@ class Pit {
383383
* 获取时钟中断次数
384384
* @return size_t 时钟中断次数
385385
*/
386-
size_t GetTicks() const { return ticks_; }
386+
[[nodiscard]] auto GetTicks() const -> size_t { return ticks_; }
387387

388388
private:
389389
/// 最大频率

include/x86_64/regs.hpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -752,12 +752,10 @@ class ReadOnlyRegBase {
752752
__asm__ volatile("sgdt %0" : "=m"(value) : :);
753753
} else if constexpr (std::is_same_v<RegInfo, register_info::LdtrInfo>) {
754754
static_assert(sizeof(RegInfo) == 0);
755-
756755
} else if constexpr (std::is_same_v<RegInfo, register_info::IdtrInfo>) {
757756
__asm__ volatile("sidt %0" : "=m"(value) : :);
758757
} else if constexpr (std::is_same_v<RegInfo, register_info::TrInfo>) {
759758
static_assert(sizeof(RegInfo) == 0);
760-
761759
} else if constexpr (std::is_same_v<RegInfo, register_info::cr::Cr0Info>) {
762760
__asm__ volatile("mov %%cr0, %0" : "=r"(value) : :);
763761
} else if constexpr (std::is_same_v<RegInfo, register_info::cr::Cr2Info>) {
@@ -772,7 +770,6 @@ class ReadOnlyRegBase {
772770
__asm__ volatile("mov %%rbp, %0" : "=r"(value) : :);
773771
} else if constexpr (std::is_same_v<RegInfo, register_info::Xcr0Info>) {
774772
static_assert(sizeof(RegInfo) == 0);
775-
776773
} else if constexpr (std::is_same_v<
777774
RegInfo,
778775
register_info::segment_register::CsInfo>) {
@@ -847,12 +844,10 @@ class WriteOnlyRegBase {
847844
__asm__ volatile("lgdt %0" : : "m"(value) :);
848845
} else if constexpr (std::is_same_v<RegInfo, register_info::LdtrInfo>) {
849846
static_assert(sizeof(RegInfo) == 0);
850-
851847
} else if constexpr (std::is_same_v<RegInfo, register_info::IdtrInfo>) {
852848
__asm__ volatile("lidt %0" : : "m"(value) :);
853849
} else if constexpr (std::is_same_v<RegInfo, register_info::TrInfo>) {
854850
static_assert(sizeof(RegInfo) == 0);
855-
856851
} else if constexpr (std::is_same_v<RegInfo, register_info::cr::Cr0Info>) {
857852
__asm__ volatile("mov %0, %%cr0" : : "r"(value) :);
858853
} else if constexpr (std::is_same_v<RegInfo, register_info::cr::Cr2Info>) {
@@ -865,7 +860,6 @@ class WriteOnlyRegBase {
865860
__asm__ volatile("mov %0, %%cr8" : : "r"(value) :);
866861
} else if constexpr (std::is_same_v<RegInfo, register_info::Xcr0Info>) {
867862
static_assert(sizeof(RegInfo) == 0);
868-
869863
} else if constexpr (std::is_same_v<
870864
RegInfo,
871865
register_info::segment_register::CsInfo>) {
@@ -932,16 +926,12 @@ class WriteOnlyRegBase {
932926
}
933927
} else if constexpr (std::is_same_v<RegInfo, register_info::GdtrInfo>) {
934928
static_assert(sizeof(RegInfo) == 0);
935-
936929
} else if constexpr (std::is_same_v<RegInfo, register_info::LdtrInfo>) {
937930
static_assert(sizeof(RegInfo) == 0);
938-
939931
} else if constexpr (std::is_same_v<RegInfo, register_info::IdtrInfo>) {
940932
static_assert(sizeof(RegInfo) == 0);
941-
942933
} else if constexpr (std::is_same_v<RegInfo, register_info::TrInfo>) {
943934
static_assert(sizeof(RegInfo) == 0);
944-
945935
} else if constexpr (std::is_same_v<RegInfo, register_info::cr::Cr0Info>) {
946936
__asm__ volatile("bts %%cr0, %0" : : "r"(offset) :);
947937
} else if constexpr (std::is_same_v<RegInfo, register_info::cr::Cr2Info>) {
@@ -954,7 +944,6 @@ class WriteOnlyRegBase {
954944
__asm__ volatile("bts %%cr8, %0" : : "r"(offset) :);
955945
} else if constexpr (std::is_same_v<RegInfo, register_info::Xcr0Info>) {
956946
static_assert(sizeof(RegInfo) == 0);
957-
958947
} else {
959948
static_assert(sizeof(RegInfo) == 0);
960949

@@ -987,16 +976,12 @@ class WriteOnlyRegBase {
987976
}
988977
} else if constexpr (std::is_same_v<RegInfo, register_info::GdtrInfo>) {
989978
static_assert(sizeof(RegInfo) == 0);
990-
991979
} else if constexpr (std::is_same_v<RegInfo, register_info::LdtrInfo>) {
992980
static_assert(sizeof(RegInfo) == 0);
993-
994981
} else if constexpr (std::is_same_v<RegInfo, register_info::IdtrInfo>) {
995982
static_assert(sizeof(RegInfo) == 0);
996-
997983
} else if constexpr (std::is_same_v<RegInfo, register_info::TrInfo>) {
998984
static_assert(sizeof(RegInfo) == 0);
999-
1000985
} else if constexpr (std::is_same_v<RegInfo, register_info::cr::Cr0Info>) {
1001986
__asm__ volatile("btr %%cr0, %0" : : "r"(offset) :);
1002987
} else if constexpr (std::is_same_v<RegInfo, register_info::cr::Cr2Info>) {
@@ -1009,7 +994,6 @@ class WriteOnlyRegBase {
1009994
__asm__ volatile("btr %%cr8, %0" : : "r"(offset) :);
1010995
} else if constexpr (std::is_same_v<RegInfo, register_info::Xcr0Info>) {
1011996
static_assert(sizeof(RegInfo) == 0);
1012-
1013997
} else {
1014998
static_assert(sizeof(RegInfo) == 0);
1015999

0 commit comments

Comments
 (0)