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