-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.cpp
More file actions
156 lines (131 loc) · 4.06 KB
/
app.cpp
File metadata and controls
156 lines (131 loc) · 4.06 KB
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
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <limits>
#include <memory>
#include "app_kefrens_bars.hpp"
#include "app_player_adapter.hpp"
#include "kb_tinymod.hpp"
#include "ost.hpp"
#include "pc_kbd.hpp"
#include "pc_pit.hpp"
#include "sb16.hpp"
#include "sb_detect.hpp"
#include "vga_mode.hpp"
#include "vga_pageflip.hpp"
#include "vga_reg.hpp"
#include "vga_irq.hpp"
using std::uint8_t;
using std::int16_t;
using std::uint16_t;
namespace rqdq {
namespace app {
const int kAudioBufferSizeInSamples = 128;
const int kAudioSampleRateInHz = 22050;
const int kAudioWidthInChannels = 1;
int kSoundBlasterIOBaseAddr = 0x220;
int kSoundBlasterIRQNum = 7; // 760eld == 5
int kSoundBlasterDMAChannelNum = 5; // 760eld == 1
const int kNumDrawTimeSamples = 500;
class Demo {
public:
Demo()
:quitSoon_(false),
mCnt_(0),
paulaPtr_(new kb::Paula()),
playerPtr_(new kb::ModPlayer(paulaPtr_.get(), (uint8_t*)ostData)) {}
void Run() {
pc::Keyboard kbd;
vga::ModeSetter modeSetter;
modeSetter.Set(vga::VM_MODEX);
vga::RetraceIRQ<vga::FlipPages> flipPagesIRQ;
measuredRefreshRateInHz_ = flipPagesIRQ.GetHz();
snd::Blaster blaster(kSoundBlasterIOBaseAddr,
kSoundBlasterIRQNum,
kSoundBlasterDMAChannelNum,
kAudioSampleRateInHz,
kAudioWidthInChannels,
kAudioBufferSizeInSamples);
adapterPtr_.reset(new PlayerAdapter(*playerPtr_));
adapterPtr_->Refill();
blaster.AttachProc(PlayerAdapter::BlasterJmp, adapterPtr_.get());
quitSoon_ = false;
char msgs[16];
int msgCnt;
const int MSG_KBD_DATA_AVAILABLE = 1;
const int MSG_VGA_PAGE_LOCKED = 2;
while (!quitSoon_) {
{
pc::CriticalSection section;
msgCnt = 0;
if (kbd.IsDataAvailable()) {
msgs[msgCnt++] = MSG_KBD_DATA_AVAILABLE; }
if (vga::backLocked) {
msgs[msgCnt++] = MSG_VGA_PAGE_LOCKED; }
if (msgCnt == 0) {
pc::Sleep();
continue; }}
for (int mi=0; mi<msgCnt; mi++) {
char& msg = msgs[mi];
if (msg == MSG_KBD_DATA_AVAILABLE) {
pc::Event ke = kbd.GetMessage();
if (ke.down) {
OnKeyDown(ke.scanCode); }}
else if (msg == MSG_VGA_PAGE_LOCKED) {
vga::AnimationPage animationPage;
if (animationPage.IsLocked()) {
Draw(animationPage.Get()); }}}}}
private:
void Draw(const vga::VRAMPage& vram) {
float T = vga::GetTime() / measuredRefreshRateInHz_;
int patternNum = playerPtr_->GetCurrentPos();
int rowNum = playerPtr_->GetCurrentRow();
#ifdef SHOW_TIMING
vga::SetRGB(0, 0x30, 0x30, 0x30);
#endif
pc::Stopwatch drawtime;
DrawKefrensBars(vram, T, patternNum, rowNum);
if (mCnt_ < kNumDrawTimeSamples) {
float m = drawtime.GetElapsedTimeInSeconds();
if (m > 0) {
mLst_[mCnt_++] = m; }}
#ifdef SHOW_TIMING
vga::SetRGB(0, 0,0,0);
#endif
adapterPtr_->Refill(); }
void OnKeyDown(int scanCode) {
if (scanCode == pc::SC_ESC) {
quitSoon_ = true; }}
private:
bool quitSoon_;
std::auto_ptr<kb::Paula> paulaPtr_;
std::auto_ptr<kb::ModPlayer> playerPtr_;
std::auto_ptr<PlayerAdapter> adapterPtr_;
public:
float measuredRefreshRateInHz_;
float mLst_[kNumDrawTimeSamples];
int mCnt_; };
} // namespace app
} // namespace rqdq
int main() {
rqdq::hw::BlasterDetectResult bd = rqdq::hw::DetectBlaster();
if (!bd.found) {
std::printf("BLASTER not found\n");
return 1; }
rqdq::app::kSoundBlasterIOBaseAddr = bd.value.ioAddr;
rqdq::app::kSoundBlasterIRQNum = bd.value.irqNum;
rqdq::app::kSoundBlasterDMAChannelNum = bd.value.BestDMA();
std::printf("Found BLASTER addr=0x%x irq=%d dma=%d\n",
rqdq::app::kSoundBlasterIOBaseAddr,
rqdq::app::kSoundBlasterIRQNum,
rqdq::app::kSoundBlasterDMAChannelNum);
rqdq::app::Demo demo;
demo.Run();
float ax = 0;
for (int i=0; i<demo.mCnt_; i++) {
ax += demo.mLst_[i]; }
ax /= demo.mCnt_;
std::printf("measuredRefreshRate: %.2f hz\n", demo.measuredRefreshRateInHz_);
std::printf(" avgDrawTime: %.2f ms\n", (ax*1000));
// std::printf(" spuriousIRQ: %d occurrences\n", rqdq::snd::spuriousIRQCnt);
return 0; }