captainys / TOWNSEMU

FM Towns Emulator "Tsugaru"
BSD 3-Clause "New" or "Revised" License
236 stars 17 forks source link

Lemmings 2: The tribes screen tearing issue. #96

Open GIKAS123 opened 12 months ago

GIKAS123 commented 12 months ago

It doesn't make the game unplayable but it is annoying.It only happens on this game of the about 15 games I tested. Im using the latest release of the windows version of the emulator on a windows 10 pc.

captainys commented 12 months ago

Thank you for the report! I looked into the disassembly.

Lemmings 2 looks to be changing VRAM offset in VSYNC interrupt handler to roll the screen up and down. However, whole screen is re-written outside of VSYNC interrupt handler once every four steps.

The overwrite-routine seems to be following after the bright spot of the CRT, so that it is not visible to the user. The routine takes about 3ms at 66MHz. If my theory is correct, the flickering artifact could have been visible in the real 486 and faster models. The screen update needs to be slow enough so that it won't catch up with the CRT's bright spot.

Tsugaru right now renders the screen during the VSYNC period, which makes this screen-update whole visible. One solution can be gradually render the screen line by line at every H-SYNC period, but the cost will be too much, and I don't want to make too much change to the rendering function that has been working stably. Feasible solution can be to render screen at the beginning of the vertical period, immediately before the function overwrites the whole screen. (Or, virtually making the bright spot move top to bottom instantaneously.)

I'll test it later and let you know.

GIKAS123 commented 12 months ago

Thank you for the report! I looked into the disassembly.

Lemmings 2 looks to be changing VRAM offset in VSYNC interrupt handler to roll the screen up and down. However, whole screen is re-written outside of VSYNC interrupt handler once every four steps.

The overwrite-routine seems to be following after the bright spot of the CRT, so that it is not visible to the user. The routine takes about 3ms at 66MHz. If my theory is correct, the flickering artifact could have been visible in the real 486 and faster models. The screen update needs to be slow enough so that it won't catch up with the CRT's bright spot.

Tsugaru right now renders the screen during the VSYNC period, which makes this screen-update whole visible. One solution can be gradually render the screen line by line at every H-SYNC period, but the cost will be too much, and I don't want to make too much change to the rendering function that has been working stably. Feasible solution can be to render screen at the beginning of the vertical period, immediately before the function overwrites the whole screen. (Or, virtually making the bright spot move top to bottom instantaneously.)

I'll test it later and let you know.

OK! Thank you so much for answering and for the really interesting stuff you shared!

captainys commented 12 months ago

I've tested it. As I expected, if I render at the beginning of the vertical period (within 2ms), and if I choose below 12MHz CPU speed, I don't see flicker. If I increase 20MHz, I start seeing flickering. I haven't checked with my real TOWNS, but my guess is you would see flickering if the CPU speed is faster than certain threshold.

What needs to happen is (1) CRTC shoots VSYNC interrupt, (2) VSYNC interrupt handler sets a flag that it updated VRAM offset four times, (3) CRTC starts rendering the next frame, and then (4) main routine detects the flag set and rewrites the screen, in this order. If the CPU is fast, (4) can happen before (3).

Chase HQ had a similar timing sensitivity, but I was able to auto-detect Chase HQ, because it was updating palette certain times during the vertical period. But I cannot come up with a good criteria for auto-detecting Lemmings2. I probably need to make it an option.

I'll add something to support Lemmings2.

captainys commented 12 months ago

OK. I've pushed the source code. Are you able to build by yourself?

I realized I did mouse-integration for Lemmings2 a while ago. If you choose LEMMINGS2 as application-specific setting, the special rendering timing is enabled. Also it forces 1X CD speed and 20MHz CPU speed. I've realized that Lemmings2 was using a very timing-sensitive way of reading CD-ROM, and it very often crashes if the CPU speed is slow compared to the CD-ROM speed. 1X speed CD + 20MHz CPU seems to be stable, and slow enough to prevent the flickering.

GIKAS123 commented 12 months ago

OK. I've pushed the source code. Are you able to build by yourself?

Thank you so much again but unfortunately I don't really know how to do that... If you could provide me with a build or at least a guide on how to do it I'd be grateful!

bcc2528 commented 12 months ago

I have tried it and can confirm that the flicker on the game screen has been disappeared. thank you for fixed. but the command menu at bottom of the screen still seems to be shaky.

GIKAS123 commented 11 months ago

I have tried it and can confirm that the flicker on the game screen has been disappeared. thank you for fixed. but the command menu at bottom of the screen still seems to be shaky.

Could you please provide me the latest build since I'm unable to build it myself so I can test it too?

Many thanks in advance!

captainys commented 11 months ago

I have tried it and can confirm that the flicker on the game screen has been disappeared. thank you for fixed. but the command menu at bottom of the screen still seems to be shaky.

Does the bottom of the screen (icons) stay stable while scrolling in real TOWNS? The bottom icons are re-drawn in the VSYNC interrupt handler, therefore it cannot be behind the CRT bright spot. I guess the icons should be shaky on real TOWNS.

bcc2528 commented 11 months ago

Does the bottom of the screen (icons) stay stable while scrolling in real TOWNS? The bottom icons are re-drawn in the VSYNC interrupt handler, therefore it cannot be behind the CRT bright spot. I guess the icons should be shaky on real TOWNS.

If I remember correctly, the demo version of Lemmings 2 with FM TOWNS 2 MX (on compatibility mode) did not shaky command menu.

bcc2528 commented 11 months ago

Perhaps this should be correct. The command menu is not shaking. https://www.youtube.com/watch?v=yVZmuTAM8dg

GIKAS123 commented 11 months ago

Can confirm that icons aren't shaky in real towns

captainys commented 11 months ago

That's very interesting. I thought icons are drawn in the VSYNC interrupt handler, in which case will be rendered in the next frame. Lemmings2 is racing with the CRT. I need to study more to stabilize entire screen, but in the next release at least map part will be stable.

I am actually also bothered that Lemmings2 will freeze if I choose slow CPU speed. It does the same trick as Fractal Engine does, which uses CD SEEK command to force re-invoking the CD interrupt handler. I initially thought if I slow down only CPU, CD-ROM interrupt comes in too fast to make re-entrance, but it doesn't seem to be the case. Even if I slow down both CPU and CD-ROM, it still freezes (actually program crashes and returning to COMMAND.COM without console output). Psygnosis did you-are-not-supposed-to-do this type of programming. I'll try to fix it, but may give up after spending a few more hours.

At least if you choose 20MHz CPU speed, it seems to be stable.