openMSX
V9990PixelRenderer.cc
Go to the documentation of this file.
1 #include "V9990PixelRenderer.hh"
2 #include "V9990.hh"
3 #include "V9990VRAM.hh"
4 #include "V9990DisplayTiming.hh"
5 #include "V9990Rasterizer.hh"
6 #include "PostProcessor.hh"
7 #include "Display.hh"
8 #include "VideoSystem.hh"
9 #include "VideoSourceSetting.hh"
10 #include "FinishFrameEvent.hh"
11 #include "RealTime.hh"
12 #include "Timer.hh"
13 #include "EventDistributor.hh"
14 #include "MSXMotherBoard.hh"
15 #include "Reactor.hh"
16 #include "RenderSettings.hh"
17 #include "IntegerSetting.hh"
18 #include "one_of.hh"
19 #include "unreachable.hh"
20 #include <cassert>
21 
22 namespace openmsx {
23 
25  : vdp(vdp_)
26  , eventDistributor(vdp.getReactor().getEventDistributor())
27  , realTime(vdp.getMotherBoard().getRealTime())
28  , renderSettings(vdp.getReactor().getDisplay().getRenderSettings())
29  , videoSourceSetting(vdp.getMotherBoard().getVideoSource())
30  , rasterizer(vdp.getReactor().getDisplay().
31  getVideoSystem().createV9990Rasterizer(vdp))
32 {
33  frameSkipCounter = 999; // force drawing of frame;
34  finishFrameDuration = 0;
35  drawFrame = false; // don't draw before frameStart is called
36  prevDrawFrame = false;
37 
39 
40  renderSettings.getMaxFrameSkipSetting().attach(*this);
41  renderSettings.getMinFrameSkipSetting().attach(*this);
42 }
43 
45 {
46  renderSettings.getMaxFrameSkipSetting().detach(*this);
47  renderSettings.getMinFrameSkipSetting().detach(*this);
48 }
49 
51 {
52  return rasterizer->getPostProcessor();
53 }
54 
55 void V9990PixelRenderer::reset(EmuTime::param time)
56 {
57  displayEnabled = vdp.isDisplayEnabled();
58  setDisplayMode(vdp.getDisplayMode(), time);
59  setColorMode(vdp.getColorMode(), time);
60 
61  rasterizer->reset();
62 }
63 
64 void V9990PixelRenderer::frameStart(EmuTime::param time)
65 {
66  if (!rasterizer->isActive()) {
67  frameSkipCounter = 999;
68  drawFrame = false;
69  prevDrawFrame = false;
70  return;
71  }
72  prevDrawFrame = drawFrame;
73  if (vdp.isInterlaced() && renderSettings.getDeinterlace() &&
74  vdp.getEvenOdd() && vdp.isEvenOddEnabled()) {
75  // deinterlaced odd frame, do same as even frame
76  } else {
77  if (frameSkipCounter < renderSettings.getMinFrameSkip()) {
78  ++frameSkipCounter;
79  drawFrame = false;
80  } else if (frameSkipCounter >= renderSettings.getMaxFrameSkip()) {
81  frameSkipCounter = 0;
82  drawFrame = true;
83  } else {
84  ++frameSkipCounter;
85  if (rasterizer->isRecording()) {
86  drawFrame = true;
87  } else {
88  drawFrame = realTime.timeLeft(
89  unsigned(finishFrameDuration), time);
90  }
91  if (drawFrame) {
92  frameSkipCounter = 0;
93  }
94  }
95  }
96  if (!drawFrame) return;
97 
98  accuracy = renderSettings.getAccuracy();
99  lastX = 0;
100  lastY = 0;
101  verticalOffsetA = verticalOffsetB = vdp.getTopBorder();
102 
103  // Make sure that the correct timing is used
104  setDisplayMode(vdp.getDisplayMode(), time);
105  rasterizer->frameStart();
106 }
107 
108 void V9990PixelRenderer::frameEnd(EmuTime::param time)
109 {
110  bool skipEvent = !drawFrame;
111  if (drawFrame) {
112  // Render last changes in this frame before starting a new frame
113  sync(time, true);
114 
115  auto time1 = Timer::getTime();
116  rasterizer->frameEnd(time);
117  auto time2 = Timer::getTime();
118  auto current = time2 - time1;
119  const float ALPHA = 0.2f;
120  finishFrameDuration = finishFrameDuration * (1 - ALPHA) +
121  current * ALPHA;
122 
123  if (vdp.isInterlaced() && vdp.isEvenOddEnabled() &&
124  renderSettings.getDeinterlace() &&
125  !prevDrawFrame) {
126  // dont send event in deinterlace mode when
127  // previous frame was not rendered
128  skipEvent = true;
129  }
130 
131  }
132  if (vdp.getMotherBoard().isActive() &&
133  !vdp.getMotherBoard().isFastForwarding()) {
134  eventDistributor.distributeEvent(
135  std::make_shared<FinishFrameEvent>(
136  rasterizer->getPostProcessor()->getVideoSource(),
137  videoSourceSetting.getSource(),
138  skipEvent));
139  }
140 }
141 
142 void V9990PixelRenderer::sync(EmuTime::param time, bool force)
143 {
144  if (!drawFrame) return;
145 
146  if (accuracy != RenderSettings::ACC_SCREEN || force) {
147  vdp.getVRAM().sync(time);
148  renderUntil(time);
149  }
150 }
151 
152 void V9990PixelRenderer::renderUntil(EmuTime::param time)
153 {
154  // Translate time to pixel position
155  int limitTicks = vdp.getUCTicksThisFrame(time);
156  assert(limitTicks <=
158  int toX, toY;
159  switch (accuracy) {
161  toX = limitTicks % V9990DisplayTiming::UC_TICKS_PER_LINE;
162  toY = limitTicks / V9990DisplayTiming::UC_TICKS_PER_LINE;
163  break;
166  // TODO figure out rounding point
167  toX = 0;
168  toY = (limitTicks + V9990DisplayTiming::UC_TICKS_PER_LINE - 400) /
170  break;
171  default:
172  UNREACHABLE;
173  toX = toY = 0; // avoid warning
174  }
175 
176  if ((toX == lastX) && (toY == lastY)) return;
177 
178  // edges of the DISPLAY part of the vdp output
179  int left = vdp.getLeftBorder();
180  int right = vdp.getRightBorder();
182 
183  if (displayEnabled) {
184  // Left border
185  subdivide(lastX, lastY, toX, toY, 0, left, DRAW_BORDER);
186  // Display area
187  // It's possible this draws a few pixels too many (this
188  // allowed to simplify the implementation of the Bx modes).
189  // So it's important to draw from left to right (right border
190  // must come _after_ display area).
191  subdivide(lastX, lastY, toX, toY, left, right, DRAW_DISPLAY);
192  // Right border
193  subdivide(lastX, lastY, toX, toY, right, rightEdge, DRAW_BORDER);
194  } else {
195  // complete screen
196  subdivide(lastX, lastY, toX, toY, 0, rightEdge, DRAW_BORDER);
197  }
198 
199  lastX = toX;
200  lastY = toY;
201 }
202 
203 void V9990PixelRenderer::subdivide(int fromX, int fromY, int toX, int toY,
204  int clipL, int clipR, DrawType drawType)
205 {
206  // partial first line
207  if (fromX > clipL) {
208  if (fromX < clipR) {
209  bool atEnd = (fromY != toY) || (toX >= clipR);
210  draw(fromX, fromY, (atEnd ? clipR : toX), fromY + 1,
211  drawType);
212  }
213  if (fromY == toY) return;
214  fromY++;
215  }
216 
217  bool drawLast = false;
218  if (toX >= clipR) {
219  toY++;
220  } else if (toX > clipL) {
221  drawLast = true;
222  }
223  // full middle lines
224  if (fromY < toY) {
225  draw(clipL, fromY, clipR, toY, drawType);
226  }
227 
228  // partial last line
229  if (drawLast) draw(clipL, toY, toX, toY + 1, drawType);
230 }
231 
232 void V9990PixelRenderer::draw(int fromX, int fromY, int toX, int toY,
233  DrawType type)
234 {
235  if (type == DRAW_BORDER) {
236  rasterizer->drawBorder(fromX, fromY, toX, toY);
237 
238  } else {
239  assert(type == DRAW_DISPLAY);
240 
241  int displayX = fromX - vdp.getLeftBorder();
242  int displayY = fromY - vdp.getTopBorder();
243  int displayYA = fromY - verticalOffsetA;
244  int displayYB = fromY - verticalOffsetB;
245 
246  rasterizer->drawDisplay(fromX, fromY, toX, toY,
247  displayX,
248  displayY, displayYA, displayYB);
249  }
250 }
251 
252 void V9990PixelRenderer::updateDisplayEnabled(bool enabled, EmuTime::param time)
253 {
254  sync(time, true);
255  displayEnabled = enabled;
256 }
257 
259 {
260  sync(time);
261  rasterizer->setDisplayMode(mode);
262 }
263 
264 void V9990PixelRenderer::updatePalette(int index, byte r, byte g, byte b, bool ys,
265  EmuTime::param time)
266 {
267  if (displayEnabled) {
268  sync(time);
269  } else {
270  // TODO only sync if border color changed
271  sync(time);
272  }
273  rasterizer->setPalette(index, r, g, b, ys);
274 }
275 void V9990PixelRenderer::updateSuperimposing(bool enabled, EmuTime::param time)
276 {
277  sync(time);
278  rasterizer->setSuperimpose(enabled);
279 }
280 void V9990PixelRenderer::setColorMode(V9990ColorMode mode, EmuTime::param time)
281 {
282  sync(time);
283  rasterizer->setColorMode(mode);
284 }
285 
286 void V9990PixelRenderer::updateBackgroundColor(int /*index*/, EmuTime::param time)
287 {
288  sync(time);
289 }
290 
291 void V9990PixelRenderer::updateScrollAX(EmuTime::param time)
292 {
293  if (displayEnabled) sync(time);
294 }
295 void V9990PixelRenderer::updateScrollBX(EmuTime::param time)
296 {
297  // TODO only in P1 mode
298  if (displayEnabled) sync(time);
299 }
300 void V9990PixelRenderer::updateScrollAYLow(EmuTime::param time)
301 {
302  if (displayEnabled) {
303  sync(time);
304  // happens in all display modes (verified)
305  // TODO high byte still seems to be wrong .. need to investigate
306  verticalOffsetA = lastY;
307  }
308 }
309 void V9990PixelRenderer::updateScrollBYLow(EmuTime::param time)
310 {
311  // TODO only in P1 mode
312  if (displayEnabled) {
313  sync(time);
314  // happens in all display modes (verified)
315  // TODO high byte still seems to be wrong .. need to investigate
316  verticalOffsetB = lastY;
317  }
318 }
319 
320 void V9990PixelRenderer::update(const Setting& setting)
321 {
322  assert(&setting == one_of(&renderSettings.getMinFrameSkipSetting(),
323  &renderSettings.getMaxFrameSkipSetting()));
324  (void)setting;
325  // Force drawing of frame
326  frameSkipCounter = 999;
327 }
328 
329 } // namespace openmsx
openmsx::V9990::isPalTiming
bool isPalTiming() const
Is PAL timing active? This setting is fixed at start of frame.
Definition: V9990.hh:126
one_of.hh
VideoSystem.hh
IntegerSetting.hh
openmsx::V9990PixelRenderer::getPostProcessor
PostProcessor * getPostProcessor() const override
See V9990::getPostProcessor.
Definition: V9990PixelRenderer.cc:50
openmsx::MSXMotherBoard::getCurrentTime
EmuTime::param getCurrentTime()
Convenience method: This is the same as getScheduler().getCurrentTime().
Definition: MSXMotherBoard.cc:503
openmsx::Subject::detach
void detach(Observer< T > &observer)
Definition: Subject.hh:56
openmsx::V9990::getTopBorder
int getTopBorder() const
Definition: V9990.hh:334
Timer.hh
Display.hh
openmsx::V9990PixelRenderer::~V9990PixelRenderer
~V9990PixelRenderer() override
Definition: V9990PixelRenderer.cc:44
openmsx::RenderSettings::ACC_LINE
@ ACC_LINE
Definition: RenderSettings.hh:32
openmsx::V9990PixelRenderer::setDisplayMode
void setDisplayMode(V9990DisplayMode mode, EmuTime::param time) override
Set screen mode.
Definition: V9990PixelRenderer.cc:258
openmsx::V9990PixelRenderer::updateBackgroundColor
void updateBackgroundColor(int index, EmuTime::param time) override
Set background color.
Definition: V9990PixelRenderer.cc:286
openmsx::RenderSettings::getMaxFrameSkipSetting
IntegerSetting & getMaxFrameSkipSetting()
The current max frameskip.
Definition: RenderSettings.hh:59
openmsx::V9990DisplayTiming::UC_TICKS_PER_LINE
static constexpr int UC_TICKS_PER_LINE
The number of clockticks per line is independent of the crystal used or the display mode (NTSC/PAL)
Definition: V9990DisplayTiming.hh:36
openmsx::V9990::getRightBorder
int getRightBorder() const
Get the number of VDP clockticks between the start of the line and the end of the right border.
Definition: V9990.hh:324
openmsx::V9990ColorMode
V9990ColorMode
Definition: V9990ModeEnum.hh:11
VideoSourceSetting.hh
openmsx::Timer::getTime
uint64_t getTime()
Get current (real) time in us.
Definition: Timer.cc:7
openmsx::Subject::attach
void attach(Observer< T > &observer)
Definition: Subject.hh:50
openmsx::Setting
Definition: Setting.hh:120
openmsx::V9990PixelRenderer::updateScrollBX
void updateScrollBX(EmuTime::param time) override
Definition: V9990PixelRenderer.cc:295
openmsx::V9990::isInterlaced
bool isInterlaced() const
Get interlace status.
Definition: V9990.hh:58
openmsx::V9990PixelRenderer::frameStart
void frameStart(EmuTime::param time) override
Signal the start of a new frame.
Definition: V9990PixelRenderer.cc:64
V9990VRAM.hh
openmsx::MSXMotherBoard::isFastForwarding
bool isFastForwarding() const
Definition: MSXMotherBoard.hh:97
openmsx::RenderSettings::getDeinterlace
bool getDeinterlace() const
Deinterlacing [on, off].
Definition: RenderSettings.hh:53
RealTime.hh
Reactor.hh
openmsx::V9990PixelRenderer::updatePalette
void updatePalette(int index, byte r, byte g, byte b, bool ys, EmuTime::param time) override
Set a palette entry.
Definition: V9990PixelRenderer.cc:264
UNREACHABLE
#define UNREACHABLE
Definition: unreachable.hh:38
openmsx::V9990PixelRenderer::V9990PixelRenderer
V9990PixelRenderer(V9990 &vdp)
Definition: V9990PixelRenderer.cc:24
V9990.hh
openmsx::RenderSettings::getMinFrameSkipSetting
IntegerSetting & getMinFrameSkipSetting()
The current min frameskip.
Definition: RenderSettings.hh:63
openmsx::V9990::getDisplayMode
V9990DisplayMode getDisplayMode() const
Return the current display mode.
Definition: V9990.hh:191
openmsx::V9990PixelRenderer::updateDisplayEnabled
void updateDisplayEnabled(bool enabled, EmuTime::param time) override
Informs the renderer of a VDP display enabled change.
Definition: V9990PixelRenderer.cc:252
one_of
Definition: one_of.hh:7
openmsx::V9990::getVRAM
V9990VRAM & getVRAM()
Obtain a reference to the V9990's VRAM.
Definition: V9990.hh:51
EventDistributor.hh
V9990Rasterizer.hh
PostProcessor.hh
openmsx::RenderSettings::getAccuracy
Accuracy getAccuracy() const
Accuracy [screen, line, pixel].
Definition: RenderSettings.hh:50
openmsx::MSXDevice::getMotherBoard
MSXMotherBoard & getMotherBoard() const
Get the mother board this device belongs to.
Definition: MSXDevice.cc:75
openmsx::V9990DisplayMode
V9990DisplayMode
Definition: V9990ModeEnum.hh:6
openmsx::VideoSourceSetting::getSource
int getSource() noexcept
Definition: VideoSourceSetting.cc:35
openmsx::PostProcessor
Abstract base class for post processors.
Definition: PostProcessor.hh:30
RenderSettings.hh
V9990PixelRenderer.hh
FinishFrameEvent.hh
openmsx::V9990PixelRenderer::updateSuperimposing
void updateSuperimposing(bool enabled, EmuTime::param time) override
Change superimpose status.
Definition: V9990PixelRenderer.cc:275
g
int g
Definition: ScopedAssign_test.cc:20
openmsx::V9990VRAM::sync
void sync(EmuTime::param time)
Update VRAM state to specified moment in time.
Definition: V9990VRAM.hh:33
openmsx::V9990PixelRenderer::frameEnd
void frameEnd(EmuTime::param time) override
Signal the end of the current frame.
Definition: V9990PixelRenderer.cc:108
openmsx::V9990::getEvenOdd
bool getEvenOdd() const
Is the even or odd field being displayed?
Definition: V9990.hh:72
openmsx::RenderSettings::getMinFrameSkip
int getMinFrameSkip() const
Definition: RenderSettings.hh:64
openmsx::RenderSettings::ACC_SCREEN
@ ACC_SCREEN
Definition: RenderSettings.hh:32
openmsx::MSXMotherBoard::isActive
bool isActive() const
Definition: MSXMotherBoard.hh:96
openmsx::RenderSettings::getMaxFrameSkip
int getMaxFrameSkip() const
Definition: RenderSettings.hh:60
openmsx::V9990::getUCTicksThisFrame
int getUCTicksThisFrame(EmuTime::param time) const
Get the number of elapsed UC ticks in this frame.
Definition: V9990.hh:118
V9990DisplayTiming.hh
openmsx::V9990PixelRenderer::updateScrollAYLow
void updateScrollAYLow(EmuTime::param time) override
Definition: V9990PixelRenderer.cc:300
openmsx::V9990PixelRenderer::setColorMode
void setColorMode(V9990ColorMode mode, EmuTime::param time) override
Set color mode.
Definition: V9990PixelRenderer.cc:280
openmsx::V9990::isDisplayEnabled
bool isDisplayEnabled() const
Is the display enabled? Note this is simpler than the V99x8 version.
Definition: V9990.hh:81
openmsx::V9990PixelRenderer::updateScrollBYLow
void updateScrollBYLow(EmuTime::param time) override
Definition: V9990PixelRenderer.cc:309
openmsx::V9990PixelRenderer::updateScrollAX
void updateScrollAX(EmuTime::param time) override
Set scroll register.
Definition: V9990PixelRenderer.cc:291
openmsx::V9990PixelRenderer::reset
void reset(EmuTime::param time) override
Re-initialise the V9990Renderer's state.
Definition: V9990PixelRenderer.cc:55
unreachable.hh
openmsx::V9990::getColorMode
V9990ColorMode getColorMode() const
Return the current color mode.
Definition: V9990.cc:812
openmsx::RealTime::timeLeft
bool timeLeft(uint64_t us, EmuTime::param time)
Check that there is enough real time left before we reach as certain point in emulated time.
Definition: RealTime.cc:67
openmsx::V9990::isEvenOddEnabled
bool isEvenOddEnabled() const
Get even/odd page alternation status.
Definition: V9990.hh:65
openmsx
This file implemented 3 utility functions:
Definition: Autofire.cc:5
MSXMotherBoard.hh
openmsx::V9990::getLeftBorder
int getLeftBorder() const
Get the number of VDP clockticks between the start of the line and the end of the left border.
Definition: V9990.hh:317
openmsx::V9990DisplayTiming::getUCTicksPerFrame
static constexpr int getUCTicksPerFrame(bool palTiming)
Get the number of UC ticks in 1 frame.
Definition: V9990DisplayTiming.hh:75
openmsx::EventDistributor::distributeEvent
void distributeEvent(const EventPtr &event)
Schedule the given event for delivery.
Definition: EventDistributor.cc:44
openmsx::RenderSettings::ACC_PIXEL
@ ACC_PIXEL
Definition: RenderSettings.hh:32
openmsx::V9990
Implementation of the Yamaha V9990 VDP as used in the GFX9000 cartridge by Sunrise.
Definition: V9990.hh:31