Annotation of hatari/src/video.c, revision 1.1.1.22

1.1       root        1: /*
1.1.1.5   root        2:   Hatari - video.c
                      3: 
1.1.1.21  root        4:   This file is distributed under the GNU General Public License, version 2
                      5:   or at your option any later version. Read the file gpl.txt for details.
1.1       root        6: 
1.1.1.7   root        7:   Video hardware handling. This code handling all to do with the video chip.
                      8:   So, we handle VBLs, HBLs, copying the ST screen to a buffer to simulate the
                      9:   TV raster trace, border removal, palette changes per HBL, the 'video address
                     10:   pointer' etc...
1.1       root       11: */
1.1.1.11  root       12: 
                     13: /* 2007/03/xx  [NP]    Support for cycle precise border removal / hardware scrolling by using  */
                     14: /*                     Cycles_GetCounterOnWriteAccess (support left/right border and lines with*/
                     15: /*                     length of +26, +2, -2, +44, -106 bytes).                                */
                     16: /*                     Add support for 'Enchanted Lands' second removal of right border.       */
                     17: /*                     More precise support for reading video counter $ff8205/07/09.           */
                     18: /* 2007/04/14  [NP]    Precise reloading of $ff8201/03 into $ff8205/07/09 at line 310 on cycle */
                     19: /*                     RESTART_VIDEO_COUNTER_CYCLE (ULM DSOTS Demo).                           */
1.1.1.21  root       20: /* 2007/04/16  [NP]    Better Video_CalculateAddress. We must subtract a "magic" 12 cycles to  */
1.1.1.11  root       21: /*                     Cycles_GetCounterOnReadAccess(CYCLES_COUNTER_VIDEO) to get a correct    */
1.1.1.21  root       22: /*                     value (No Cooper's video synchro protection is finally OK :) ).         */
1.1.1.11  root       23: /* 2007/04/17  [NP]    - Switch to 60 Hz to remove top border on line 33 should occur before   */
                     24: /*                     LINE_REMOVE_TOP_CYCLE (a few cycles before the HBL)                     */
                     25: /* 2007/04/23  [NP]    - Slight change in Video_StoreResolution to ignore hi res if the line   */
                     26: /*                     has left/right border removed -> assume of lo res line.                 */
                     27: /*                     - Handle simultaneous removal of right border and bottom border with    */
                     28: /*                     the same long switch to 60 Hz (Sync Screen in SNY II).                  */
                     29: /* 2007/05/06  [NP]    More precise tests for top border's removal.                            */
1.1.1.15  root       30: /* 2007/05/11  [NP]    Add support for med res overscan (No Cooper Greetings).                 */
1.1.1.11  root       31: /* 2007/05/12  [NP]    - LastCycleSync50 and LastCycleSync60 for better top border's removal   */
                     32: /*                     in Video_EndHBL.                                                        */
                     33: /*                     - Use VideoOffset in Video_CopyScreenLineColor to handle missing planes */
1.1.1.15  root       34: /*                     depending on line (med/lo and borders).                                 */
1.1.1.11  root       35: /* 2007/09/25  [NP]    Replace printf by calls to HATARI_TRACE.                                */
                     36: /* 2007/10/02  [NP]    Use the new int.c to add interrupts with INT_CPU_CYCLE / INT_MFP_CYCLE. */
                     37: /* 2007/10/23  [NP]    Add support for 0 byte line (60/50 switch at cycle 56). Allow 5 lines   */
                     38: /*                     hardscroll (e.g. SHFORSTV.EXE by Paulo Simmoes).                        */
1.1.1.15  root       39: /* 2007/10/31  [NP]    Use BORDERMASK_LEFT_OFF_MED when left border is removed with hi/med     */
1.1.1.11  root       40: /*                     switch (ST CNX in PYM).                                                 */
                     41: /* 2007/11/02  [NP]    Add support for 4 pixel hardware scrolling ("Let's Do The Twist" by     */
                     42: /*                     ST CNX in Punish Your Machine).                                         */
1.1.1.15  root       43: /* 2007/11/05  [NP]    Depending on the position of the med res switch, the planes will be     */
                     44: /*                     shifted when doing med res overscan (Best Part Of the Creation in PYM   */
1.1.1.11  root       45: /*                     or No Cooper Greetings).                                                */
1.1.1.15  root       46: /* 2007/11/30  [NP]    A hi/med switch to remove the left border can be either used to initiate*/
                     47: /*                     a right hardware scrolling in low res (St Cnx) or a complete med res    */
1.1.1.11  root       48: /*                     overscan line (Dragonnels Reset Part).                                  */
                     49: /*                     Use bit 0-15, 16-19 and 20-23 in ScreenBorderMask[] to track border     */
                     50: /*                     trick, STF hardware scrolling and plane shifting.                       */
                     51: /* 2007/12/22  [NP]    Very precise values for VBL_VIDEO_CYCLE_OFFSET, HBL_VIDEO_CYCLE_OFFSET  */
                     52: /*                     TIMERB_VIDEO_CYCLE_OFFSET and RESTART_VIDEO_COUNTER_CYCLE. These values */
                     53: /*                     were calculated using sttiming.s on a real STF and should give some very*/
                     54: /*                     accurate results (also uses 56 cycles instead of 44 to process an       */
1.1.1.13  root       55: /*                     HBL/VBL/MFP exception).                                                 */
1.1.1.11  root       56: /* 2007/12/29  [NP]    Better support for starting line 2 bytes earlier (if the line starts in */
                     57: /*                     60 Hz and goes back to 50 Hz later), when combined with top border      */
                     58: /*                     removal (Mindbomb Demo - D.I. No Shit).                                 */
                     59: /* 2007/12/30  [NP]    Slight improvement of VideoAdress in Video_CalculateAddress when reading*/
                     60: /*                     during the top border.                                                  */
                     61: /*                     Correct the case where removing top border on line 33 could also be     */
                     62: /*                     interpreted as a right border removal (which is not possible since the  */
                     63: /*                     display is still off at that point).                                    */
                     64: /* 2008/01/03  [NP]    Better handling of nStartHBL and nEndHBL when switching freq from       */
                     65: /*                     50 to 60 Hz. Allows emulation of a "short" 50 Hz screen of 171 lines    */
                     66: /*                     and a more precise removal of bottom border in 50 and 60 Hz.            */
                     67: /* 2008/01/04  [NP]    More generic detection for removing 2 bytes to the right of the line    */
                     68: /*                     when switching from 60 to 50 Hz (works even with a big number of cycles */
                     69: /*                     between the freq changes) (Phaleon's Menus).                            */
                     70: /* 2008/01/06  [NP]    More generic detection for stopping the display in the middle of a line */
                     71: /*                     with a hi / lo res switch (-106 bytes per line). Although switch to     */
                     72: /*                     hi res should occur at cycle 160, some demos use 164 (Phaleon's Menus). */
                     73: /* 2008/01/06  [NP]    Better bottom border's removal in 50 Hz : switch to 60 Hz must occur    */
                     74: /*                     before cycle LINE_REMOVE_BOTTOM_CYCLE on line 263 and switch back to 50 */
                     75: /*                     Hz must occur after LINE_REMOVE_BOTTOM_CYCLE on line 263 (this means    */
                     76: /*                     we can already be in 50 Hz when Video_EndHBL is called and still remove */
                     77: /*                     the bottom border). This is similar to the tests used to remove the     */
                     78: /*                     top border.                                                             */
                     79: /* 2008/01/12  [NP]    In Video_SetHBLPaletteMaskPointers, consider that if a color's change   */
                     80: /*                     occurs after cycle LINE_END_CYCLE_NO_RIGHT, then it's related to the    */
                     81: /*                     next line.                                                              */
                     82: /*                     FIXME : it would be better to handle all color changes through spec512.c*/
                     83: /*                     and drop the 16 colors palette per line.                                */
                     84: /*                     FIXME : we should use Cycles_GetCounterOnWriteAccess, but it doesn't    */
                     85: /*                     support multiple accesses like move.l or movem.                         */
                     86: /* 2008/01/12  [NP]    Handle 60 Hz switch during the active display of the last line to remove*/
                     87: /*                     the bottom border : this should also shorten line by 2 bytes (F.N.I.L.  */
                     88: /*                     Demo by TNT).                                                           */
                     89: /* 2008/01/15  [NP]    Don't do 'left+2' if switch back to 50 Hz occurs when line is not active*/
                     90: /*                     (after cycle LINE_END_CYCLE_60) (XXX International Demos).              */
                     91: /* 2008/01/31  [NP]    Improve left border detection : allow switch to low res on cycle <= 28  */
                     92: /*                     instead of <= 20 (Vodka Demo Main Menu).                                */
                     93: /* 2008/02/02  [NP]    Added 0 byte line detection when switching hi/lo res at position 28     */
                     94: /*                     (Lemmings screen in Nostalgic-o-demo).                                  */
                     95: /* 2008/02/03  [NP]    On STE, write to video counter $ff8205/07/09 should only be applied     */
1.1.1.21  root       96: /*                     immediately if display has not started for the line (before cycle       */
1.1.1.11  root       97: /*                     LINE_END_CYCLE_50). If write occurs after, the change to pVideoRaster   */
                     98: /*                     should be delayed to the end of the line, after processing the current  */
                     99: /*                     line with Video_CopyScreenLineColor (Stardust Tunnel Demo).             */
                    100: /* 2008/02/04  [NP]    The problem is similar when writing to hwscroll $ff8264, we must delay  */
                    101: /*                     the change until the end of the line if display was already started     */
                    102: /*                     (Mindrewind by Reservoir Gods).                                         */
                    103: /* 2008/02/06  [NP]    On STE, when left/right borders are off and hwscroll > 0, we must read  */
                    104: /*                     6 bytes less than the expected value (E605 by Light).                   */
                    105: /* 2008/02/17  [NP]    In Video_CopyScreenLine, LineWidth*2 bytes should be added after        */
                    106: /*                     pNewVideoRaster is copied to pVideoRaster (Braindamage Demo).           */
                    107: /*                     When reading a byte at ff8205/07/09, all video address bytes should be  */
                    108: /*                     updated in Video_ScreenCounter_ReadByte, not just the byte that was     */
                    109: /*                     read. Fix programs that just modify one byte in the video address       */
                    110: /*                     counter (e.g. sub #1,$ff8207 in Braindamage Demo).                      */
                    111: /* 2008/02/19  [NP]    In Video_CalculateAddress, use pVideoRaster instead of VideoBase to     */
                    112: /*                     determine the video address when display is off in the upper part of    */
                    113: /*                     the screen (in case ff8205/07/09 were modified on STE).                 */
                    114: /* 2008/02/20  [NP]    Better handling in Video_ScreenCounter_WriteByte by changing only one   */
                    115: /*                     byte and keeping the other (Braindamage End Part).                      */
                    116: /* 2008/03/08  [NP]    Use M68000_INT_VIDEO when calling M68000_Exception().                   */
                    117: /* 2008/03/13  [NP]    On STE, LineWidth value in $ff820f is added to the shifter counter just */
                    118: /*                     when display is turned off on a line (when right border is started,     */
                    119: /*                     which is usually on cycle 376).                                         */
1.1.1.21  root      120: /*                     This means a write to $ff820f should be applied immediately only if it  */
1.1.1.11  root      121: /*                     occurs before cycle LineEndCycle. Else, it is stored in NewLineWidth    */
                    122: /*                     and used after Video_CopyScreenLine has processed the current line      */
                    123: /*                     (improve the bump mapping part in Pacemaker by Paradox).                */
                    124: /*                     LineWidth should be added to pVideoRaster before checking the possible  */
                    125: /*                     modification of $ff8205/07/09 in Video_CopyScreenLine.                  */
                    126: /* 2008/03/14  [NP]    Rename ScanLineSkip to LineWidth (more consistent with STE docs).       */
                    127: /*                     On STE, better support for writing to video counter, line width and     */
                    128: /*                     hw scroll. If write to register occurs just at the start of a new line  */
                    129: /*                     but before Video_EndHBL (because the move started just before cycle 512)*/
1.1.1.21  root      130: /*                     then the new value should not be set immediately but stored and set     */
1.1.1.11  root      131: /*                     during Video_EndHBL (fix the bump mapping part in Pacemaker by Paradox).*/
1.1.1.12  root      132: /* 2008/03/25  [NP]    On STE, when bSteBorderFlag is true, we should add 16 pixels to the left*/
                    133: /*                     border, not to the right one (Just Musix 2 Menu by DHS).                */
                    134: /* 2008/03/26  [NP]    Clear the rest of the border when using border tricks left+2, left+8    */
                    135: /*                     or right-106 (remove garbage pixels when hatari resolution changes).    */
                    136: /* 2008/03/29  [NP]    Function Video_SetSystemTimings to use different values depending on    */
                    137: /*                     the machine type. On STE, top/bottom border removal can occur at cycle  */
                    138: /*                     500 instead of 504 on STF.                                              */
1.1.1.13  root      139: /* 2008/04/02  [NP]    Correct a rare case in Video_Sync_WriteByte at the end of line 33 :     */
                    140: /*                     nStartHBL was set to 33 instead of 64, which gave a wrong address in    */
                    141: /*                     Video_CalculateAddress.                                                 */
                    142: /* 2008/04/04  [NP]    The value of RestartVideoCounterCycle is slightly different between     */
                    143: /*                     an STF and an STE.                                                      */
                    144: /* 2008/04/05  [NP]    The value of VblVideoCycleOffset is different of 4 cycles between       */
                    145: /*                     STF and STE (fix end part in Pacemaker by Paradox).                     */
                    146: /* 2008/04/09  [NP]    Preliminary support for lines using different frequencies in the same   */
                    147: /*                     screen. In Video_InterruptHandler_EndLine, if the current freq is 50 Hz,*/
                    148: /*                     then next int should be scheduled in 512 cycles ; if freq is 60 Hz,     */
                    149: /*                     next int should be in 508 cycles (used by timer B event count mode).    */
                    150: /* 2008/04/10  [NP]    Update LineEndCycle after changing freq to 50 or 60 Hz.                 */
                    151: /*                     Set EndLine interrupt to happen 28 cycles after LineEndCycle. This way  */
                    152: /*                     Timer B occurs at cycle 404 in 50 Hz, or cycle 400 in 60 Hz (improve    */
                    153: /*                     flickering bottom border in B.I.G. Demo screen 1).                      */
                    154: /* 2008/04/12  [NP]    In the case of a 'right-2' line, we should not change the EndLine's int */
                    155: /*                     position when switching back to 50 Hz ; the int should happen at        */
                    156: /*                     position LINE_END_CYCLE_60 + 28 (Anomaly Demo main menu).               */
                    157: /* 2008/05/31  [NP]    Ignore consecutives writes of the same value in the freq/res register.  */
                    158: /*                     Only the 1st write matters, else this could confuse the code to remove  */
                    159: /*                     top/bottom border (fix OSZI.PRG demo by ULM).                           */
                    160: /* 2008/06/07  [NP]    In Video_SetHBLPaletteMaskPointers, use LineStartCycle instead of the   */
                    161: /*                     50 Hz constant SCREEN_START_CYCLE.                                      */
                    162: /*                     Rename SCREEN_START_HBL_xxx to VIDEO_START_HBL_xxx.                     */
                    163: /*                     Rename SCREEN_END_HBL_xxx to VIDEO_END_HBL_xxx.                         */
                    164: /*                     Rename SCREEN_HEIGHT_HBL_xxx to VIDEO_HEIGHT_HBL_xxx.                   */
                    165: /*                     Use VIDEO_HEIGHT_BOTTOM_50HZ instead of OVERSCAN_BOTTOM.                */
                    166: /* 2008/06/16  [NP]    When Hatari is configured to display the screen's borders, 274 lines    */
                    167: /*                     will be rendered on screen, but if the shifter is in 60 Hz, the last    */
                    168: /*                     16 lines will never be used, which can leave some bad pixels on         */
                    169: /*                     screen. We clear the remaining lines before calling 'Screen_Draw'.      */
                    170: /*                     (in FNIL by Delta Force, fix flickering gfx in the bottom border of the */
                    171: /*                     F2 screen : last 16 lines were the ones from the menu where bottom      */
                    172: /*                     border was removed ).                                                   */
                    173: /* 2008/06/26  [NP]    Improve STE scrolling : handle $ff8264 (no prefetch) and $ff8265        */
                    174: /*                     (prefetch). See Video_HorScroll_Write for details on both registers.    */
                    175: /*                     More generic support for starting display 16 pixels earlier on STE      */
                    176: /*                     by writing to $ff8265 and settting $ff8264=0 just after.                */
                    177: /*                     (fix Digiworld 2 by ICE, which uses $ff8264 for horizontal scroll).     */
                    178: /* 2008/07/07  [NP]    Ignore other 50/60 Hz switches once the right border was removed, keep  */
                    179: /*                     the timer B to occur at pos 460+28 (fix Oxygene screen in Transbeauce 2)*/
                    180: /* 2008/07/14  [NP]    When removing only left border in 60Hz, line size is 26+158 bytes       */
                    181: /*                     instead of 26+160 bytes in 50 Hz (HigResMode demo by Paradox).          */
                    182: /* 2008/07/19  [NP]    If $ff8260==3 (which is not a valid resolution mode), we use 0 instead  */
                    183: /*                     (low res) (fix Omegakul screen in old Omega Demo from 1988).            */
                    184: /* 2008/09/05  [NP]    No need to test 60/50 switch if HblCounterVideo < nStartHBL (display    */
                    185: /*                     has not started yet).                                                   */
                    186: /* 2008/09/25  [NP]    Use nLastVisibleHbl to store the number of the last hbl line that should*/
                    187: /*                     be copied to the emulator's screen buffer.                              */
1.1.1.21  root      188: /*                     On STE, allow to change immediately video address, hw scroll and        */
1.1.1.13  root      189: /*                     linewidth when nHBL>=nLastVisibleHbl instead of nHBL>=nEndHBL           */
                    190: /*                     (fix Power Rise / Xtrem D demo).                                        */
                    191: /* 2008/11/15  [NP]    For STE registers, add in the TRACE call if the write is delayed or     */
                    192: /*                     not (linewidth, hwscroll, video address).                               */
                    193: /*                     On STE, allow to change linewdith, hwscroll and video address with no   */
                    194: /*                     delay as soon as nHBL >= nEndHBL (revert previous changes). Power Rise  */
                    195: /*                     is still working due to NewHWScrollCount=-1 when setting immediate      */
                    196: /*                     hwscroll. Fix regression in Braindamage.                                */
1.1.1.14  root      197: /* 2008/11/29  [NP]    Increment jitter's index for HBL and VBL each time a possible interrupt */
                    198: /*                     occurs. Each interrupt can have a jitter between 0, 4 and 8 cycles ; the*/
                    199: /*                     jitter follows a predefined pattern of 5 values. The HBL and the VBL    */
                    200: /*                     have their own pattern. See InterruptAddJitter() in uae-cpu/newcpu.c    */
                    201: /*                     (fix Fullscreen tunnel in Suretrip 49% by Checkpoint and digi sound in  */
                    202: /*                     Swedish New Year's TCB screen).                                         */
                    203: /* 2008/12/10  [NP]    Enhance support for 0 byte line. The 60/50 Hz switch can happen at      */
                    204: /*                     cycles 56/64, but also at 58/66 (because access to $ff820a doesn't      */
                    205: /*                     require to be on a 4 cycles boundary). As hatari doesn't handle         */
                    206: /*                     multiple of 2 cycles, we allow cycles 56/64 and 60/68 (fix nosync.tos   */
                    207: /*                     that uses the STOP instruction to produce a 0 byte line on the first    */
                    208: /*                     displayed line (found on atari-forum.com)).                             */
1.1.1.17  root      209: /* 2008/12/26  [NP]    When reading $ff8260 on STF, set unused bits to 1 instead of 0          */
1.1.1.14  root      210: /*                     (fix wrong TOS resolution in Awesome Menu Disk 16).                     */
                    211: /*                     Set unused bit to 1 when reading $ff820a too.                           */
                    212: /* 2009/01/16  [NP]    Handle special case when writing only in upper byte of a color reg.     */
                    213: /* 2009/01/21  [NP]    Implement STE horizontal scroll for medium res (fixes cool_ste.prg).    */
                    214: /*                     Take the current res into account in Video_CopyScreenLineColor to       */
1.1.1.15  root      215: /*                     allow mixing low/med res with horizontal scroll on STE.                 */      
                    216: /* 2009/01/24  [NP]    Better detection of 'right-2' when freq is changed to 60 Hz and         */
                    217: /*                     restored to 50 after the end of the current line (fixes games menu on   */
                    218: /*                     BBC compil 10).                                                         */
                    219: /* 2009/01/31  [NP]    Handle a rare case where 'move.b #8,$fffa1f' to start the timer B is    */
                    220: /*                     done just a few cycles before the actual signal for end of line. In that*/
                    221: /*                     case we must ensure that the write was really effective before the end  */
                    222: /*                     of line (else no interrupt should be made) (fix Pompey Pirate Menu #57).*/
                    223: /* 2009/02/08  [NP]    Handle special case for simultaneous HBL exceptions (fixes flickering in*/
                    224: /*                     Monster Business and Super Monaco GP).                                  */
                    225: /* 2009/02/25  [NP]    Ignore other 50/60 Hz switches after display was stopped in the middle  */
                    226: /*                     of the line with a hi/lo switch. Correct missing end of line timer B    */
                    227: /*                     interrupt in that case (fix flickering Dragon Ball part in Blood disk 2 */
                    228: /*                     by Holocaust).                                                          */
                    229: /* 2008/02/02  [NP]    Added 0 byte line detection in STE mode when switching hi/lo res        */
                    230: /*                     at position 32 (Lemmings screen in Nostalgic-o-demo).                   */
                    231: /* 2009/03/28  [NP]    Depending on bit 3 of MFP's AER, timer B will count end of line events  */
                    232: /*                     (bit=0) or start of line events (bit=1) (fix Seven Gates Of Jambala).   */
                    233: /* 2009/04/02  [NP]    Add another method to obtain a 0 byte line, by switching to hi/lo res   */
                    234: /*                     at position 500/508 (fix the game No Buddies Land).                     */
                    235: /* 2009/04/xx  [NP]    Rewrite of many parts : add SHIFTER_FRAME structure, better accuracy    */
                    236: /*                     when mixing 50/60 Hz lines and reading $ff8209, better emulation of     */
                    237: /*                     HBL and Timer B position when changing freq/res, better emulation of    */
                    238: /*                     freq changes for top/bottom/right borders.                              */
                    239: /* 2009/07/16  [NP]    In Video_SetHBLPaletteMaskPointers, if LineCycle>460 we consider the    */
                    240: /*                     color's change should be applied to next line (used when spec512 mode   */
                    241: /*                     if off).                                                                */
1.1.1.16  root      242: /* 2009/10/31  [NP]    Depending on the overscan mode, the displayed lines must be shifted     */
                    243: /*                     left or right (fix Spec 512 images in the Overscan Demos, fix pixels    */
                    244: /*                     alignment in screens mixing normal lines and overscan lines).           */
                    245: /* 2009/12/02  [NP]    If we switch hi/lo around position 464 (as in Enchanted Lands) and      */
                    246: /*                     right border was not removed, then we get an empty line on the next     */
                    247: /*                     HBL (fix Pax Plax Parralax in Beyond by Kruz).                          */
                    248: /* 2009/12/06  [NP]    Add support for STE 224 bytes overscan without stabiliser by switching  */
                    249: /*                     hi/lo at cycle 504/4 to remove left border (fix More Or Less Zero and   */
                    250: /*                     Cernit Trandafir by DHS, as well as Save The Earth by Defence Force).   */
                    251: /* 2009/12/13  [NP]    Improve STE 224 bytes lines : correctly set leftmost 16 pixels to color */
                    252: /*                     0 and correct small glitches when combined with hscroll ($ff8264).      */
                    253: /* 2009/12/13  [NP]    Line scrolling caused by hi/lo switch (STF_PixelScroll) should be       */
                    254: /*                     applied after STE's hardware scrolling, else in overscan 4 color 0      */
                    255: /*                     pixels will appear in the right border (because overscan shift the      */
                    256: /*                     whole displayed area 4 pixels to the left) (fix possible regression on  */
                    257: /*                     STE introduced on 2009/10/31).                                          */
                    258: /* 2010/01/10  [NP]    In Video_CalculateAddress, take bSteBorderFlag into account (+16 pixels */
                    259: /*                     in left border on STE).                                                 */
                    260: /* 2010/01/10  [NP]    In Video_CalculateAddress, take HWScrollPrefetch into account (shifter  */
                    261: /*                     starts 16 pixels earlier) (fix EPSS demo by Unit 17).                   */
                    262: /* 2010/02/05  [NP]    In Video_CalculateAddress, take STE's LineWidth into account when       */
                    263: /*                     display is disabled in the right border (fix flickering in Utopos).     */
                    264: /* 2010/02/07  [NP]    Better support for modifying $ff8205/07/09 while display is on          */
                    265: /*                     (fix EPSS demo by Unit 17).                                             */
                    266: /* 2010/04/12  [NP]    Improve timings when writing to $ff8205/07/09 when hscroll is used,     */
                    267: /*                     using Video_GetMMUStartCycle (fix Pacemaker's Bump Part by Paradox).    */
                    268: /* 2010/05/02  [NP]    In Video_ConvertPosition, handle the case where we read the position    */
                    269: /*                     between the last HBL and the start of the next VBL. During 64 cycles    */
                    270: /*                     FrameCycles can be >= CYCLES_PER_FRAME (harmless fix, only useful when  */
                    271: /*                     using --trace to get correct positions in the logs).                    */
                    272: /* 2010/05/04  [NP]    Improve Video_ConvertPosition, use CyclesPerVBL instead of evaluating   */
                    273: /*                     CYCLES_PER_FRAME (whose value could have changed this the start of the  */
                    274: /*                     VBL).                                                                   */
                    275: /* 2010/05/15  [NP]    In Video_StartInterrupts() when running in monochrome (224 cycles per   */
                    276: /*                     line), the VBL could sometimes be delayed by 160 cycles (divs) and      */
                    277: /*                     hbl/timer B interrupts for line 0 were not called, which could cause an */
                    278: /*                     assert/crash in Hatari when setting timer B on line 2.                  */
                    279: /*                     If we detect VBL was delayed too much, we add hbl/timer b in the next   */
                    280: /*                     4 cycles.                                                               */
1.1.1.17  root      281: /* 2010/07/05  [NP]    When removing left border, allow up to 32 cycles between hi and low     */
                    282: /*                     res switching (fix Megabeer by Invizibles).                             */
                    283: /* 2010/11/01  [NP]    On STE, the 224 bytes overscan will shift the screen 8 pixels to the    */
                    284: /*                     left.                                                                   */
                    285: /*                     For 230 bytes overscan, handle scrolling prefetching when computing     */
                    286: /*                     pVideoRaster for the next line.                                         */
                    287: /* 2010/12/12  [NP]    In Video_CopyScreenLineColor, use pVideoRasterEndLine to improve        */
                    288: /*                     STE's horizontal scrolling for any line's length (160, 224, 230, ...).  */
                    289: /*                     Fix the last 16 pixels for 224 bytes overscan (More Or Less Zero and    */
                    290: /*                     Cernit Trandafir by DHS, Save The Earth by Defence Force).              */
                    291: /* 2011/04/03  [NP]    Call DmaSnd_HBL_Update() on each HBL to handle programs that modify     */
                    292: /*                     the samples data while those data are played by the DMA sound.          */
                    293: /*                     (fixes the game Power Up Plus and the demo Mental Hangover).            */ 
1.1.1.18  root      294: /* 2011/07/30  [NP]    Add blank line detection in STF mode when switching 60/50 Hz at cycle   */
                    295: /*                     28. The shifter will still read bytes and border removal is possible,   */
                    296: /*                     but the line will be blank (we use color 0 for now, but the line should */
                    297: /*                     be black).                                                              */
                    298: /*                     (fix spectrum 512 part in Overscan Demo and shforstv by Paulo Simoes    */
                    299: /*                     by removing "parasite" pixels on the 1st line).                         */
                    300: /* 2011/11/17  [NP]    Improve timings used for the 0 byte line when switching hi/lo at the    */
                    301: /*                     end of the line. The hi/lo switch can be at 496/508 or 500/508          */
                    302: /*                     (fix NGC screen in Delirious Demo IV).                                  */
                    303: /* 2011/11/18  [NP]    Add support for another method to do 4 pixel hardware scrolling by doing*/
                    304: /*                     a med/lo switch after the hi/lo switch to remove left border            */
                    305: /*                     (fix NGC screen in Delirious Demo IV).                                  */
                    306: /* 2011/11/19  [NP]    The 0 byte line obtained by switching hi/lo at the end of the line has  */
                    307: /*                     no video signal at all (blank). In that case, the screen is shifted one */
                    308: /*                     line down, and bottom border removal will happen one line later too     */
                    309: /*                     (fix NGC screen in Delirious Demo IV).                                  */
1.1.1.19  root      310: /* 2012/01/11  [NP]    Don't remove left border when the hi/lo switch is made at cycle >= 12   */
                    311: /*                     (fix 'Kill The Beast 2' in the Vodka Demo)                              */
1.1.1.20  root      312: /* 2012/05/19  [NP]    Allow bottom border to be removed when switch back to 50 Hz is made at  */
                    313: /*                     cycle 504 and more (instead of 508 and more). Same for top border       */
                    314: /*                     (fix 'Musical Wonders 1990' by Offbeat).                                */
1.1.1.21  root      315: /* 2013/03/05  [NP]    An extra 4 cycle delay is added by the MFP to set IRQ when the timer B  */
                    316: /*                     expires in event count mode. Update TIMERB_VIDEO_CYCLE_OFFSET to 24     */
                    317: /*                     cycles instead of 28 to compensate for this and keep the same position. */
                    318: /* 2013/04/26  [NP]    Cancel changes from 2012/05/19, 'Musical Wonders 1990' is really broken */
                    319: /*                     on a real STF and bottom border is not removed.                         */
                    320: /* 2013/05/03  [NP]    Add support for IACK sequence when handling HBL/VBL exceptions. Allow   */
                    321: /*                     to handle the case where interrupt pending bit is set twice (correct    */
                    322: /*                     fix for Super Monaco GP, Super Hang On, Monster Business, European      */
                    323: /*                     Demo's Intro, BBC Menu 52).                                             */
1.1.1.22! root      324: /* 2013/07/17  [NP]    Handle a special case when writing only in lower byte of a color reg.   */
        !           325: /* 2013/12/02  [NP]    If $ff8260==3 (which is not a valid resolution mode), we use 2 instead  */
        !           326: /*                     (high res) (cancel wrong change from 2008/07/19 and fix 'The World Is   */
        !           327: /*                     My Oyster - Convention Report Part' by Aura).                           */
        !           328: /* 2013/12/24  [NP]    In Video_ColorReg_ReadWord, randomly return 0 or 1 for unused bits      */
        !           329: /*                     in STF's color registers (fix 'UMD 8730' by PHF in STF mode)            */
        !           330: /* 2013/12/28  [NP]    For bottom border removal on a 60 Hz screen, max position to go back    */
        !           331: /*                     to 60 Hz should be 4 cycles earlier, as a 60 Hz line starts 4 cycles    */
        !           332: /*                     earlier (fix STE demo "It's a girl 2" by Paradox).                      */
        !           333: /* 2014/02/22  [NP]    In Video_ColorReg_ReadWord(), don't set unused STF bits to rand() if    */
        !           334: /*                     the PC is not executing from the RAM between 0 and 4MB (fix 'Union Demo'*/
        !           335: /* 2014/03/21  [NP]    For STE in med res overscan at 60 Hz, add a 3 pixels shift to have      */
        !           336: /*                     bitmaps and color changes synchronised (fix 'HighResMode' by Paradox).  */
        !           337: /*                     protection code running at address $ff8240).                            */
        !           338: /* 2014/05/08  [NP]    In case we're mixing 50 Hz and 60 Hz lines (512 or 508 cycles), we must */
        !           339: /*                     update the position where the VBL interrupt will happen (fix "keyboard  */
        !           340: /*                     no jitter" test program by Nyh, with 4 lines at 60 Hz and 160240 cycles */
        !           341: /*                     per VBL).                                                               */
        !           342: /* 2014/05/31  [NP]    Ensure pVideoRaster always points into a 24 bit space region. In case   */
        !           343: /*                     video address at $ff8201/03 is set into IO space $ffxxxx, the new value */
        !           344: /*                     for video pointer should not be >= $1000000 (fix "Leavin' Teramis"      */
        !           345: /*                     which sets video address to $ffe100 to display "loading please wait".   */
        !           346: /*                     In that case, we must display $ffe100-$ffffff then $0-$5e00)            */
1.1.1.11  root      347: 
1.1.1.12  root      348: 
1.1.1.15  root      349: const char Video_fileid[] = "Hatari video.c : " __DATE__ " " __TIME__;
1.1       root      350: 
1.1.1.7   root      351: #include <SDL_endian.h>
1.1.1.4   root      352: 
1.1       root      353: #include "main.h"
1.1.1.6   root      354: #include "configuration.h"
1.1.1.10  root      355: #include "cycles.h"
1.1       root      356: #include "fdc.h"
1.1.1.16  root      357: #include "cycInt.h"
1.1.1.8   root      358: #include "ioMem.h"
1.1.1.4   root      359: #include "keymap.h"
1.1       root      360: #include "m68000.h"
1.1.1.21  root      361: #include "hatari-glue.h"
1.1       root      362: #include "memorySnapShot.h"
                    363: #include "mfp.h"
1.1.1.11  root      364: #include "printer.h"
1.1       root      365: #include "screen.h"
1.1.1.13  root      366: #include "screenSnapShot.h"
1.1       root      367: #include "shortcut.h"
                    368: #include "sound.h"
1.1.1.17  root      369: #include "dmaSnd.h"
1.1       root      370: #include "spec512.h"
                    371: #include "stMemory.h"
                    372: #include "vdi.h"
                    373: #include "video.h"
                    374: #include "ymFormat.h"
1.1.1.11  root      375: #include "falcon/videl.h"
                    376: #include "falcon/hostscreen.h"
1.1.1.16  root      377: #include "avi_record.h"
1.1.1.21  root      378: #include "ikbd.h"
1.1.1.22! root      379: #include "floppy_ipf.h"
1.1.1.4   root      380: 
                    381: 
1.1.1.11  root      382: /* The border's mask allows to keep track of all the border tricks             */
                    383: /* applied to one video line. The masks for all lines are stored in the array  */
                    384: /* ScreenBorderMask[].                                                         */
                    385: /* - bits 0-15 are used to describe the border tricks.                         */
                    386: /* - bits 20-23 are used to store the bytes offset to apply for some particular        */
1.1.1.15  root      387: /*   tricks (for example med res overscan can shift display by 0 or 2 bytes    */
                    388: /*   depending on when the switch to med res is done after removing the left   */
1.1.1.11  root      389: /*   border).                                                                  */
                    390: 
1.1.1.14  root      391: #define BORDERMASK_NONE                        0x00    /* no effect on this line */
1.1.1.11  root      392: #define BORDERMASK_LEFT_OFF            0x01    /* removal of left border with hi/lo res switch -> +26 bytes */
                    393: #define BORDERMASK_LEFT_PLUS_2         0x02    /* line starts earlier in 60 Hz -> +2 bytes */
                    394: #define BORDERMASK_STOP_MIDDLE         0x04    /* line ends in hires at cycle 160 -> -106 bytes */
                    395: #define BORDERMASK_RIGHT_MINUS_2       0x08    /* line ends earlier in 60 Hz -> -2 bytes */
                    396: #define BORDERMASK_RIGHT_OFF           0x10    /* removal of right border -> +44 bytes */
                    397: #define BORDERMASK_RIGHT_OFF_FULL      0x20    /* full removal of right border and next left border -> +22 bytes */
1.1.1.15  root      398: #define BORDERMASK_OVERSCAN_MED_RES    0x40    /* some borders were removed and the line is in med res instead of low res */
1.1.1.11  root      399: #define BORDERMASK_EMPTY_LINE          0x80    /* 60/50 Hz switch prevents the line to start, video counter is not incremented */
1.1.1.15  root      400: #define BORDERMASK_LEFT_OFF_MED                0x100   /* removal of left border with hi/med res switch -> +26 bytes (for 4 pixels hardware scrolling) */
1.1.1.16  root      401: #define BORDERMASK_LEFT_OFF_2_STE      0x200   /* shorter removal of left border with hi/lo res switch -> +20 bytes (STE only)*/
1.1.1.18  root      402: #define BORDERMASK_BLANK_LINE          0x400   /* 60/50 Hz switch blanks the rest of the line, but video counter is still incremented */
1.1.1.11  root      403: 
                    404: 
                    405: int STRes = ST_LOW_RES;                         /* current ST resolution */
                    406: int TTRes;                                      /* TT shifter resolution mode */
1.1.1.13  root      407: int nFrameSkips;                                /* speed up by skipping video frames */
1.1.1.9   root      408: 
1.1.1.13  root      409: bool bUseHighRes;                               /* Use hi-res (ie Mono monitor) */
1.1       root      410: int OverscanMode;                               /* OVERSCANMODE_xxxx for current display frame */
1.1.1.17  root      411: Uint16 HBLPalettes[HBL_PALETTE_LINES];          /* 1x16 colour palette per screen line, +1 line just incase write after line 200 */
1.1.1.8   root      412: Uint16 *pHBLPalettes;                           /* Pointer to current palette lists, one per HBL */
1.1.1.17  root      413: Uint32 HBLPaletteMasks[HBL_PALETTE_MASKS];      /* Bit mask of palette colours changes, top bit set is resolution change */
1.1.1.9   root      414: Uint32 *pHBLPaletteMasks;
1.1.1.15  root      415: int nScreenRefreshRate = 50;                    /* 50 or 60 Hz in color, 71 Hz in mono */
1.1.1.8   root      416: Uint32 VideoBase;                               /* Base address in ST Ram for screen (read on each VBL) */
1.1.1.9   root      417: 
1.1.1.11  root      418: int nVBLs;                                      /* VBL Counter */
                    419: int nHBL;                                       /* HBL line */
                    420: int nStartHBL;                                  /* Start HBL for visible screen */
                    421: int nEndHBL;                                    /* End HBL for visible screen */
1.1.1.10  root      422: int nScanlinesPerFrame = 313;                   /* Number of scan lines per frame */
                    423: int nCyclesPerLine = 512;                       /* Cycles per horizontal line scan */
1.1.1.13  root      424: static int nFirstVisibleHbl = FIRST_VISIBLE_HBL_50HZ;                  /* The first line of the ST screen that is copied to the PC screen buffer */
                    425: static int nLastVisibleHbl = FIRST_VISIBLE_HBL_50HZ+NUM_VISIBLE_LINES; /* The last line of the ST screen that is copied to the PC screen buffer */
1.1.1.16  root      426: static int CyclesPerVBL = 313*512;             /* Number of cycles per VBL */
1.1.1.10  root      427: 
1.1.1.13  root      428: static Uint8 HWScrollCount;                    /* HW scroll pixel offset, STE only (0...15) */
                    429: static int NewHWScrollCount = -1;              /* Used in STE mode when writing to the scrolling registers $ff8264/65 */
                    430: static Uint8 HWScrollPrefetch;                 /* 0 when scrolling with $ff8264, 1 when scrolling with $ff8265 */
                    431: static int NewHWScrollPrefetch = -1;           /* Used in STE mode when writing to the scrolling registers $ff8264/65 */
                    432: static Uint8 LineWidth;                                /* Scan line width add, STe only (words, minus 1) */
                    433: static int NewLineWidth = -1;                  /* Used in STE mode when writing to the line width register $ff820f */
1.1.1.16  root      434: static int VideoCounterDelayedOffset = 0;      /* Used in STE mode when changing video counter while display is on */
                    435: static Uint8 *pVideoRasterDelayed = NULL;      /* Used in STE mode when changing video counter while display is off in the right border */
1.1.1.13  root      436: static Uint8 *pVideoRaster;                    /* Pointer to Video raster, after VideoBase in PC address space. Use to copy data on HBL */
1.1.1.15  root      437: static bool bSteBorderFlag;                    /* true when screen width has been switched to 336 (e.g. in Obsession) */
1.1.1.13  root      438: static int NewSteBorderFlag = -1;              /* New value for next line */
1.1.1.21  root      439: static bool bTTColorsSync, bTTColorsSTSync;    /* whether TT colors need conversion to SDL */
                    440: 
                    441: bool bTTSampleHold = false;                            /* TT special video mode */
                    442: static bool bTTHypermono = false;              /* TT special video mode */
                    443: 
                    444: static int TTSpecialVideoMode = 0;             /* TT special video mode */
                    445: static int nPrevTTSpecialVideoMode = 0;        /* TT special video mode */
1.1       root      446: 
1.1.1.17  root      447: static int LastCycleScroll8264;                        /* value of Cycles_GetCounterOnWriteAccess last time ff8264 was set for the current VBL */
                    448: static int LastCycleScroll8265;                        /* value of Cycles_GetCounterOnWriteAccess last time ff8265 was set for the current VBL */
                    449: 
                    450: static int LineRemoveTopCycle = LINE_REMOVE_TOP_CYCLE_STF;
                    451: static int LineRemoveBottomCycle = LINE_REMOVE_BOTTOM_CYCLE_STF;
                    452: static int RestartVideoCounterCycle = RESTART_VIDEO_COUNTER_CYCLE_STF;
                    453: static int VblVideoCycleOffset = VBL_VIDEO_CYCLE_OFFSET_STF;
1.1.1.13  root      454: 
                    455: int    LineTimerBCycle = LINE_END_CYCLE_50 + TIMERB_VIDEO_CYCLE_OFFSET;        /* position of the Timer B interrupt on active lines */
1.1.1.15  root      456: int    TimerBEventCountCycleStart = -1;        /* value of Cycles_GetCounterOnWriteAccess last time timer B was started for the current VBL */
1.1       root      457: 
1.1.1.20  root      458: int HblJitterIndex = 0;
                    459: const int HblJitterArray[] = {
                    460:        8,4,4,0,0 /* measured on STF */
                    461: };
                    462: const int HblJitterArrayPending[] = {
                    463:        4,4,4,4,4 // { 8,8,12,8,12 }; /* measured on STF, not always accurate */
                    464: };
                    465: int VblJitterIndex = 0;
                    466: const int VblJitterArray[] = {
                    467:        8,0,4,0,4 /* measured on STF */
                    468: };
                    469: const int VblJitterArrayPending[] = {
                    470:        8,8,12,8,12 /* not verified on STF, use the same as HBL */
                    471: };
1.1.1.14  root      472: 
1.1.1.18  root      473: int    BlankLines = 0;                         /* Number of empty line with no signal (by switching hi/lo near cycles 500) */
1.1.1.12  root      474: 
1.1.1.15  root      475: 
                    476: typedef struct
                    477: {
                    478:        int     VBL;                            /* VBL for this Pos (or -1 if Pos not defined for now) */
                    479:        int     FrameCycles;                    /* Number of cycles since this VBL */
                    480:        int     HBL;                            /* HBL in the VBL */
                    481:        int     LineCycles;                     /* cycles in the HBL */
                    482: } SHIFTER_POS;
                    483: 
                    484: 
                    485: typedef struct 
                    486: {
                    487:        int     StartCycle;                     /* first cycle of this line, as returned by Cycles_GetCounter */
                    488: 
                    489:        Uint32  BorderMask;                     /* borders' states for this line */
                    490:        int     DisplayPixelShift;              /* number of pixels to shift the whole line (<0 shift to the left, >0 shift to the right) */
                    491:                                                /* On STF, this is obtained when switching hi/med for a variable number of cycles, */
                    492:                                                /* but just removing left border will shift the line too. */
                    493: 
                    494:        int     DisplayStartCycle;              /* cycle where display starts for this line (0-512) : 0, 52 or 56 */
                    495:        int     DisplayEndCycle;                /* cycle where display ends for this line (0-512) : 0, 160, 372, 376, 460 or 512 */
                    496:        int     DisplayBytes;                   /* how many bytes to display for this line */
                    497: 
                    498: } SHIFTER_LINE;
                    499: 
                    500: 
                    501: typedef struct
                    502: {
                    503:        int     HBL_CyclePos;                   /* cycle position for the HBL int (depends on freq/res) */
                    504:        int     TimerB_CyclePos;                /* cycle position for the Timer B int (depends on freq/res) */
                    505: 
                    506:        int     Freq;                           /* value of ff820a & 2, or -1 if not set */
                    507:        int     Res;                            /* value of ff8260 & 3, or -1 if not set */
                    508:        SHIFTER_POS     FreqPos50;              /* position of latest freq change to 50 Hz*/
                    509:        SHIFTER_POS     FreqPos60;              /* position of latest freq change to 60 Hz*/
                    510:        SHIFTER_POS     ResPosLo;               /* position of latest change to low res */
                    511:        SHIFTER_POS     ResPosMed;              /* position of latest change to med res */
                    512:        SHIFTER_POS     ResPosHi;               /* position of latest change to high res */
                    513: 
                    514:        SHIFTER_POS     Scroll8264Pos;          /* position of latest write to $ff8264 */
                    515:        SHIFTER_POS     Scroll8265Pos;          /* position of latest write to $ff8265 */
                    516: 
                    517:        SHIFTER_LINE    ShifterLines[ MAX_SCANLINES_PER_FRAME ];
                    518: } SHIFTER_FRAME;
                    519: 
                    520: 
                    521: SHIFTER_FRAME  ShifterFrame;
                    522: 
                    523: 
                    524: 
                    525: /*--------------------------------------------------------------*/
                    526: /* Local functions prototypes                                   */
                    527: /*--------------------------------------------------------------*/
                    528: 
                    529: static void    Video_SetSystemTimings ( void );
                    530: 
                    531: static Uint32  Video_CalculateAddress ( void );
1.1.1.16  root      532: static int     Video_GetMMUStartCycle ( int DisplayStartCycle );
1.1.1.15  root      533: static void    Video_WriteToShifter ( Uint8 Res );
                    534: static void    Video_Sync_SetDefaultStartEnd ( Uint8 Freq , int HblCounterVideo , int LineCycles );
                    535: 
                    536: static int     Video_HBL_GetPos ( void );
1.1.1.18  root      537: static int     Video_TimerB_GetDefaultPos ( void );
1.1.1.15  root      538: static void    Video_EndHBL ( void );
                    539: static void    Video_StartHBL ( void );
                    540: 
                    541: static void    Video_StoreFirstLinePalette(void);
                    542: static void    Video_StoreResolution(int y);
                    543: static void    Video_CopyScreenLineMono(void);
                    544: static void    Video_CopyScreenLineColor(void);
                    545: static void    Video_CopyVDIScreen(void);
                    546: static void    Video_SetHBLPaletteMaskPointers(void);
                    547: 
                    548: static void    Video_UpdateTTPalette(int bpp);
                    549: static void    Video_DrawScreen(void);
                    550: 
                    551: static void    Video_ResetShifterTimings(void);
                    552: static void    Video_InitShifterLines(void);
                    553: static void    Video_ClearOnVBL(void);
                    554: 
                    555: static void    Video_AddInterrupt ( int Pos , interrupt_id Handler );
                    556: static void    Video_AddInterruptHBL ( int Pos );
                    557: 
1.1.1.22! root      558: static void    Video_ColorReg_WriteWord(void);
        !           559: static void    Video_ColorReg_ReadWord(void);
1.1.1.15  root      560: 
                    561: 
1.1       root      562: /*-----------------------------------------------------------------------*/
1.1.1.11  root      563: /**
                    564:  * Save/Restore snapshot of local variables('MemorySnapShot_Store' handles type)
                    565:  */
1.1.1.13  root      566: void Video_MemorySnapShot_Capture(bool bSave)
1.1       root      567: {
1.1.1.11  root      568:        /* Save/Restore details */
                    569:        MemorySnapShot_Store(&TTRes, sizeof(TTRes));
                    570:        MemorySnapShot_Store(&bUseHighRes, sizeof(bUseHighRes));
                    571:        MemorySnapShot_Store(&nVBLs, sizeof(nVBLs));
                    572:        MemorySnapShot_Store(&nHBL, sizeof(nHBL));
                    573:        MemorySnapShot_Store(&nStartHBL, sizeof(nStartHBL));
                    574:        MemorySnapShot_Store(&nEndHBL, sizeof(nEndHBL));
                    575:        MemorySnapShot_Store(&OverscanMode, sizeof(OverscanMode));
                    576:        MemorySnapShot_Store(HBLPalettes, sizeof(HBLPalettes));
                    577:        MemorySnapShot_Store(HBLPaletteMasks, sizeof(HBLPaletteMasks));
                    578:        MemorySnapShot_Store(&VideoBase, sizeof(VideoBase));
                    579:        MemorySnapShot_Store(&LineWidth, sizeof(LineWidth));
                    580:        MemorySnapShot_Store(&HWScrollCount, sizeof(HWScrollCount));
                    581:        MemorySnapShot_Store(&pVideoRaster, sizeof(pVideoRaster));
                    582:        MemorySnapShot_Store(&nScanlinesPerFrame, sizeof(nScanlinesPerFrame));
                    583:        MemorySnapShot_Store(&nCyclesPerLine, sizeof(nCyclesPerLine));
                    584:        MemorySnapShot_Store(&nFirstVisibleHbl, sizeof(nFirstVisibleHbl));
                    585:        MemorySnapShot_Store(&bSteBorderFlag, sizeof(bSteBorderFlag));
1.1.1.14  root      586:        MemorySnapShot_Store(&HblJitterIndex, sizeof(HblJitterIndex));
                    587:        MemorySnapShot_Store(&VblJitterIndex, sizeof(VblJitterIndex));
1.1.1.15  root      588:        MemorySnapShot_Store(&ShifterFrame, sizeof(ShifterFrame));
1.1.1.21  root      589:        MemorySnapShot_Store(&bTTSampleHold, sizeof(bTTSampleHold));
                    590:        MemorySnapShot_Store(&bTTHypermono, sizeof(bTTHypermono));
                    591:        MemorySnapShot_Store(&TTSpecialVideoMode, sizeof(TTSpecialVideoMode));
1.1.1.15  root      592: }
                    593: 
                    594: 
                    595: /*-----------------------------------------------------------------------*/
                    596: /**
                    597:  * Reset video chip
                    598:  */
                    599: void Video_Reset(void)
                    600: {
                    601:        /* NOTE! Must reset all of these register type things here!!!! */
1.1.1.19  root      602:        Video_Reset_Glue();
1.1.1.15  root      603: 
                    604:        /* Set system specific timings */
                    605:        Video_SetSystemTimings();
                    606: 
                    607:        /* Reset VBL counter */
                    608:        nVBLs = 0;
                    609:        /* Reset addresses */
                    610:        VideoBase = 0L;
                    611: 
                    612:        /* Reset shifter's state variables */
                    613:        ShifterFrame.Freq = -1;
                    614:        ShifterFrame.Res = -1;
                    615:        ShifterFrame.FreqPos50.VBL = -1;
                    616:        ShifterFrame.FreqPos60.VBL = -1;
                    617:        ShifterFrame.ResPosLo.VBL = -1;
                    618:        ShifterFrame.ResPosMed.VBL = -1;
                    619:        ShifterFrame.ResPosHi.VBL = -1;
                    620:        ShifterFrame.Scroll8264Pos.VBL = -1;
                    621:        ShifterFrame.Scroll8265Pos.VBL = -1;
                    622: 
                    623:        Video_InitShifterLines ();
                    624: 
                    625:        /* Reset STE screen variables */
                    626:        LineWidth = 0;
                    627:        HWScrollCount = 0;
                    628:        bSteBorderFlag = false;
                    629: 
                    630:        NewLineWidth = -1;                      /* cancel pending modifications set before the reset */
                    631:        NewHWScrollCount = -1;
                    632: 
1.1.1.16  root      633:        VideoCounterDelayedOffset = 0;
                    634:        pVideoRasterDelayed = NULL;
                    635: 
1.1.1.15  root      636:        /* Reset jitter indexes */
                    637:        HblJitterIndex = 0;
                    638:        VblJitterIndex = 0;
                    639: 
                    640:        /* Clear framecycles counter */
                    641:        Cycles_SetCounter(CYCLES_COUNTER_VIDEO, 0);
                    642: 
                    643:        /* Clear ready for new VBL */
                    644:        Video_ClearOnVBL();
                    645: }
                    646: 
                    647: 
                    648: /*-----------------------------------------------------------------------*/
                    649: /**
                    650:  * Reset the GLUE chip responsible for generating the H/V sync signals.
                    651:  * When the 68000 RESET instruction is called, frequency and resolution
                    652:  * should be reset to 0.
                    653:  */
                    654: void Video_Reset_Glue(void)
                    655: {
1.1.1.20  root      656:        Uint8 VideoShifterByte;
                    657: 
1.1.1.19  root      658:        IoMem_WriteByte(0xff820a,0);                    /* Video frequency */
                    659: 
                    660:        /* Are we in high-res? */
                    661:        if (bUseHighRes)
                    662:                VideoShifterByte = ST_HIGH_RES;         /* Mono monitor */
                    663:        else
                    664:                VideoShifterByte = ST_LOW_RES;
                    665:        if (bUseVDIRes)
                    666:                VideoShifterByte = VDIRes;
                    667: 
                    668:        IoMem_WriteByte(0xff8260, VideoShifterByte);
1.1       root      669: }
                    670: 
1.1.1.8   root      671: 
1.1       root      672: /*-----------------------------------------------------------------------*/
1.1.1.12  root      673: /*
                    674:  * Set specific video timings, depending on the system being emulated.
                    675:  */
1.1.1.15  root      676: static void    Video_SetSystemTimings(void)
1.1.1.12  root      677: {
1.1.1.19  root      678:        if ( ConfigureParams.System.nMachineType == MACHINE_ST )
                    679:        {
                    680:                LineRemoveTopCycle = LINE_REMOVE_TOP_CYCLE_STF;
                    681:                LineRemoveBottomCycle = LINE_REMOVE_BOTTOM_CYCLE_STF;
                    682:                RestartVideoCounterCycle = RESTART_VIDEO_COUNTER_CYCLE_STF;
                    683:                VblVideoCycleOffset = VBL_VIDEO_CYCLE_OFFSET_STF;
                    684:        }
1.1.1.20  root      685:        else                            /* STE, TT */
1.1.1.19  root      686:        {
                    687:                LineRemoveTopCycle = LINE_REMOVE_TOP_CYCLE_STE;
                    688:                LineRemoveBottomCycle = LINE_REMOVE_BOTTOM_CYCLE_STE;
                    689:                RestartVideoCounterCycle = RESTART_VIDEO_COUNTER_CYCLE_STE;
                    690:                VblVideoCycleOffset = VBL_VIDEO_CYCLE_OFFSET_STE;
                    691:        }
1.1.1.12  root      692: }
                    693: 
                    694: 
                    695: /*-----------------------------------------------------------------------*/
1.1.1.11  root      696: /**
1.1.1.15  root      697:  * Convert the elapsed number of cycles since the start of the VBL
                    698:  * into the corresponding HBL number and the cycle position in the current
                    699:  * HBL. We use the starting cycle position of the closest HBL to compute
                    700:  * the cycle position on the line (this allows to mix lines with different
                    701:  * values for nCyclesPerLine).
                    702:  * We can have 2 cases on the limit where the real video line count can be
                    703:  * different from nHBL :
                    704:  * - when reading video address between cycle 0 and 12, LineCycle will be <0,
                    705:  *   so we need to use the data from line nHBL-1
                    706:  * - if LineCycle >= nCyclesPerLine, this means the HBL int was not processed
                    707:  *   yet, so the video line number is in fact nHBL+1
                    708:  */
                    709: 
                    710: void   Video_ConvertPosition ( int FrameCycles , int *pHBL , int *pLineCycles )
                    711: {
1.1.1.16  root      712:        if ( 0 && FrameCycles >= CyclesPerVBL )                         /* rare case between end of last hbl and start of next VBL (during 64 cycles) */
1.1.1.15  root      713:        {
1.1.1.16  root      714:                *pHBL = ( FrameCycles - CyclesPerVBL ) / nCyclesPerLine;
                    715:                *pLineCycles = ( FrameCycles - CyclesPerVBL ) % nCyclesPerLine;
                    716:        //fprintf ( stderr , "out of vbl FrameCycles %d CyclesPerVBL %d nHBL=%d %d %d\n" , FrameCycles , CyclesPerVBL, nHBL , *pHBL , *pLineCycles );
1.1.1.15  root      717:        }
1.1.1.16  root      718: 
                    719:        else                                                            /* most common case */
1.1.1.15  root      720:        {
1.1.1.16  root      721:                *pHBL = nHBL;
                    722:                *pLineCycles = FrameCycles - ShifterFrame.ShifterLines[ nHBL ].StartCycle;
1.1.1.15  root      723: 
1.1.1.16  root      724:                if ( *pLineCycles < 0 )                                 /* reading from the previous video line */
                    725:                {
                    726:                        *pHBL = nHBL-1;
                    727:                        *pLineCycles = FrameCycles - ShifterFrame.ShifterLines[ nHBL-1 ].StartCycle;
                    728:                }
                    729:        
                    730:                else if ( *pLineCycles >= nCyclesPerLine )              /* reading on the next line, but HBL int was delayed */
                    731:                {
                    732:                        *pHBL = nHBL+1;
                    733:                        *pLineCycles -= nCyclesPerLine;
                    734:                }
                    735:        }
1.1.1.15  root      736: 
                    737: if ( *pLineCycles < 0 )
                    738:        fprintf ( stderr , "bug nHBL=%d %d %d\n" , nHBL , *pHBL , *pLineCycles );
                    739: 
                    740: //if ( ( *pHBL != FrameCycles / nCyclesPerLine ) || ( *pLineCycles != FrameCycles % nCyclesPerLine ) )
                    741: //  LOG_TRACE ( TRACE_VIDEO_ADDR , "conv pos %d %d - %d %d\n" , *pHBL , FrameCycles / nCyclesPerLine , *pLineCycles , FrameCycles % nCyclesPerLine );
                    742: //  LOG_TRACE ( TRACE_VIDEO_ADDR , "conv pos %d %d %d\n" , FrameCycles , *pHBL , *pLineCycles );
                    743: }
                    744: 
                    745: 
                    746: void   Video_GetPosition ( int *pFrameCycles , int *pHBL , int *pLineCycles )
                    747: {
                    748:        *pFrameCycles = Cycles_GetCounter(CYCLES_COUNTER_VIDEO);
                    749:        Video_ConvertPosition ( *pFrameCycles , pHBL , pLineCycles );
                    750: }
                    751: 
                    752: 
                    753: void   Video_GetPosition_OnWriteAccess ( int *pFrameCycles , int *pHBL , int *pLineCycles )
                    754: {
                    755:        *pFrameCycles = Cycles_GetCounterOnWriteAccess(CYCLES_COUNTER_VIDEO);
                    756:        Video_ConvertPosition ( *pFrameCycles , pHBL , pLineCycles );
                    757: }
                    758: 
                    759: 
                    760: void   Video_GetPosition_OnReadAccess ( int *pFrameCycles , int *pHBL , int *pLineCycles )
                    761: {
                    762:        *pFrameCycles = Cycles_GetCounterOnReadAccess(CYCLES_COUNTER_VIDEO);
                    763:        Video_ConvertPosition ( *pFrameCycles , pHBL , pLineCycles );
                    764: }
                    765: 
                    766: 
                    767: /*-----------------------------------------------------------------------*/
                    768: /**
1.1.1.11  root      769:  * Calculate and return video address pointer.
                    770:  */
1.1.1.15  root      771: static Uint32 Video_CalculateAddress ( void )
1.1       root      772: {
1.1.1.15  root      773:        int FrameCycles, HblCounterVideo, LineCycles;
                    774:        int X, NbBytes;
1.1.1.11  root      775:        Uint32 VideoAddress;      /* Address of video display in ST screen space */
                    776:        int nSyncByte;
                    777:        int LineBorderMask;
                    778:        int PrevSize;
                    779:        int CurSize;
1.1.1.15  root      780:        int LineStartCycle , LineEndCycle;
1.1.1.11  root      781: 
                    782:        /* Find number of cycles passed during frame */
1.1.1.21  root      783:        /* We need to subtract '12' for correct video address calculation */
1.1.1.15  root      784:        FrameCycles = Cycles_GetCounterOnReadAccess(CYCLES_COUNTER_VIDEO) - 12;
1.1.1.11  root      785: 
                    786:        /* Now find which pixel we are on (ignore left/right borders) */
1.1.1.15  root      787:        Video_ConvertPosition ( FrameCycles , &HblCounterVideo , &LineCycles );
                    788:        X = LineCycles;
1.1.1.11  root      789: 
                    790:        nSyncByte = IoMem_ReadByte(0xff820a) & 2;       /* only keep bit 1 */
                    791:        if (nSyncByte)                          /* 50 Hz */
                    792:        {
                    793:                LineStartCycle = LINE_START_CYCLE_50;
                    794:                LineEndCycle = LINE_END_CYCLE_50;
                    795:        }
                    796:        else                                            /* 60 Hz */
                    797:        {
                    798:                LineStartCycle = LINE_START_CYCLE_60;
                    799:                LineEndCycle = LINE_END_CYCLE_60;
                    800:        }
                    801: 
                    802: 
                    803:        /* Top of screen is usually 63 lines from VBL in 50 Hz */
1.1.1.15  root      804:        if ( HblCounterVideo < nStartHBL )
1.1.1.11  root      805:        {
                    806:                /* pVideoRaster was set during Video_ClearOnVBL using VideoBase */
                    807:                /* and it could also have been modified on STE by writing to ff8205/07/09 */
1.1.1.15  root      808:                /* So, we should not use ff8201/ff8203 which are reloaded in ff8205/ff8207 only once per VBL */
                    809:                /* but use pVideoRaster - STRam instead to get current shifter video address */
1.1.1.11  root      810:                VideoAddress = pVideoRaster - STRam;
                    811:        }
                    812: 
1.1.1.15  root      813:        else if (FrameCycles > RestartVideoCounterCycle)
1.1.1.11  root      814:        {
                    815:                /* This is where ff8205/ff8207 are reloaded with the content of ff8201/ff8203 on a real ST */
                    816:                /* (used in ULM DSOTS demos). VideoBase is also reloaded in Video_ClearOnVBL to be sure */
                    817:                VideoBase = (Uint32)IoMem_ReadByte(0xff8201)<<16 | (Uint32)IoMem_ReadByte(0xff8203)<<8;
                    818:                if (ConfigureParams.System.nMachineType != MACHINE_ST)
                    819:                {
1.1.1.20  root      820:                        /* on STe 2 aligned, on TT 8 aligned. We do STe. */
1.1.1.11  root      821:                        VideoBase |= IoMem_ReadByte(0xff820d) & ~1;
                    822:                }
                    823: 
                    824:                VideoAddress = VideoBase;
                    825:        }
                    826: 
                    827:        else
                    828:        {
                    829:                VideoAddress = pVideoRaster - STRam;            /* pVideoRaster is updated by Video_CopyScreenLineColor */
                    830: 
                    831:                /* Now find which pixel we are on (ignore left/right borders) */
1.1.1.15  root      832: //             X = ( Cycles_GetCounterOnReadAccess(CYCLES_COUNTER_VIDEO) - 12 ) % nCyclesPerLine;
1.1.1.11  root      833: 
                    834:                /* Get real video line count (can be different from nHBL) */
1.1.1.15  root      835: //             HblCounterVideo = ( Cycles_GetCounterOnReadAccess(CYCLES_COUNTER_VIDEO) - 12 ) / nCyclesPerLine;
1.1.1.11  root      836: 
                    837:                /* Correct the case when read overlaps end of line / start of next line */
                    838:                /* Video_CopyScreenLineColor was not called yet to update VideoAddress */
                    839:                /* so we need to determine the size of the previous line to get the */
                    840:                /* correct value of VideoAddress. */
                    841:                PrevSize = 0;
                    842:                if ( HblCounterVideo < nHBL )
                    843:                        X = 0;
                    844:                else if ( ( HblCounterVideo > nHBL )            /* HblCounterVideo = nHBL+1 */
                    845:                          &&  ( nHBL >= nStartHBL ) )           /* if nHBL was not visible, PrevSize = 0 */
                    846:                {
1.1.1.15  root      847:                        LineBorderMask = ShifterFrame.ShifterLines[ HblCounterVideo-1 ].BorderMask; /* get border mask for nHBL */
1.1.1.11  root      848:                        PrevSize = BORDERBYTES_NORMAL;          /* normal line */
                    849: 
                    850:                        if (LineBorderMask & BORDERMASK_LEFT_OFF)
                    851:                                PrevSize += BORDERBYTES_LEFT;
                    852:                        else if (LineBorderMask & BORDERMASK_LEFT_PLUS_2)
                    853:                                PrevSize += 2;
                    854: 
                    855:                        if (LineBorderMask & BORDERMASK_STOP_MIDDLE)
                    856:                                PrevSize -= 106;
                    857:                        else if (LineBorderMask & BORDERMASK_RIGHT_MINUS_2)
                    858:                                PrevSize -= 2;
                    859:                        else if (LineBorderMask & BORDERMASK_RIGHT_OFF)
                    860:                                PrevSize += BORDERBYTES_RIGHT;
                    861: 
                    862:                        if (LineBorderMask & BORDERMASK_EMPTY_LINE)
                    863:                                PrevSize = 0;
                    864:                }
                    865: 
                    866: 
1.1.1.15  root      867:                LineBorderMask = ShifterFrame.ShifterLines[ HblCounterVideo ].BorderMask;
1.1.1.11  root      868: 
                    869:                CurSize = BORDERBYTES_NORMAL;                   /* normal line */
                    870: 
                    871:                if (LineBorderMask & BORDERMASK_LEFT_OFF)
                    872:                        CurSize += BORDERBYTES_LEFT;
                    873:                else if (LineBorderMask & BORDERMASK_LEFT_PLUS_2)
                    874:                        CurSize += 2;
1.1.1.16  root      875:                else if (bSteBorderFlag)                        /* bigger line by 8 bytes on the left (STE specific) */
                    876:                        CurSize += 8;
                    877:                else if ( ( HWScrollCount > 0 ) && ( HWScrollPrefetch == 1 ) )
                    878:                        CurSize += 8;                           /* 8 more bytes are loaded when scrolling with prefetching */
1.1.1.11  root      879: 
                    880:                if (LineBorderMask & BORDERMASK_STOP_MIDDLE)
                    881:                        CurSize -= 106;
                    882:                else if (LineBorderMask & BORDERMASK_RIGHT_MINUS_2)
                    883:                        CurSize -= 2;
                    884:                else if (LineBorderMask & BORDERMASK_RIGHT_OFF)
                    885:                        CurSize += BORDERBYTES_RIGHT;
                    886:                if (LineBorderMask & BORDERMASK_RIGHT_OFF_FULL)
                    887:                        CurSize += BORDERBYTES_RIGHT_FULL;
                    888: 
                    889:                if ( LineBorderMask & BORDERMASK_LEFT_PLUS_2)
                    890:                        LineStartCycle = LINE_START_CYCLE_60;
                    891:                else if ( LineBorderMask & BORDERMASK_LEFT_OFF )
1.1.1.15  root      892:                        LineStartCycle = LINE_START_CYCLE_71;
1.1.1.16  root      893:                else if ( bSteBorderFlag )
                    894:                        LineStartCycle -= 16;                   /* display starts 16 pixels earlier */
                    895:                else if ( ( HWScrollCount > 0 ) && ( HWScrollPrefetch == 1 ) )
                    896:                        LineStartCycle -= 16;                   /* shifter starts reading 16 pixels earlier when scrolling with prefetching */
1.1.1.11  root      897: 
                    898:                LineEndCycle = LineStartCycle + CurSize*2;
                    899: 
                    900: 
                    901:                if ( X < LineStartCycle )
1.1.1.16  root      902:                        X = LineStartCycle;                     /* display is disabled in the left border */
1.1.1.11  root      903:                else if ( X > LineEndCycle )
1.1.1.16  root      904:                {
                    905:                        X = LineEndCycle;                       /* display is disabled in the right border */
                    906:                        /* On  STE, the Shifter skips the given amount of words as soon as display is disabled */
                    907:                        /* (LineWidth is 0 on STF */
                    908:                        VideoAddress += LineWidth*2;
                    909:                }
1.1.1.11  root      910: 
                    911:                NbBytes = ( (X-LineStartCycle)>>1 ) & (~1);     /* 2 cycles per byte */
                    912: 
                    913: 
                    914:                /* when left border is open, we have 2 bytes less than theorical value */
                    915:                /* (26 bytes in left border, which is not a multiple of 4 cycles) */
                    916:                if ( LineBorderMask & BORDERMASK_LEFT_OFF )
                    917:                        NbBytes -= 2;
                    918: 
                    919:                if ( LineBorderMask & BORDERMASK_EMPTY_LINE )
                    920:                        NbBytes = 0;
                    921: 
1.1.1.16  root      922:                /* Add line cycles if we have not reached end of screen yet */
1.1.1.18  root      923:                if ( HblCounterVideo < nEndHBL + BlankLines )
1.1.1.11  root      924:                        VideoAddress += PrevSize + NbBytes;
                    925:        }
                    926: 
1.1.1.16  root      927:        LOG_TRACE(TRACE_VIDEO_ADDR , "video base=%x raster=%x addr=%x video_cyc=%d "
                    928:                  "line_cyc=%d/X=%d @ nHBL=%d/video_hbl=%d %d<->%d pc=%x instr_cyc=%d\n",
                    929:                  VideoBase, (int)(pVideoRaster - STRam), VideoAddress,
                    930:                  Cycles_GetCounter(CYCLES_COUNTER_VIDEO), LineCycles, X, nHBL,
                    931:                  HblCounterVideo, LineStartCycle, LineEndCycle, M68000_GetPC(), CurrentInstrCycles);
1.1.1.11  root      932: 
                    933:        return VideoAddress;
                    934: }
                    935: 
                    936: 
                    937: /*-----------------------------------------------------------------------*/
                    938: /**
1.1.1.16  root      939:  * Calculate the cycle where the STF/STE's MMU starts reading
                    940:  * data to send them to the shifter.
                    941:  * On STE, if hscroll is used, prefetch will cause this position to
                    942:  * happen 16 cycles earlier.
                    943:  * This function should use the same logic as in Video_CalculateAddress.
                    944:  * NOTE : this function is not completly accurate, as even when there's
                    945:  * no hscroll (on STF) the mmu starts reading 16 cycles before display starts.
                    946:  * But it's good enough to emulate writing to ff8205/07/09 on STE.
                    947:  */
                    948: static int Video_GetMMUStartCycle ( int DisplayStartCycle )
                    949: {
                    950:        if ( bSteBorderFlag )
                    951:                DisplayStartCycle -= 16;                        /* display starts 16 pixels earlier */
                    952:        else if ( ( HWScrollCount > 0 ) && ( HWScrollPrefetch == 1 ) )
                    953:                DisplayStartCycle -= 16;                        /* shifter starts reading 16 pixels earlier when scrolling with prefetching */
                    954: 
                    955:        return DisplayStartCycle;
                    956: }
                    957: 
                    958: 
                    959: /*-----------------------------------------------------------------------*/
                    960: /**
1.1.1.11  root      961:  * Write to VideoShifter (0xff8260), resolution bits
                    962:  */
1.1.1.15  root      963: static void Video_WriteToShifter ( Uint8 Res )
1.1       root      964: {
1.1.1.15  root      965:        int FrameCycles, HblCounterVideo, LineCycles;
1.1.1.11  root      966: 
1.1.1.15  root      967:        Video_GetPosition_OnWriteAccess ( &FrameCycles , &HblCounterVideo , &LineCycles );
1.1.1.11  root      968: 
1.1.1.15  root      969:        LOG_TRACE(TRACE_VIDEO_RES ,"shifter=0x%2.2X video_cyc_w=%d line_cyc_w=%d @ nHBL=%d/video_hbl_w=%d pc=%x instr_cyc=%d\n",
                    970:                       Res, FrameCycles, LineCycles, nHBL, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles );
1.1.1.11  root      971: 
                    972: 
1.1.1.13  root      973:        /* Ignore consecutive writes of the same value */
1.1.1.15  root      974:        if ( Res == ShifterFrame.Res )
1.1.1.13  root      975:                return;                                         /* do nothing */
                    976: 
1.1.1.15  root      977: 
                    978:        if ( Res == 0x02 )                                      /* switch to high res */
                    979:        {
                    980:                if ( LineCycles < ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle )      /* start could be 0,52,56 */
                    981:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle = LINE_START_CYCLE_71;
                    982: 
                    983:                if ( ( LineCycles < ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle )      /* end could be 160,372,376,460 */
                    984:                  && ( LineCycles < LINE_END_CYCLE_71 ) )
                    985:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle = LINE_END_CYCLE_71;
                    986:        }
                    987:        else                                                    /* switch to lo/med res */
                    988:        {
                    989:                /* In lo/med res, display start/end depends on the freq register in $ff820a */
                    990:                Video_Sync_SetDefaultStartEnd ( IoMem[0xff820a] & 2 , HblCounterVideo , LineCycles );
                    991:        }
                    992: 
                    993: 
1.1.1.11  root      994:        /* Remove left border : +26 bytes */
1.1.1.15  root      995:        /* This can be done with a hi/lo res switch or a hi/med res switch */
                    996:        if ( ( ShifterFrame.Res == 0x02 ) && ( Res == 0x00 )    /* switched from hi res to lo res */
1.1.1.16  root      997: //             && ( LineCycles >= 12 )                         /* switch back to low res should be after cycle 8 */
1.1.1.19  root      998:                && ( ( ShifterFrame.ResPosHi.LineCycles < 12 ) || ( ShifterFrame.ResPosHi.LineCycles >= 504 ) )         /* switch to hi between 504 and 8 */
1.1.1.15  root      999:                && ( LineCycles <= (LINE_START_CYCLE_71+28) )
1.1.1.17  root     1000:                && ( FrameCycles - ShifterFrame.ResPosHi.FrameCycles <= 32 ) )
1.1.1.15  root     1001:        {
1.1.1.20  root     1002:                if ( ( ( ConfigureParams.System.nMachineType == MACHINE_STE )   /* special case for 504/4 and 508/4 on STE -> add 20 bytes to left border */
1.1.1.17  root     1003:                        || ( ConfigureParams.System.nMachineType == MACHINE_MEGA_STE ) )
1.1.1.20  root     1004:                        && ( ( ( ShifterFrame.ResPosHi.LineCycles == 504 ) && ( LineCycles == 4 ) )
                   1005:                          || ( ( ShifterFrame.ResPosHi.LineCycles == 508 ) && ( LineCycles == 4 ) ) ) )
1.1.1.16  root     1006:                {
                   1007:                        ShifterFrame.ShifterLines[ HblCounterVideo ].BorderMask |= BORDERMASK_LEFT_OFF_2_STE;
                   1008:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle = LINE_START_CYCLE_71+16;        /* starts 16 pixels later */
1.1.1.17  root     1009:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayPixelShift = -8;            /* screen is shifted 8 pixels to the left */
1.1.1.16  root     1010:                        LOG_TRACE ( TRACE_VIDEO_BORDER_H , "detect remove left 2 ste %d<->%d\n" ,
                   1011:                                ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle , ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle );
                   1012:                }
                   1013:                else                                                            /* other case for STF/STE -> add 26 bytes */
                   1014:                {
                   1015:                        ShifterFrame.ShifterLines[ HblCounterVideo ].BorderMask |= BORDERMASK_LEFT_OFF;
                   1016:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle = LINE_START_CYCLE_71;
                   1017:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayPixelShift = -4;            /* screen is shifted 4 pixels to the left */
                   1018:                        LOG_TRACE ( TRACE_VIDEO_BORDER_H , "detect remove left %d<->%d\n" ,
                   1019:                                ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle , ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle );
                   1020:                }
1.1.1.15  root     1021:        }
                   1022: 
                   1023:        if ( ( ShifterFrame.Res == 0x02 ) && ( Res == 0x01 )    /* switched from hi res to med res */
                   1024:                && ( LineCycles <= (LINE_START_CYCLE_71+20) )
                   1025:                && ( FrameCycles - ShifterFrame.ResPosHi.FrameCycles <= 30 ) )
                   1026:        {
                   1027:                ShifterFrame.ShifterLines[ HblCounterVideo ].BorderMask |= BORDERMASK_LEFT_OFF_MED;     /* a later switch to low res might gives right scrolling */
                   1028:                /* By default, this line will be in med res, except if we detect hardware scrolling later */
                   1029:                ShifterFrame.ShifterLines[ HblCounterVideo ].BorderMask |= BORDERMASK_OVERSCAN_MED_RES | ( 2 << 20 );
                   1030:                ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle = LINE_START_CYCLE_71;
                   1031:                LOG_TRACE ( TRACE_VIDEO_BORDER_H , "detect remove left med %d<->%d\n" ,
                   1032:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle , ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle );
                   1033:        }
                   1034: 
                   1035:        /* Empty line switching res on STF : switch to hi res on cycle 28, then go back to med/lo res */
                   1036:        /* This creates a 0 byte line, the video counter won't change for this line */
                   1037:        else if ( ( ShifterFrame.Res == 0x02 )                  /* switched from hi res */
                   1038:                  && ( FrameCycles - ShifterFrame.ResPosHi.FrameCycles <= 16 )
                   1039:                  && ( ShifterFrame.ResPosHi.LineCycles == LINE_EMPTY_CYCLE_71_STF )
                   1040:                  && ( ConfigureParams.System.nMachineType == MACHINE_ST ) )
                   1041:        {
                   1042:                ShifterFrame.ShifterLines[ HblCounterVideo ].BorderMask |= BORDERMASK_EMPTY_LINE;
                   1043:                ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle = 0;
                   1044:                ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle = 0;
                   1045:                LOG_TRACE ( TRACE_VIDEO_BORDER_H , "detect empty line res %d<->%d\n" ,
                   1046:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle , ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle );
                   1047:        }
                   1048: 
                   1049:        /* Empty line switching res on STE (switch is 4 cycles later than on STF) */
                   1050:        else if ( ( ShifterFrame.Res == 0x02 )                  /* switched from hi res */
                   1051:                  && ( FrameCycles - ShifterFrame.ResPosHi.FrameCycles <= 16 )
                   1052:                  && ( ShifterFrame.ResPosHi.LineCycles == LINE_EMPTY_CYCLE_71_STE )
1.1.1.17  root     1053:                  && ( ( ConfigureParams.System.nMachineType == MACHINE_STE )
                   1054:                      || ( ConfigureParams.System.nMachineType == MACHINE_MEGA_STE ) ) )
1.1.1.15  root     1055:        {
                   1056:                ShifterFrame.ShifterLines[ HblCounterVideo ].BorderMask |= BORDERMASK_EMPTY_LINE;
                   1057:                ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle = 0;
                   1058:                ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle = 0;
                   1059:                LOG_TRACE ( TRACE_VIDEO_BORDER_H , "detect empty line res %d<->%d\n" ,
                   1060:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle , ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle );
                   1061:        }
                   1062: 
                   1063:        /* Empty line switching res on STF : switch to hi res just before the HBL then go back to lo/med res */
1.1.1.19  root     1064:        /* Next HBL will be an empty line (used in 'No Buddies Land' and 'Delirious Demo IV / NGC') */
1.1.1.15  root     1065:        else if ( ( ShifterFrame.Res == 0x02 )                  /* switched from hi res */
1.1.1.18  root     1066:                  && ( ( ShifterFrame.ResPosHi.LineCycles == 500-4 ) || ( ShifterFrame.ResPosHi.LineCycles == 500 ) )
1.1.1.15  root     1067:                  && ( LineCycles == 508 ) )
                   1068:        {
1.1.1.16  root     1069:                ShifterFrame.ShifterLines[ HblCounterVideo+1 ].BorderMask |= BORDERMASK_EMPTY_LINE;
                   1070:                ShifterFrame.ShifterLines[ HblCounterVideo+1 ].DisplayStartCycle = 0;
                   1071:                ShifterFrame.ShifterLines[ HblCounterVideo+1 ].DisplayEndCycle = 0;
1.1.1.18  root     1072:                BlankLines++;                                           /* no video signal at all for this line */
1.1.1.16  root     1073:                LOG_TRACE ( TRACE_VIDEO_BORDER_H , "detect empty line res 2 %d<->%d for nHBL=%d\n" ,
                   1074:                        ShifterFrame.ShifterLines[ HblCounterVideo+1 ].DisplayStartCycle , ShifterFrame.ShifterLines[ HblCounterVideo+1 ].DisplayEndCycle , nHBL+1 );
1.1.1.15  root     1075:        }
                   1076: 
                   1077:        /* Start right border near middle of the line : -106 bytes */ 
                   1078:        /* Switch to hi res just before the start of the right border in hi res, then go back to lo/mid res */
                   1079:        if ( ( ShifterFrame.Res == 0x02 )                               /* switched from hi res */
                   1080:                && ( ShifterFrame.ResPosHi.HBL == HblCounterVideo )     /* switch during the same line */
                   1081:                && ( ShifterFrame.ResPosHi.LineCycles <= LINE_END_CYCLE_71+4 )  /* switched to hi res before cycle 164 */
                   1082:                && ( LineCycles >= LINE_END_CYCLE_71+4 ) )              /* switch to lo res after cycle 164 */
                   1083:        {
                   1084:                ShifterFrame.ShifterLines[ HblCounterVideo ].BorderMask |= BORDERMASK_STOP_MIDDLE;
                   1085:                ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle = LINE_END_CYCLE_71;
                   1086:                LOG_TRACE ( TRACE_VIDEO_BORDER_H , "detect stop middle %d<->%d\n" ,
                   1087:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle , ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle );
                   1088:        }
                   1089: 
                   1090:        /* Remove right border a second time after removing it a first time. Display will */
                   1091:        /* stop at cycle 512 instead of 460. */
                   1092:        /* This removes left border on next line too (used in 'Enchanted Lands') */
1.1.1.16  root     1093:        /* If right border was not removed, then we will get an empty line for the next HBL (used in Beyond by Kruz) */
                   1094:        if ( ( ShifterFrame.Res == 0x02 )                               /* switched from hi res */
                   1095:                && ( LineCycles > LINE_END_CYCLE_50_2 )                 /* switch to low just after end of right border */
                   1096:                && ( ShifterFrame.ResPosHi.LineCycles <= LINE_END_CYCLE_50_2 )  /* switch to hi just before end of right border */
                   1097:                && ( FrameCycles - ShifterFrame.ResPosHi.FrameCycles <= 20 ) )
                   1098:        {
                   1099:                if ( ShifterFrame.ShifterLines[ HblCounterVideo ].BorderMask & BORDERMASK_RIGHT_OFF )           /* Enchanted Lands */
                   1100:                {
                   1101:                        ShifterFrame.ShifterLines[ HblCounterVideo ].BorderMask |= BORDERMASK_RIGHT_OFF_FULL;
                   1102:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle = LINE_END_CYCLE_FULL;
                   1103:                        ShifterFrame.ShifterLines[ HblCounterVideo+1 ].BorderMask |= BORDERMASK_LEFT_OFF;       /* no left border on next line */
                   1104:                        ShifterFrame.ShifterLines[ HblCounterVideo+1 ].DisplayStartCycle = LINE_START_CYCLE_71;
                   1105:                        LOG_TRACE ( TRACE_VIDEO_BORDER_H , "detect remove right full %d<->%d\n" ,
                   1106:                                ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle , ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle );
                   1107:                }
                   1108:                else                                                                    /* Pax Plax Parralax in Beyond by Kruz */
                   1109:                {
                   1110:                        ShifterFrame.ShifterLines[ HblCounterVideo+1 ].BorderMask = BORDERMASK_EMPTY_LINE;
                   1111:                        ShifterFrame.ShifterLines[ HblCounterVideo+1 ].DisplayStartCycle = 0;
                   1112:                        ShifterFrame.ShifterLines[ HblCounterVideo+1 ].DisplayEndCycle = 0;
                   1113:                        LOG_TRACE ( TRACE_VIDEO_BORDER_H , "detect empty line res 3 %d<->%d for nHBL=%d\n" ,
                   1114:                                ShifterFrame.ShifterLines[ HblCounterVideo+1 ].DisplayStartCycle , ShifterFrame.ShifterLines[ HblCounterVideo+1 ].DisplayEndCycle , nHBL+1 );
                   1115:                }
1.1.1.15  root     1116:        }
                   1117: 
                   1118:        /* If left border is opened and we switch to medium resolution during the next cycles, */
                   1119:        /* then we assume a med res overscan line instead of a low res overscan line. */
                   1120:        /* Note that in that case, the switch to med res can shift the display by 0-3 words */
1.1.1.11  root     1121:        /* Used in 'No Cooper' greetings by 1984 and 'Punish Your Machine' by Delta Force */
1.1.1.15  root     1122:        if ( ( ShifterFrame.ShifterLines[ HblCounterVideo ].BorderMask & BORDERMASK_LEFT_OFF )
                   1123:                && ( Res == 0x01 ) )
1.1.1.11  root     1124:        {
1.1.1.18  root     1125:                if ( ( LineCycles == LINE_LEFT_MED_CYCLE_1 )            /* 'No Cooper' timing */
                   1126:                  || ( LineCycles == LINE_LEFT_MED_CYCLE_1+16 ) )       /* 'No Cooper' timing while removing bottom border */
1.1.1.11  root     1127:                {
1.1.1.15  root     1128:                        LOG_TRACE ( TRACE_VIDEO_BORDER_H , "detect med res overscan offset 0 byte\n" );
                   1129:                        ShifterFrame.ShifterLines[ HblCounterVideo ].BorderMask |= BORDERMASK_OVERSCAN_MED_RES | ( 0 << 20 );
1.1.1.11  root     1130:                }
1.1.1.15  root     1131:                else if ( LineCycles == LINE_LEFT_MED_CYCLE_2 )         /* 'Best Part Of The Creation / PYM' timing */
1.1.1.11  root     1132:                {
1.1.1.15  root     1133:                        LOG_TRACE ( TRACE_VIDEO_BORDER_H , "detect med res overscan offset 2 bytes\n" );
                   1134:                        ShifterFrame.ShifterLines[ HblCounterVideo ].BorderMask |= BORDERMASK_OVERSCAN_MED_RES | ( 2 << 20 );
1.1.1.11  root     1135:                }
                   1136:        }
                   1137: 
1.1.1.15  root     1138:        /* If left border was opened with a hi/med res switch we need to check */
                   1139:        /* if the switch to low res can trigger a right hardware scrolling. */
                   1140:        /* We store the pixels count in DisplayPixelShift */
                   1141:        if ( ( ShifterFrame.ShifterLines[ HblCounterVideo ].BorderMask & BORDERMASK_LEFT_OFF_MED )
                   1142:                && ( Res == 0x00 ) && ( LineCycles <= LINE_SCROLL_1_CYCLE_50 ) )
1.1.1.11  root     1143:        {
1.1.1.15  root     1144:                /* The hi/med switch was a switch to do low res hardware scrolling, */
                   1145:                /* so we must cancel the med res overscan bit. */
                   1146:                ShifterFrame.ShifterLines[ HblCounterVideo ].BorderMask &= (~BORDERMASK_OVERSCAN_MED_RES);
1.1.1.11  root     1147: 
1.1.1.15  root     1148:                if ( LineCycles == LINE_SCROLL_13_CYCLE_50 )            /* cycle 20 */
1.1.1.11  root     1149:                {
1.1.1.15  root     1150:                        LOG_TRACE(TRACE_VIDEO_BORDER_H , "detect 13 pixels right scroll\n" );
                   1151:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayPixelShift = 13;
1.1.1.11  root     1152:                }
1.1.1.15  root     1153:                else if ( LineCycles == LINE_SCROLL_9_CYCLE_50 )        /* cycle 24 */
1.1.1.11  root     1154:                {
1.1.1.15  root     1155:                        LOG_TRACE(TRACE_VIDEO_BORDER_H , "detect 9 pixels right scroll\n" );
                   1156:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayPixelShift = 9;
1.1.1.11  root     1157:                }
1.1.1.15  root     1158:                else if ( LineCycles == LINE_SCROLL_5_CYCLE_50 )        /* cycle 28 */
1.1.1.11  root     1159:                {
1.1.1.15  root     1160:                        LOG_TRACE(TRACE_VIDEO_BORDER_H , "detect 5 pixels right scroll\n" );
                   1161:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayPixelShift = 5;
1.1.1.11  root     1162:                }
1.1.1.15  root     1163:                else if ( LineCycles == LINE_SCROLL_1_CYCLE_50 )        /* cycle 32 */
1.1.1.11  root     1164:                {
1.1.1.15  root     1165:                        LOG_TRACE(TRACE_VIDEO_BORDER_H , "detect 1 pixel right scroll\n" );
                   1166:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayPixelShift = 1;
1.1.1.11  root     1167:                }
                   1168:        }
                   1169: 
1.1.1.18  root     1170: #define SCROLL2_4PX
                   1171: #ifdef SCROLL2_4PX
                   1172:        /* Left border was removed with a hi/lo switch, then a med res switch was made */
                   1173:        /* Depending on the low res switch, the screen will be shifted as a low res overscan line */
                   1174:        /* This is a different method than the one used by ST Connexion with only 3 res switches */
                   1175:        /* (so we must cancel the med res overscan bit) */
                   1176:        if ( ( ShifterFrame.ShifterLines[ HblCounterVideo ].BorderMask & BORDERMASK_OVERSCAN_MED_RES )
                   1177:                && ( ( ShifterFrame.ShifterLines[ HblCounterVideo ].BorderMask & ( 0xf << 20 ) ) == 0 )
                   1178:                && ( Res == 0x00 ) && ( LineCycles <= 40 )  )
                   1179:        {
                   1180:                ShifterFrame.ShifterLines[ HblCounterVideo ].BorderMask &= (~BORDERMASK_OVERSCAN_MED_RES);      /* cancel mid res */
                   1181: 
                   1182:                if ( LineCycles == 28 )
                   1183:                {
                   1184:                        LOG_TRACE(TRACE_VIDEO_BORDER_H , "detect 13 pixels right scroll 2\n" );
                   1185:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayPixelShift = 13;
                   1186:                }
                   1187:                else if ( LineCycles == 32 )
                   1188:                {
                   1189:                        LOG_TRACE(TRACE_VIDEO_BORDER_H , "detect 9 pixels right scroll 2\n" );
                   1190:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayPixelShift = 9;
                   1191:                }
                   1192:                else if ( LineCycles == 36 )
                   1193:                {
                   1194:                        LOG_TRACE(TRACE_VIDEO_BORDER_H , "detect 5 pixels right scroll 2\n" );
                   1195:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayPixelShift = 5;
                   1196:                }
                   1197:                else if ( LineCycles == 40 )
                   1198:                {
                   1199:                        LOG_TRACE(TRACE_VIDEO_BORDER_H , "detect 1 pixel right scroll 2\n" );
                   1200:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayPixelShift = 1;
                   1201:                }
                   1202:        }
                   1203: #endif
                   1204: 
1.1.1.15  root     1205: 
                   1206:        /* Update HBL's position only if display has not reached pos LINE_START_CYCLE_50 */
                   1207:        /* and HBL interrupt was already handled at the beginning of this line. */
                   1208:        /* This also changes the number of cycles per line. */
                   1209:        if ( ( LineCycles <= LINE_START_CYCLE_50 ) && ( HblCounterVideo == nHBL ) )
                   1210:        {
                   1211:                nCyclesPerLine = Video_HBL_GetPos();
                   1212:                Video_AddInterruptHBL ( nCyclesPerLine );
                   1213:        }
                   1214: 
                   1215: 
                   1216:        /* Update Timer B's position */
                   1217:        LineTimerBCycle = Video_TimerB_GetPos ( HblCounterVideo );
                   1218:        Video_AddInterruptTimerB ( LineTimerBCycle );
                   1219: 
                   1220: 
                   1221:        ShifterFrame.Res = Res;
                   1222:        if ( Res == 0x02 )                                              /* high res */
                   1223:        {
                   1224:                ShifterFrame.ResPosHi.VBL = nVBLs;
                   1225:                ShifterFrame.ResPosHi.FrameCycles = FrameCycles;
                   1226:                ShifterFrame.ResPosHi.HBL = HblCounterVideo;
                   1227:                ShifterFrame.ResPosHi.LineCycles = LineCycles;
                   1228:        }
                   1229:        else if ( Res == 0x01 )                                         /* med res */
                   1230:        {
                   1231:                ShifterFrame.ResPosMed.VBL = nVBLs;
                   1232:                ShifterFrame.ResPosMed.FrameCycles = FrameCycles;
                   1233:                ShifterFrame.ResPosMed.HBL = HblCounterVideo;
                   1234:                ShifterFrame.ResPosMed.LineCycles = LineCycles;
                   1235:        }
                   1236:        else                                                            /* low res */
                   1237:        {
                   1238:                ShifterFrame.ResPosLo.VBL = nVBLs;
                   1239:                ShifterFrame.ResPosLo.FrameCycles = FrameCycles;
                   1240:                ShifterFrame.ResPosLo.HBL = HblCounterVideo;
                   1241:                ShifterFrame.ResPosLo.LineCycles = LineCycles;
                   1242:        }
                   1243: }
                   1244: 
                   1245: 
                   1246: 
                   1247: /*-----------------------------------------------------------------------*/
                   1248: /**
                   1249:  * Set some default values for DisplayStartCycle/DisplayEndCycle
                   1250:  * when changing frequency in lo/med res (testing orders are important
                   1251:  * because the line can already have some borders changed).
                   1252:  * This is necessary as some freq changes can modify start/end
                   1253:  * even if they're not made at the exact borders' positions.
                   1254:  * These values will be modified later if some borders are changed.
                   1255:  */
                   1256: static void    Video_Sync_SetDefaultStartEnd ( Uint8 Freq , int HblCounterVideo , int LineCycles )
                   1257: {
                   1258:        if ( Freq == 0x02 )                                     /* switch to 50 Hz */
                   1259:        {
                   1260:                if ( ( LineCycles <= ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle )   /* start could be 0,52,56 */
                   1261:                  && ( ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle == LINE_START_CYCLE_60 ) )
                   1262:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle = LINE_START_CYCLE_50;
                   1263: 
                   1264:                if ( ( LineCycles <= ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle )     /* end could be 160,372,376,460 */
                   1265:                  && ( ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle < LINE_END_CYCLE_50 ) )
                   1266:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle = LINE_END_CYCLE_50;
                   1267:        }
                   1268: 
                   1269:        else                                                    /* switch to 60 Hz */
                   1270:        {
                   1271:                if ( LineCycles < ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle )      /* start could be 0,52,56 */
                   1272:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle = LINE_START_CYCLE_60;
                   1273: 
                   1274:                if ( ( LineCycles < ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle )      /* end could be 160,372,376,460 */
                   1275:                  && ( ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle <= LINE_END_CYCLE_50 ) )
                   1276:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle = LINE_END_CYCLE_60;
                   1277:        }
                   1278: 
1.1.1.21  root     1279: //fprintf ( stderr , "sync default pos %d %d %d\n", HblCounterVideo , ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle , ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle );
1.1.1.11  root     1280: }
                   1281: 
                   1282: 
                   1283: /*-----------------------------------------------------------------------*/
                   1284: /**
                   1285:  * Write to VideoSync (0xff820a), Hz setting
                   1286:  */
1.1.1.15  root     1287: void Video_Sync_WriteByte ( void )
1.1       root     1288: {
1.1.1.15  root     1289:        int FrameCycles, HblCounterVideo, LineCycles;
                   1290:        Uint8 Freq;
1.1.1.11  root     1291: 
                   1292: 
1.1.1.15  root     1293:        if ( bUseVDIRes )
                   1294:                return;                                         /* no 50/60 Hz freq in VDI mode */
1.1.1.11  root     1295: 
                   1296: 
1.1.1.15  root     1297:        /* We're only interested in bit 1 (50/60Hz) */
                   1298:        Freq = IoMem[0xff820a] & 2;
1.1.1.11  root     1299: 
1.1.1.15  root     1300:        Video_GetPosition_OnWriteAccess ( &FrameCycles , &HblCounterVideo , &LineCycles );
                   1301: 
                   1302:        LOG_TRACE(TRACE_VIDEO_SYNC ,"sync=0x%2.2X video_cyc_w=%d line_cyc_w=%d @ nHBL=%d/video_hbl_w=%d pc=%x instr_cyc=%d\n",
                   1303:                       Freq, FrameCycles, LineCycles, nHBL, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles );
1.1.1.11  root     1304: 
1.1.1.13  root     1305:        /* Ignore consecutive writes of the same value */
1.1.1.15  root     1306:        if ( Freq == ShifterFrame.Freq )
1.1.1.13  root     1307:                return;                                         /* do nothing */
                   1308: 
1.1.1.15  root     1309:        /* Ignore freq changes if we are in high res */
                   1310:        /* 2009/04/26 : don't ignore for now (see ST Cnx in Punish Your Machine) */
                   1311: //     if ( ShifterFrame.Res == 0x02 )
                   1312: //             return;                                         /* do nothing */
                   1313: 
                   1314:        /* Set some default values for DisplayStartCycle/DisplayEndCycle before checking for border removal */
                   1315:        Video_Sync_SetDefaultStartEnd ( Freq , HblCounterVideo , LineCycles );
                   1316: 
                   1317: 
                   1318:        if ( ( ShifterFrame.Freq == 0x00 ) && ( Freq == 0x02 )  /* switched from 60 Hz to 50 Hz ? */
                   1319: //             && ( ShifterFrame.FreqPos60.VBL == nVBLs )      /* switched during the same VBL */
                   1320:                && ( HblCounterVideo >= nStartHBL )             /* only if display is on */
1.1.1.18  root     1321:                && ( HblCounterVideo < nEndHBL + BlankLines ) ) /* only if display is on */
1.1.1.15  root     1322:        {
1.1.1.18  root     1323:                /* Blank line switching freq on STF : switch to 60 Hz on cycle 28, then go back to 50 Hz on cycle 36 */
                   1324:                /* This creates a blank line where no signal is displayed, but the video counter will still change for this line */
                   1325:                /* This blank line can be combined with left/right border changes */
                   1326:                if ( ( FrameCycles - ShifterFrame.FreqPos60.FrameCycles <= 16 )
                   1327:                        && ( ShifterFrame.FreqPos60.LineCycles == LINE_EMPTY_CYCLE_71_STF )
                   1328:                        && ( ConfigureParams.System.nMachineType == MACHINE_ST ) )
                   1329:                {
                   1330:                        ShifterFrame.ShifterLines[ HblCounterVideo ].BorderMask |= BORDERMASK_BLANK_LINE;
1.1.1.22! root     1331:                        LOG_TRACE ( TRACE_VIDEO_BORDER_H , "detect blank line freq stf\n"  );
1.1.1.18  root     1332:                }
                   1333: 
1.1.1.15  root     1334:                /* Add 2 bytes to left border : switch to 60 Hz before LINE_START_CYCLE_60 to force an early start */
                   1335:                /* of the DE signal, then go back to 50 Hz. Note that depending on where the 50 Hz switch is made */
                   1336:                /* the HBL signal will be at position 508 (60 Hz line) or 512 (50 Hz line) */
1.1.1.22! root     1337:                /* Obtaining a +2 line with 512 cycles requires a 2 cycles precision and is "wake up" state dependent : */
        !          1338:                /*   - On STF, switch must be on cycles 36/56 or 36/54 (depending on wake up state) */
        !          1339:                /*   - On STE, switch can be on cycles 36/56 or 36/54 (no wake up state in STE) */
        !          1340:                /* TODO : we should change HBL signal to be on cycles 508 or 512 (it will always be 512 for now) */
1.1.1.15  root     1341:                if ( ( ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle == LINE_START_CYCLE_60 )
                   1342:                        && ( LineCycles >= LINE_START_CYCLE_50 )        /* The line started in 60 Hz and continues in 50 Hz */
                   1343:                        && ( LineCycles <= ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle ) )     /* change when line is active */
1.1.1.11  root     1344:                {
1.1.1.22! root     1345:                        /* [FIXME] 'Panic' by Paulo Simoes, dont' trigger left+2 (need 2 cycles precision) */
        !          1346:                        /* The switch to 50 Hz on line 34 cycle 56 should just start a normal 50 Hz line, not a left+2 */
        !          1347:                        /* For now, we detect that we're running 'Panic' and if so we don't do left+2 (ugly hack...) */
        !          1348:                        if ( ( STMemory_ReadLong ( M68000_GetPC() ) == 0x4e7352b8 )
        !          1349:                          && ( STMemory_ReadLong ( M68000_GetPC()+4 ) == 0x04664e73 )
        !          1350:                          && ( HblCounterVideo == 34 ) && ( LineCycles == 56 ) )
        !          1351:                        {
        !          1352:                                ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle = LINE_START_CYCLE_50;
        !          1353:                        }
        !          1354: 
        !          1355:                        /* Normal case where left+2 should be made */
        !          1356:                        else
        !          1357:                        {
        !          1358:                          ShifterFrame.ShifterLines[ HblCounterVideo ].BorderMask |= BORDERMASK_LEFT_PLUS_2;
        !          1359:                          ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle = LINE_END_CYCLE_50;
        !          1360:                          LOG_TRACE ( TRACE_VIDEO_BORDER_H , "detect left+2 %d<->%d\n" ,
1.1.1.15  root     1361:                                ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle , ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle );
1.1.1.22! root     1362:                        }
1.1.1.11  root     1363:                }
                   1364: 
1.1.1.22! root     1365:                /* Empty line switching freq on STF : start the line in 50 Hz, change to 60 Hz at the exact place */
1.1.1.15  root     1366:                /* where display is enabled in 50 Hz, then go back to 50 Hz. */
                   1367:                /* Due to 4 cycles precision instead of 2, we must accept a 60 Hz switch at pos 56 or 56+4 */
                   1368:                else if ( ( FrameCycles - ShifterFrame.FreqPos60.FrameCycles <= 24 )
                   1369:                        && ( ( ShifterFrame.FreqPos60.LineCycles == LINE_START_CYCLE_50 ) || ( ShifterFrame.FreqPos60.LineCycles == LINE_START_CYCLE_50+4 ) )
1.1.1.22! root     1370:                        && ( LineCycles > LINE_START_CYCLE_50 )
        !          1371:                        && ( ConfigureParams.System.nMachineType == MACHINE_ST ) )
        !          1372:                {
        !          1373:                        ShifterFrame.ShifterLines[ HblCounterVideo ].BorderMask |= BORDERMASK_EMPTY_LINE;
        !          1374:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle = 0;
        !          1375:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle = 0;
        !          1376:                        LOG_TRACE ( TRACE_VIDEO_BORDER_H , "detect empty line freq stf %d<->%d\n" ,
        !          1377:                                ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle , ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle );
        !          1378:                }
        !          1379: 
        !          1380:                /* Empty line switching freq on STE : similar to STF above, but doesn't require a 2 cycles precision */
        !          1381:                /* The switches are made at cycles 40/52 */
        !          1382:                else if ( ( FrameCycles - ShifterFrame.FreqPos60.FrameCycles <= 24 )
        !          1383:                        && ( ShifterFrame.FreqPos60.LineCycles == 40 )
        !          1384:                        && ( LineCycles == LINE_START_CYCLE_60 )
        !          1385:                        && ( ConfigureParams.System.nMachineType == MACHINE_STE ) )
1.1.1.15  root     1386:                {
                   1387:                        ShifterFrame.ShifterLines[ HblCounterVideo ].BorderMask |= BORDERMASK_EMPTY_LINE;
                   1388:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle = 0;
                   1389:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle = 0;
1.1.1.22! root     1390:                        LOG_TRACE ( TRACE_VIDEO_BORDER_H , "detect empty line freq ste %d<->%d\n" ,
1.1.1.15  root     1391:                                ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle , ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle );
1.1.1.11  root     1392:                }
1.1.1.15  root     1393: 
1.1.1.22! root     1394: 
1.1.1.15  root     1395:                /* Remove 2 bytes to the right : start the line in 50 Hz (pos 0 or 56), change to 60 Hz before the position */
                   1396:                /* where display is disabled in 60 Hz, then go back to 50 Hz */
                   1397:                if ( ( LineCycles > LINE_END_CYCLE_60 )                                 /* back to 50 Hz after end of 60 Hz line */
                   1398:                        && ( ShifterFrame.ShifterLines[ nHBL ].DisplayStartCycle != LINE_START_CYCLE_60 )       /* start could be 0 or 56 */
                   1399:                        && ( ShifterFrame.ShifterLines[ nHBL ].DisplayEndCycle == LINE_END_CYCLE_60 ) )
                   1400:                {
                   1401:                        ShifterFrame.ShifterLines[ HblCounterVideo ].BorderMask |= BORDERMASK_RIGHT_MINUS_2;
                   1402:                        LOG_TRACE ( TRACE_VIDEO_BORDER_H , "detect right-2 %d<->%d\n" ,
                   1403:                                ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle , ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle );
                   1404:                }
                   1405: 
1.1.1.11  root     1406:        }
                   1407: 
1.1.1.15  root     1408: 
                   1409:        if ( ( ShifterFrame.Freq == 0x02 && Freq == 0x00 )      /* switched from 50 Hz to 60 Hz ? */
                   1410:                && ( HblCounterVideo >= nStartHBL )             /* only if display is on */
1.1.1.18  root     1411:                && ( HblCounterVideo < nEndHBL + BlankLines ) ) /* only if display is on */
1.1.1.15  root     1412:        {
                   1413:                /* remove right border, display 44 bytes more : switch to 60 Hz at the position where */
1.1.1.21  root     1414:                /* the line ends in 50 Hz. Some programs don't switch back to 50 Hz immediately */
1.1.1.15  root     1415:                /* (sync screen in SNY II), so we just check if freq changes to 60 Hz at the position where line should end in 50 Hz */
                   1416:                if ( ( LineCycles == LINE_END_CYCLE_50 )
                   1417:                        && ( ShifterFrame.ShifterLines[ nHBL ].DisplayEndCycle == LINE_END_CYCLE_50 ) )
                   1418:                {
                   1419:                        ShifterFrame.ShifterLines[ HblCounterVideo ].BorderMask |= BORDERMASK_RIGHT_OFF;
1.1.1.16  root     1420:                        ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle = LINE_END_CYCLE_NO_RIGHT;
1.1.1.15  root     1421:                        LOG_TRACE ( TRACE_VIDEO_BORDER_H , "detect remove right %d<->%d\n" ,
                   1422:                                ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayStartCycle , ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle );
1.1.1.11  root     1423:                }
                   1424:        }
                   1425: 
                   1426: 
                   1427:        /* Store cycle position of freq 50/60 to check for top/bottom border removal in Video_EndHBL. */
1.1.1.15  root     1428:        if ( Freq == 0x02 )                                             /* switch to 50 Hz */
1.1.1.11  root     1429:        {
1.1.1.13  root     1430:                if ( ( HblCounterVideo < VIDEO_START_HBL_50HZ )         /* nStartHBL can change only if display is not ON yet */
                   1431:                        && ( OverscanMode & OVERSCANMODE_TOP ) == 0 )   /* update only if top was not removed */
                   1432:                        nStartHBL = VIDEO_START_HBL_50HZ;
1.1.1.11  root     1433: 
1.1.1.13  root     1434:                if ( ( HblCounterVideo < VIDEO_END_HBL_50HZ )           /* nEndHBL can change only if display is not OFF yet */
1.1.1.11  root     1435:                        && ( OverscanMode & OVERSCANMODE_BOTTOM ) == 0 )        /* update only if bottom was not removed */
1.1.1.13  root     1436:                        nEndHBL = VIDEO_END_HBL_50HZ;                           /* 263 */
1.1.1.11  root     1437:        }
1.1.1.15  root     1438:        else if ( Freq == 0x00 )                                        /* switch to 60 Hz */
1.1.1.11  root     1439:        {
1.1.1.13  root     1440:                if ( ( HblCounterVideo < VIDEO_START_HBL_60HZ-1 )       /* nStartHBL can change only if display is not ON yet */
1.1.1.15  root     1441:                        || ( ( HblCounterVideo == VIDEO_START_HBL_60HZ-1 ) && ( LineCycles <= LineRemoveTopCycle ) ) )
1.1.1.13  root     1442:                        nStartHBL = VIDEO_START_HBL_60HZ;
1.1.1.11  root     1443: 
1.1.1.13  root     1444:                if ( ( HblCounterVideo < VIDEO_END_HBL_60HZ )           /* nEndHBL can change only if display is not OFF yet */
1.1.1.11  root     1445:                        && ( OverscanMode & OVERSCANMODE_BOTTOM ) == 0 )        /* update only if bottom was not removed */
1.1.1.13  root     1446:                        nEndHBL = VIDEO_END_HBL_60HZ;                           /* 234 */
                   1447:        }
                   1448: 
1.1.1.15  root     1449: 
1.1.1.13  root     1450:        /* If the frequence changed, we need to update the EndLine interrupt */
1.1.1.21  root     1451:        /* so that it happens TIMERB_VIDEO_CYCLE_OFFSET cycles after the current DisplayEndCycle.*/
1.1.1.13  root     1452:        /* We check if the change affects the current line or the next one. */
1.1.1.15  root     1453:        /* We also need to check if the HBL interrupt and nCyclesPerLine need */
                   1454:        /* to be updated first. */
                   1455:        if ( Freq != ShifterFrame.Freq )
                   1456:        {
                   1457:                /* Update HBL's position only if display has not reached pos LINE_START_CYCLE_50 */
                   1458:                /* and HBL interrupt was already handled at the beginning of this line. */
                   1459:                /* This also changes the number of cycles per line. */
                   1460:                if ( ( LineCycles <= LINE_START_CYCLE_50 ) && ( HblCounterVideo == nHBL ) )
                   1461:                {
1.1.1.22! root     1462:                        int     CyclesPerLine_old = nCyclesPerLine;
        !          1463: 
1.1.1.15  root     1464:                        nCyclesPerLine = Video_HBL_GetPos();
                   1465:                        Video_AddInterruptHBL ( nCyclesPerLine );
1.1.1.22! root     1466: 
        !          1467:                        /* In case we're mixing 50 Hz (512 cycles) and 60 Hz (508 cycles) lines on the same screen, */
        !          1468:                        /* we must update the position where the next VBL will happen (instead of the initial value in CyclesPerVBL) */
        !          1469:                        /* We check if number of cycles per line changes, and if so, we update the VBL's position */
        !          1470:                        if ( CyclesPerLine_old != nCyclesPerLine )
        !          1471:                        {
        !          1472:                                CyclesPerVBL += ( nCyclesPerLine - CyclesPerLine_old );         /* +4 or -4 */
        !          1473:                                CycInt_ModifyInterrupt ( nCyclesPerLine - CyclesPerLine_old , INT_CPU_CYCLE , INTERRUPT_VIDEO_VBL );
        !          1474:                        }
1.1.1.15  root     1475:                }
                   1476: 
                   1477:                /* Update Timer B's position */
                   1478:                LineTimerBCycle = Video_TimerB_GetPos ( HblCounterVideo );
                   1479:                Video_AddInterruptTimerB ( LineTimerBCycle );
                   1480:        }
                   1481: 
                   1482: 
                   1483:        ShifterFrame.Freq = Freq;
                   1484:        if ( Freq == 0x02 )                                             /* 50 Hz */
                   1485:        {
                   1486:                ShifterFrame.FreqPos50.VBL = nVBLs;
                   1487:                ShifterFrame.FreqPos50.FrameCycles = FrameCycles;
                   1488:                ShifterFrame.FreqPos50.HBL = HblCounterVideo;
                   1489:                ShifterFrame.FreqPos50.LineCycles = LineCycles;
                   1490:        }
                   1491:        else
                   1492:        {
                   1493:                ShifterFrame.FreqPos60.VBL = nVBLs;
                   1494:                ShifterFrame.FreqPos60.FrameCycles = FrameCycles;
                   1495:                ShifterFrame.FreqPos60.HBL = HblCounterVideo;
                   1496:                ShifterFrame.FreqPos60.LineCycles = LineCycles;
                   1497:        }
                   1498: }
                   1499: 
                   1500: 
                   1501: /*-----------------------------------------------------------------------*/
                   1502: /**
                   1503:  * Compute the cycle position where the HBL should happen on each line.
                   1504:  * In low/med res, the position depends on the video frequency (50/60 Hz)
                   1505:  * In high res, the position is always the same.
                   1506:  * This position also gives the number of CPU cycles per video line.
                   1507:  */
                   1508: static int Video_HBL_GetPos ( void )
                   1509: {
                   1510:        int Pos;
                   1511: 
                   1512:        if ( ( IoMem_ReadByte ( 0xff8260 ) & 3 ) == 2 )         /* hi res */
                   1513:                Pos = CYCLES_PER_LINE_71HZ;
                   1514: 
                   1515:        else                                                    /* low res or med res */
                   1516:        {
                   1517:                if ( IoMem_ReadByte ( 0xff820a ) & 2 )          /* 50 Hz, pos 512 */
                   1518:                        Pos = CYCLES_PER_LINE_50HZ;
                   1519:                else                                            /* 60 Hz, pos 508 */
                   1520:                        Pos = CYCLES_PER_LINE_60HZ;
                   1521:        }
                   1522: 
                   1523:        return Pos;
                   1524: }
                   1525: 
                   1526: 
                   1527: /*-----------------------------------------------------------------------*/
                   1528: /**
                   1529:  * Compute the cycle position where the timer B should happen on each
                   1530:  * visible line.
                   1531:  * We compute Timer B position for the given LineNumber, using start/end
                   1532:  * display cycles from ShifterLines[ LineNumber ].
                   1533:  * The position depends on the start of line / end of line positions
                   1534:  * (which depend on the current frequency / border tricks) and
                   1535:  * on the value of the bit 3 in the MFP's AER.
                   1536:  * If bit is 0, timer B will count end of line events (usual case),
                   1537:  * but if bit is 1, timer B will count start of line events (eg Seven Gates Of Jambala)
                   1538:  */
                   1539: int Video_TimerB_GetPos ( int LineNumber )
                   1540: {
                   1541:        int Pos;
                   1542: 
                   1543:        if ( ( IoMem[0xfffa03] & ( 1 << 3 ) ) == 0 )                    /* we're counting end of line events */
                   1544:        {
                   1545:                Pos = ShifterFrame.ShifterLines[ LineNumber ].DisplayEndCycle + TIMERB_VIDEO_CYCLE_OFFSET;
                   1546:        }
                   1547:        else                                                            /* we're counting start of line events */
                   1548:        {
                   1549:                Pos = ShifterFrame.ShifterLines[ LineNumber ].DisplayStartCycle + TIMERB_VIDEO_CYCLE_OFFSET;
                   1550:        }
                   1551: 
1.1.1.18  root     1552: //fprintf ( stderr , "timerb pos=%d\n" , Pos );
                   1553:        return Pos;
                   1554: }
                   1555: 
                   1556: 
                   1557: /*-----------------------------------------------------------------------*/
                   1558: /**
                   1559:  * Compute the default cycle position where the timer B should happen
                   1560:  * on the next line, when restarting the INTERRUPT_VIDEO_ENDLINE handler.
                   1561:  * In low/med res, the position depends on the video frequency (50/60 Hz)
                   1562:  * In high res, the position is always the same.
                   1563:  */
                   1564: int Video_TimerB_GetDefaultPos ( void )
                   1565: {
                   1566:        int Pos;
                   1567: 
                   1568:        if ( ( IoMem[0xfffa03] & ( 1 << 3 ) ) == 0 )                    /* we're counting end of line events */
                   1569:        {
                   1570:                if ( ( IoMem_ReadByte ( 0xff8260 ) & 3 ) == 2 )         /* hi res */
                   1571:                        Pos = LINE_END_CYCLE_71;
                   1572: 
                   1573:                else                                                    /* low res or med res */
                   1574:                {
                   1575:                        if ( IoMem_ReadByte ( 0xff820a ) & 2 )          /* 50 Hz, pos 376 */
                   1576:                                Pos = LINE_END_CYCLE_50;
                   1577:                        else                                            /* 60 Hz, pos 372 */
                   1578:                                Pos = LINE_END_CYCLE_60;
                   1579:                }
                   1580:        }
                   1581:        else                                                            /* we're counting start of line events */
                   1582:        {
                   1583:                if ( ( IoMem_ReadByte ( 0xff8260 ) & 3 ) == 2 )         /* hi res */
                   1584:                        Pos = LINE_START_CYCLE_71;
                   1585: 
                   1586:                else                                                    /* low res or med res */
                   1587:                {
                   1588:                        if ( IoMem_ReadByte ( 0xff820a ) & 2 )          /* 50 Hz, pos 56 */
                   1589:                                Pos = LINE_START_CYCLE_50;
                   1590:                        else                                            /* 60 Hz, pos 52 */
                   1591:                                Pos = LINE_START_CYCLE_60;
                   1592:                }
                   1593:        }
                   1594: 
                   1595:        Pos += TIMERB_VIDEO_CYCLE_OFFSET;
                   1596: 
                   1597: //fprintf ( stderr , "timerb default pos=%d\n" , Pos );
1.1.1.15  root     1598:        return Pos;
                   1599: }
                   1600: 
                   1601: 
                   1602: /*-----------------------------------------------------------------------*/
                   1603: /**
                   1604:  * HBL interrupt : this occurs at the end of every line, on cycle 512 (in 50 Hz)
                   1605:  * It takes 56 cycles to handle the 68000's exception.
                   1606:  */
                   1607: void Video_InterruptHandler_HBL ( void )
                   1608: {
                   1609:        int FrameCycles = Cycles_GetCounter(CYCLES_COUNTER_VIDEO);
                   1610:        int PendingCyclesOver;
                   1611:        int NewHBLPos;
                   1612: 
                   1613:        /* How many cycle was this HBL delayed (>= 0) */
                   1614:        PendingCyclesOver = -INT_CONVERT_FROM_INTERNAL ( PendingInterruptCount , INT_CPU_CYCLE );
                   1615: 
                   1616:        /* Remove this interrupt from list and re-order */
1.1.1.16  root     1617:        CycInt_AcknowledgeInterrupt();
1.1.1.15  root     1618: 
1.1.1.17  root     1619:        /* Videl Vertical counter increment (To be removed when Videl emulation is finished) */
                   1620:        /* VFC is incremented every half line, here, we increment it every line (should be completed) */
                   1621:        if (ConfigureParams.System.nMachineType == MACHINE_FALCON) {
                   1622:                vfc_counter += 1;
                   1623:        }
                   1624: 
                   1625:        
1.1.1.15  root     1626:        /* Increment the hbl jitter index */
                   1627:        HblJitterIndex++;
1.1.1.20  root     1628:        HblJitterIndex %= HBL_JITTER_ARRAY_SIZE;
1.1.1.15  root     1629:        
                   1630:        LOG_TRACE ( TRACE_VIDEO_HBL , "HBL %d video_cyc=%d pending_cyc=%d jitter=%d\n" ,
                   1631:                       nHBL , FrameCycles , PendingCyclesOver , HblJitterArray[ HblJitterIndex ] );
                   1632: 
                   1633:        /* Default cycle position for next HBL */
                   1634:        NewHBLPos = Video_HBL_GetPos();
                   1635: 
                   1636:        /* Generate new HBL, if need to - there are 313 HBLs per frame in 50 Hz */
                   1637:        if (nHBL < nScanlinesPerFrame-1)
                   1638:                Video_AddInterruptHBL ( NewHBLPos );
                   1639: 
1.1.1.22! root     1640: 
        !          1641:        /* In case we're mixing 50 Hz (512 cycles) and 60 Hz (508 cycles) lines on the same screen, */
        !          1642:        /* we must update the position where the next VBL will happen (instead of the initial value in CyclesPerVBL) */
        !          1643:        /* During a 50 Hz screen, each 60 Hz line will make the VBL happen 4 cycles earlier */
        !          1644:         if ( ( nScanlinesPerFrame == SCANLINES_PER_FRAME_50HZ )
        !          1645:          && ( NewHBLPos == CYCLES_PER_LINE_60HZ ) )
        !          1646:        {
        !          1647:                CyclesPerVBL -= 4;
        !          1648:                CycInt_ModifyInterrupt ( -4 , INT_CPU_CYCLE , INTERRUPT_VIDEO_VBL );
        !          1649:        }
        !          1650:        /* During a 60 Hz screen, each 50 Hz line will make the VBL happen 4 cycles later */
        !          1651:         else if ( ( nScanlinesPerFrame == SCANLINES_PER_FRAME_60HZ )
        !          1652:          && ( NewHBLPos == CYCLES_PER_LINE_50HZ ) )
        !          1653:        {
        !          1654:                CyclesPerVBL += 4;
        !          1655:                CycInt_ModifyInterrupt ( 4 , INT_CPU_CYCLE , INTERRUPT_VIDEO_VBL );
        !          1656:        }
        !          1657: 
        !          1658: 
1.1.1.21  root     1659:        /* Print traces if pending HBL bit changed just before IACK when HBL interrupt is allowed */
                   1660:        if ( ( CPU_IACK == true ) && ( regs.intmask < 2 ) )
1.1.1.15  root     1661:        {
1.1.1.21  root     1662:                if ( pendingInterrupts & ( 1 << 2 ) )
                   1663:                {
                   1664:                        LOG_TRACE ( TRACE_VIDEO_HBL , "HBL %d, pending set again just before iack, skip one HBL interrupt VBL=%d video_cyc=%d pending_cyc=%d jitter=%d\n" ,
                   1665:                                nHBL , nVBLs , FrameCycles , PendingCyclesOver , VblJitterArray[ VblJitterIndex ] );
                   1666:                }
                   1667:                else
                   1668:                {
                   1669:                        LOG_TRACE ( TRACE_VIDEO_HBL , "HBL %d, new pending HBL set just before iack VBL=%d video_cyc=%d pending_cyc=%d jitter=%d\n" ,
                   1670:                                nHBL , nVBLs , FrameCycles , PendingCyclesOver , VblJitterArray[ VblJitterIndex ] );
                   1671:                }
1.1.1.15  root     1672:        }
                   1673: 
1.1.1.21  root     1674:        /* Set pending bit for HBL interrupt in the CPU IPL */
                   1675:        M68000_Exception(EXCEPTION_HBLANK , M68000_EXC_SRC_AUTOVEC);    /* Horizontal blank interrupt, level 2 */
                   1676: 
1.1.1.15  root     1677: 
                   1678:        Video_EndHBL();                                 /* Check some borders removal and copy line to display buffer */
                   1679: 
1.1.1.17  root     1680:        DmaSnd_STE_HBL_Update();                        /* Update STE DMA sound if needed */
                   1681: 
1.1.1.22! root     1682:        /* TEMP IPF */
        !          1683:        IPF_Emulate();
        !          1684:        /* TEMP IPF */
        !          1685: 
1.1.1.15  root     1686:        nHBL++;                                         /* Increase HBL count */
                   1687: 
                   1688:        if (nHBL < nScanlinesPerFrame)
                   1689:        {
                   1690:                /* Update start cycle for next HBL */
                   1691:                ShifterFrame.ShifterLines[ nHBL ].StartCycle = FrameCycles - PendingCyclesOver;
                   1692:                LOG_TRACE(TRACE_VIDEO_HBL, "HBL %d start=%d %x\n", nHBL,
                   1693:                          ShifterFrame.ShifterLines[nHBL].StartCycle, ShifterFrame.ShifterLines[nHBL].StartCycle);
                   1694: 
                   1695:                /* Setup next HBL */
                   1696:                Video_StartHBL();
                   1697:        }
                   1698: }
                   1699: 
                   1700: 
                   1701: /*-----------------------------------------------------------------------*/
                   1702: /**
                   1703:  * Check at end of each HBL to see if any Shifter hardware tricks have been attempted
                   1704:  * and copy the line to the screen buffer.
                   1705:  * This is the place to check if top/bottom border were removed, as well as if some
                   1706:  * left/right border changes were not validated before.
                   1707:  * NOTE : the tests must be made with nHBL in ascending order.
                   1708:  */
                   1709: static void Video_EndHBL(void)
                   1710: {
                   1711:        //
                   1712:        // Handle top/bottom borders removal when switching freq
                   1713:        //
                   1714: 
                   1715:        /* Remove top border if the switch to 60 Hz was made during this vbl before cycle       */
1.1.1.21  root     1716:        /* LineRemoveTopCycle on line 33 and if the switch to 50 Hz has not yet occurred or     */
                   1717:        /* occurred before the 60 Hz or occurred after cycle LineRemoveTopCycle on line 33.     */
1.1.1.15  root     1718:        if ( ( nHBL == VIDEO_START_HBL_60HZ-1 )                         /* last HBL before first line of a 60 Hz screen */
                   1719:                && ( ShifterFrame.FreqPos60.VBL == nVBLs )              /* switch to 60 Hz during this VBL */
1.1.1.20  root     1720:                && ( ( ShifterFrame.FreqPos60.HBL < nHBL )
                   1721:                    || ( ( ShifterFrame.FreqPos60.HBL == nHBL ) && ( ShifterFrame.FreqPos60.LineCycles <= LineRemoveTopCycle ) ) )
1.1.1.15  root     1722:                && (   ( ShifterFrame.FreqPos50.VBL < nVBLs )
                   1723:                    || ( ShifterFrame.FreqPos50.FrameCycles < ShifterFrame.FreqPos60.FrameCycles )
                   1724:                    || ( ShifterFrame.FreqPos50.HBL > nHBL )
1.1.1.21  root     1725:                    || ( ( ShifterFrame.FreqPos50.HBL == nHBL ) && ( ShifterFrame.FreqPos50.LineCycles > LineRemoveTopCycle ) ) ) )
1.1.1.15  root     1726:        {
                   1727:                /* Top border */
                   1728:                LOG_TRACE ( TRACE_VIDEO_BORDER_V , "detect remove top\n" );
                   1729:                OverscanMode |= OVERSCANMODE_TOP;       /* Set overscan bit */
                   1730:                nStartHBL = VIDEO_START_HBL_60HZ;       /* New start screen line */
                   1731:                pHBLPaletteMasks -= OVERSCAN_TOP;       // FIXME useless ?
                   1732:                pHBLPalettes -= OVERSCAN_TOP;   // FIXME useless ?
                   1733:        }
                   1734: 
                   1735:        /* Remove bottom border for a 60 Hz screen (tests are similar to the ones for top border) */
1.1.1.18  root     1736:        else if ( ( nHBL == VIDEO_END_HBL_60HZ + BlankLines - 1 )       /* last displayed line in 60 Hz */
1.1.1.15  root     1737:                && ( nStartHBL == VIDEO_START_HBL_60HZ )                /* screen started in 60 Hz */
                   1738:                && ( ( OverscanMode & OVERSCANMODE_TOP ) == 0 )         /* and top border was not removed : this screen is only 60 Hz */
                   1739:                && ( ShifterFrame.FreqPos50.VBL == nVBLs )              /* switch to 50 Hz during this VBL */
1.1.1.20  root     1740:                && ( ( ShifterFrame.FreqPos50.HBL < nHBL )
1.1.1.22! root     1741:                    || ( ( ShifterFrame.FreqPos50.HBL == nHBL ) && ( ShifterFrame.FreqPos50.LineCycles <= LineRemoveBottomCycle-4 ) ) )
1.1.1.15  root     1742:                && (   ( ShifterFrame.FreqPos60.VBL < nVBLs )
                   1743:                    || ( ShifterFrame.FreqPos60.FrameCycles < ShifterFrame.FreqPos50.FrameCycles )
                   1744:                    || ( ShifterFrame.FreqPos60.HBL > nHBL )
1.1.1.22! root     1745:                    || ( ( ShifterFrame.FreqPos60.HBL == nHBL ) && ( ShifterFrame.FreqPos60.LineCycles > LineRemoveBottomCycle-4 ) ) ) )
1.1.1.15  root     1746:        {
                   1747:                LOG_TRACE ( TRACE_VIDEO_BORDER_V , "detect remove bottom 60Hz\n" );
                   1748:                OverscanMode |= OVERSCANMODE_BOTTOM;
                   1749:                nEndHBL = SCANLINES_PER_FRAME_60HZ;     /* new end for a 60 Hz screen */
                   1750:        }
                   1751: 
                   1752:        /* Remove bottom border for a 50 Hz screen (tests are similar to the ones for top border) */
1.1.1.18  root     1753:        else if ( ( nHBL == VIDEO_END_HBL_50HZ + BlankLines - 1 )       /* last displayed line in 50 Hz */
1.1.1.15  root     1754:                && ( ( OverscanMode & OVERSCANMODE_BOTTOM ) == 0 )      /* border was not already removed at line VIDEO_END_HBL_60HZ-1 */
                   1755:                && ( ShifterFrame.FreqPos60.VBL == nVBLs )              /* switch to 60 Hz during this VBL */
1.1.1.20  root     1756:                && ( ( ShifterFrame.FreqPos60.HBL < nHBL )
                   1757:                    || ( ( ShifterFrame.FreqPos60.HBL == nHBL ) && ( ShifterFrame.FreqPos60.LineCycles <= LineRemoveBottomCycle ) ) )
1.1.1.15  root     1758:                && (   ( ShifterFrame.FreqPos50.VBL < nVBLs )
                   1759:                    || ( ShifterFrame.FreqPos50.FrameCycles < ShifterFrame.FreqPos60.FrameCycles )
                   1760:                    || ( ShifterFrame.FreqPos50.HBL > nHBL )
1.1.1.21  root     1761:                    || ( ( ShifterFrame.FreqPos50.HBL == nHBL ) && ( ShifterFrame.FreqPos50.LineCycles > LineRemoveBottomCycle ) ) ) )
1.1.1.13  root     1762:        {
1.1.1.15  root     1763:                LOG_TRACE ( TRACE_VIDEO_BORDER_V , "detect remove bottom\n" );
                   1764:                OverscanMode |= OVERSCANMODE_BOTTOM;
                   1765:                nEndHBL = VIDEO_END_HBL_50HZ+VIDEO_HEIGHT_BOTTOM_50HZ;  /* new end for a 50 Hz screen */
                   1766:        }
                   1767: 
                   1768: 
                   1769:        //
                   1770:        // Check some left/right borders effects that were not detected earlier
                   1771:        // (this is usually due to staying in 60 Hz for too long, which is often a bad
                   1772:        // coding practice as it can distort the display on a real ST)
                   1773:        //
                   1774: 
                   1775:        /* Special case when the line was not started in 60 Hz, then switched to 60 Hz */
                   1776:        /* and was not restored to 50 Hz before the end of the line. In that case, the */
                   1777:        /* line ends 2 bytes earlier on the right (line can start at LINE_START_CYCLE_71/50) */
                   1778:        /* Some programs also turn to 60 Hz too early during the active display of the last */
                   1779:        /* line to remove the bottom border (FNIL by TNT), in that case, we should also remove */
                   1780:        /* 2 bytes to this line */
                   1781:        if ( ( ( ShifterFrame.ShifterLines[ nHBL ].BorderMask & BORDERMASK_RIGHT_MINUS_2 ) == 0 )
                   1782:          && ( ShifterFrame.ShifterLines[ nHBL ].DisplayStartCycle != LINE_START_CYCLE_60 )             /* start could be 0 or 56 */
                   1783:          && ( ShifterFrame.ShifterLines[ nHBL ].DisplayEndCycle == LINE_END_CYCLE_60 ) )
                   1784:        {
                   1785:                ShifterFrame.ShifterLines[ nHBL ].BorderMask |= BORDERMASK_RIGHT_MINUS_2;
                   1786:                LOG_TRACE ( TRACE_VIDEO_BORDER_H , "detect late right-2 %d<->%d\n" ,
                   1787:                        ShifterFrame.ShifterLines[ nHBL ].DisplayStartCycle , ShifterFrame.ShifterLines[ nHBL ].DisplayEndCycle );
                   1788:        }
                   1789: 
                   1790: 
                   1791:        /* Similar case when line started in 60 Hz but did not end at the usual LINE_END_CYCLE_60 position */
                   1792:        /* (line can end at LINE_END_CYCLE_71/50 or have right border removed) */
                   1793:        /* This means left border had 2 bytes more to display */
                   1794:        if ( ( ( ShifterFrame.ShifterLines[ nHBL ].BorderMask & BORDERMASK_LEFT_PLUS_2 ) == 0 )
                   1795:          && ( ShifterFrame.ShifterLines[ nHBL ].DisplayStartCycle == LINE_START_CYCLE_60 )
                   1796:          && ( ShifterFrame.ShifterLines[ nHBL ].DisplayEndCycle != LINE_END_CYCLE_60 ) )               /* end could be 160, 372 or 460 */
                   1797:        {
                   1798:                ShifterFrame.ShifterLines[ nHBL ].BorderMask |= BORDERMASK_LEFT_PLUS_2;
                   1799:                LOG_TRACE ( TRACE_VIDEO_BORDER_H , "detect late left+2 %d<->%d\n" ,
                   1800:                        ShifterFrame.ShifterLines[ nHBL ].DisplayStartCycle , ShifterFrame.ShifterLines[ nHBL ].DisplayEndCycle );
                   1801:        }
                   1802: 
                   1803: 
                   1804:        /* Although a 'left+2' was detected earlier, the freq was switched back to 60 Hz during DE, so the line is just */
                   1805:        /* a normal 60 Hz line ; we must cancel the 'left+2' flag */
                   1806:        else if ( ( ShifterFrame.ShifterLines[ nHBL ].BorderMask & BORDERMASK_LEFT_PLUS_2 )
                   1807:          && ( ShifterFrame.ShifterLines[ nHBL ].DisplayEndCycle == LINE_END_CYCLE_60 ) )
                   1808:        {
                   1809:                ShifterFrame.ShifterLines[ nHBL ].BorderMask &= ~BORDERMASK_LEFT_PLUS_2;
                   1810:                LOG_TRACE ( TRACE_VIDEO_BORDER_H , "cancel late left+2 %d<->%d\n" ,
                   1811:                        ShifterFrame.ShifterLines[ nHBL ].DisplayStartCycle , ShifterFrame.ShifterLines[ nHBL ].DisplayEndCycle );
                   1812:        }
                   1813: 
                   1814: 
1.1.1.13  root     1815: 
1.1.1.15  root     1816:        /* Store palette for very first line on screen - HBLPalettes[0] */
                   1817:        if (nHBL == nFirstVisibleHbl-1)
                   1818:        {
                   1819:                /* Store ALL palette for this line into raster table for datum */
                   1820:                Video_StoreFirstLinePalette();
                   1821:        }
                   1822: 
                   1823:        if (bUseHighRes)
                   1824:        {
                   1825:                /* Copy for hi-res (no overscan) */
                   1826:                if (nHBL >= nFirstVisibleHbl && nHBL < nLastVisibleHbl)
                   1827:                        Video_CopyScreenLineMono();
                   1828:        }
                   1829:        /* Are we in possible visible color display (including borders)? */
                   1830:        else if (nHBL >= nFirstVisibleHbl && nHBL < nLastVisibleHbl)
                   1831:        {
                   1832:                /* Store resolution for every line so can check for mix low/med screens */
                   1833:                Video_StoreResolution(nHBL-nFirstVisibleHbl);
                   1834: 
                   1835:                /* Copy line of screen to buffer to simulate TV raster trace
                   1836:                 * - required for mouse cursor display/game updates
                   1837:                 * Eg, Lemmings and The Killing Game Show are good examples */
                   1838:                Video_CopyScreenLineColor();
                   1839:        }
                   1840: }
                   1841: 
                   1842: 
                   1843: /*-----------------------------------------------------------------------*/
                   1844: /**
                   1845:  * Set default values for the next HBL, depending on the current res/freq.
                   1846:  * We set the number of cycles per line, as well as some default values
                   1847:  * for display start/end cycle.
                   1848:  */
                   1849: static void Video_StartHBL(void)
                   1850: {
1.1.1.19  root     1851:        if ((IoMem_ReadByte(0xff8260) & 3) == 2)  /* hi res */
1.1.1.15  root     1852:        {
                   1853:                nCyclesPerLine = CYCLES_PER_LINE_71HZ;
                   1854:                ShifterFrame.ShifterLines[ nHBL ].DisplayStartCycle = LINE_START_CYCLE_71;
                   1855:                ShifterFrame.ShifterLines[ nHBL ].DisplayEndCycle = LINE_END_CYCLE_71;
                   1856:        }
                   1857:        else
                   1858:        {
                   1859:                if ( IoMem_ReadByte ( 0xff820a ) & 2 )          /* 50 Hz */
                   1860:                {
                   1861:                        nCyclesPerLine = CYCLES_PER_LINE_50HZ;
                   1862:                        if ( ShifterFrame.ShifterLines[ nHBL ].DisplayStartCycle == -1 )        /* start not set yet */
                   1863:                                ShifterFrame.ShifterLines[ nHBL ].DisplayStartCycle = LINE_START_CYCLE_50;
                   1864:                        ShifterFrame.ShifterLines[ nHBL ].DisplayEndCycle = LINE_END_CYCLE_50;
                   1865:                }
                   1866:                else                                            /* 60 Hz */
                   1867:                {
                   1868:                        nCyclesPerLine = CYCLES_PER_LINE_60HZ;
                   1869:                        if ( ShifterFrame.ShifterLines[ nHBL ].DisplayStartCycle == -1 )        /* start not set yet */
                   1870:                                ShifterFrame.ShifterLines[ nHBL ].DisplayStartCycle = LINE_START_CYCLE_60;
                   1871:                        ShifterFrame.ShifterLines[ nHBL ].DisplayEndCycle = LINE_END_CYCLE_60;
                   1872:                }
                   1873:        }
1.1.1.21  root     1874: //fprintf (stderr , "Video_StartHBL %d %d %d\n", nHBL , ShifterFrame.ShifterLines[ nHBL ].DisplayStartCycle , ShifterFrame.ShifterLines[ nHBL ].DisplayEndCycle );
1.1.1.15  root     1875: }
                   1876: 
                   1877: 
                   1878: /*-----------------------------------------------------------------------*/
                   1879: /**
                   1880:  * End Of Line interrupt
                   1881:  * This interrupt is started on cycle position 404 in 50 Hz and on cycle
                   1882:  * position 400 in 60 Hz. 50 Hz display ends at cycle 376 and 60 Hz displays
1.1.1.21  root     1883:  * ends at cycle 372. This means the EndLine interrupt happens 24 cycles
1.1.1.15  root     1884:  * after DisplayEndCycle.
                   1885:  * Note that if bit 3 of MFP AER is 1, then timer B will count start of line
1.1.1.21  root     1886:  * instead of end of line (at cycle 52+24 or 56+24)
1.1.1.15  root     1887:  */
                   1888: void Video_InterruptHandler_EndLine(void)
                   1889: {
                   1890:        int FrameCycles, HblCounterVideo, LineCycles;
                   1891:        int PendingCycles = -INT_CONVERT_FROM_INTERNAL ( PendingInterruptCount , INT_CPU_CYCLE );
                   1892: 
                   1893:        Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
                   1894: 
                   1895:        LOG_TRACE ( TRACE_VIDEO_HBL , "EndLine TB %d video_cyc=%d line_cyc=%d pending_int_cnt=%d\n" ,
                   1896:                       nHBL , FrameCycles , LineCycles , PendingCycles );
                   1897: 
                   1898:        /* Remove this interrupt from list and re-order */
1.1.1.16  root     1899:        CycInt_AcknowledgeInterrupt();
1.1.1.15  root     1900: 
                   1901:        /* Ignore HBLs in VDI mode */
                   1902:        if (bUseVDIRes)
                   1903:                return;
                   1904: 
                   1905:        /* Generate new Endline, if need to - there are 313 HBLs per frame */
                   1906:        if (nHBL < nScanlinesPerFrame-1)
                   1907:        {
1.1.1.21  root     1908:                /* By default, next EndLine's int will be on line nHBL+1 at pos 376+24 or 372+24 */
1.1.1.15  root     1909:                if ( ( IoMem[0xfffa03] & ( 1 << 3 ) ) == 0 )            /* count end of line */
1.1.1.13  root     1910:                {
1.1.1.15  root     1911:                        /* If EndLine int is delayed too much (more than 100 cycles), nLineCycles will */
                   1912:                        /* be in the range 0..xxx instead of 400..512. In that case, we need to add */
                   1913:                        /* nCyclesPerLine to be in the range 512..x+512 */
                   1914:                        /* Maximum possible delay should be around 160 cycles on STF (DIVS) */
                   1915:                        /* In that case, HBL int will be delayed too, so we will have HblCounterVideo == nHBL+1 */
                   1916:                        if ( HblCounterVideo == nHBL+1 )                /* int happened in fact on the next line nHBL+1 */
                   1917:                                LineCycles += nCyclesPerLine;
1.1.1.13  root     1918: 
1.1.1.18  root     1919:                        LineTimerBCycle = Video_TimerB_GetDefaultPos ();
1.1.1.13  root     1920:                }
                   1921: 
1.1.1.15  root     1922:                else                                                    /* count start of line, no possible delay to handle */
1.1.1.13  root     1923:                {
1.1.1.18  root     1924:                        LineTimerBCycle = Video_TimerB_GetDefaultPos ();
1.1.1.13  root     1925:                }
                   1926: 
1.1.1.15  root     1927: //fprintf ( stderr , "new tb %d %d %d\n" , LineTimerBCycle , nCyclesPerLine , LineTimerBCycle - LineCycles + nCyclesPerLine );
1.1.1.16  root     1928:                CycInt_AddRelativeInterrupt ( LineTimerBCycle - LineCycles + nCyclesPerLine,
1.1.1.15  root     1929:                                         INT_CPU_CYCLE, INTERRUPT_VIDEO_ENDLINE );
1.1.1.11  root     1930:        }
                   1931: 
1.1.1.15  root     1932:        /* Timer B occurs at END of first visible screen line in Event Count mode */
1.1.1.18  root     1933:        if ( ( nHBL >= nStartHBL ) && ( nHBL < nEndHBL + BlankLines ) )
1.1.1.11  root     1934:        {
1.1.1.15  root     1935:                /* Handle Timer B when using Event Count mode */
                   1936:                /* We must ensure that the write to fffa1b to activate timer B was */
                   1937:                /* completed before the point where the end of line signal was generated */
                   1938:                /* (in the case of a move.b #8,$fffa1b that would happen 4 cycles */
                   1939:                /* before end of line, the interrupt should not be generated) */
                   1940:                if ( (MFP_TBCR == 0x08)                                         /* Is timer in Event Count mode ? */
                   1941:                        && ( ( TimerBEventCountCycleStart == -1 )               /* timer B was started during a previous VBL */
                   1942:                          || ( TimerBEventCountCycleStart < FrameCycles-PendingCycles ) ) )     /* timer B was started before this possible interrupt */
1.1.1.21  root     1943:                        MFP_TimerB_EventCount_Interrupt ( PendingCycles );      /* we have a valid timer B interrupt */
1.1.1.11  root     1944:        }
1.1       root     1945: }
                   1946: 
1.1.1.2   root     1947: 
1.1.1.15  root     1948: 
                   1949: 
1.1.1.2   root     1950: /*-----------------------------------------------------------------------*/
1.1.1.11  root     1951: /**
                   1952:  * Store whole palette on first line so have reference to work from
                   1953:  */
1.1.1.7   root     1954: static void Video_StoreFirstLinePalette(void)
1.1       root     1955: {
1.1.1.11  root     1956:        Uint16 *pp2;
                   1957:        int i;
                   1958: 
                   1959:        pp2 = (Uint16 *)&IoMem[0xff8240];
                   1960:        for (i = 0; i < 16; i++)
1.1.1.22! root     1961:        {
1.1.1.11  root     1962:                HBLPalettes[i] = SDL_SwapBE16(*pp2++);
1.1.1.22! root     1963:                if ( ConfigureParams.System.nMachineType == MACHINE_ST)
        !          1964:                        HBLPalettes[i] &= 0x777;                        /* Force unused "random" bits to 0 */
        !          1965:        }
1.1       root     1966: 
1.1.1.11  root     1967:        /* And set mask flag with palette and resolution */
                   1968: //     FIXME ; enlever PALETTEMASK_RESOLUTION
                   1969: 
1.1.1.15  root     1970: //     if ( ShifterFrame.ShifterLines[ nFirstVisibleHbl ].BorderMask == BORDERMASK_NONE )      // no border trick, store the current res
1.1.1.11  root     1971:        HBLPaletteMasks[0] = (PALETTEMASK_RESOLUTION|PALETTEMASK_PALETTE) | (((Uint32)IoMem_ReadByte(0xff8260)&0x3)<<16);
                   1972: //     else                                            // border removal, assume low res for the whole line
                   1973: //             HBLPaletteMasks[0] = (PALETTEMASK_RESOLUTION|PALETTEMASK_PALETTE) | (0<<16);
1.1       root     1974: }
                   1975: 
1.1.1.2   root     1976: 
                   1977: /*-----------------------------------------------------------------------*/
1.1.1.11  root     1978: /**
                   1979:  * Store resolution on each line (used to test if mixed low/medium resolutions)
                   1980:  */
1.1.1.7   root     1981: static void Video_StoreResolution(int y)
1.1       root     1982: {
1.1.1.11  root     1983:        Uint8 res;
                   1984:        int Mask;
1.1       root     1985: 
1.1.1.11  root     1986:        /* Clear resolution, and set with current value */
                   1987:        if (!(bUseHighRes || bUseVDIRes))
                   1988:        {
1.1.1.20  root     1989:                if ( y >= HBL_PALETTE_MASKS )                           /* we're above the limit (res was switched to mono for more than 1 VBL in color mode ?) */
                   1990:                {
                   1991: //                     fprintf ( stderr , "store res %d line %d hbl %d %x %x %d\n" , res , y , nHBL, Mask , HBLPaletteMasks[y] , sizeof(HBLPalettes) );
                   1992:                        y = HBL_PALETTE_MASKS - 1;                      /* store in the last palette line */
                   1993:                }
                   1994: 
1.1.1.11  root     1995:                HBLPaletteMasks[y] &= ~(0x3<<16);
                   1996:                res = IoMem_ReadByte(0xff8260)&0x3;
                   1997: 
1.1.1.15  root     1998:                Mask = ShifterFrame.ShifterLines[ y+nFirstVisibleHbl ].BorderMask;
1.1.1.11  root     1999: 
1.1.1.15  root     2000:                if ( Mask & BORDERMASK_OVERSCAN_MED_RES )               /* special case for med res to render the overscan line */
                   2001:                        res = 1;                                        /* med res instead of low res */
1.1.1.11  root     2002:                else if ( Mask != BORDERMASK_NONE )                     /* border removal : assume low res for the whole line */
                   2003:                        res = 0;
1.1.1.2   root     2004: 
1.1.1.11  root     2005:                HBLPaletteMasks[y] |= PALETTEMASK_RESOLUTION|((Uint32)res)<<16;
                   2006: 
                   2007: #if 0
                   2008:                if ( ( Mask == BORDERMASK_NONE )                        /* no border trick, store the current res */
                   2009:                        || ( res == 0 ) || ( res == 1 ) )                       /* if border trick, ignore passage to hi res */
                   2010:                        HBLPaletteMasks[y] |= PALETTEMASK_RESOLUTION|((Uint32)res)<<16;
                   2011:                else                                            /* border removal or hi res : assume low res for the whole line */
                   2012:                        HBLPaletteMasks[y] |= (0)<<16;
                   2013: 
1.1.1.15  root     2014:                /* special case for med res to render the overscan line */
                   2015:                if ( Mask & BORDERMASK_OVERSCAN_MED_RES )
                   2016:                        HBLPaletteMasks[y] |= PALETTEMASK_RESOLUTION|((Uint32)1)<<16;   /* med res instead of low res */
1.1.1.11  root     2017: #endif
1.1       root     2018: 
1.1.1.11  root     2019: //   fprintf ( stderr , "store res %d line %d %x %x\n" , res , y , Mask , HBLPaletteMasks[y] );
                   2020:        }
1.1.1.9   root     2021: }
                   2022: 
                   2023: 
                   2024: /*-----------------------------------------------------------------------*/
1.1.1.11  root     2025: /**
                   2026:  * Copy one line of monochrome screen into buffer for conversion later.
                   2027:  */
                   2028: static void Video_CopyScreenLineMono(void)
                   2029: {
                   2030:        /* Copy one line - 80 bytes in ST high resolution */
                   2031:        memcpy(pSTScreen, pVideoRaster, SCREENBYTES_MONOLINE);
                   2032:        pVideoRaster += SCREENBYTES_MONOLINE;
                   2033: 
                   2034:        /* Handle STE fine scrolling (HWScrollCount is zero on ST). */
                   2035:        if (HWScrollCount)
                   2036:        {
                   2037:                Uint16 *pScrollAdj;
                   2038:                int nNegScrollCnt;
                   2039: 
                   2040:                pScrollAdj = (Uint16 *)pSTScreen;
                   2041:                nNegScrollCnt = 16 - HWScrollCount;
                   2042: 
                   2043:                /* Shift the whole line by the given scroll count */
                   2044:                while ((Uint8*)pScrollAdj < pSTScreen + SCREENBYTES_MONOLINE-2)
                   2045:                {
                   2046:                        do_put_mem_word(pScrollAdj, (do_get_mem_word(pScrollAdj) << HWScrollCount)
                   2047:                                        | (do_get_mem_word(pScrollAdj+1) >> nNegScrollCnt));
                   2048:                        ++pScrollAdj;
                   2049:                }
                   2050: 
                   2051:                /* Handle the last 16 pixels of the line */
                   2052:                do_put_mem_word(pScrollAdj, (do_get_mem_word(pScrollAdj) << HWScrollCount)
                   2053:                                | (do_get_mem_word(pVideoRaster) >> nNegScrollCnt));
                   2054: 
                   2055:                /* HW scrolling advances Shifter video counter by one */
                   2056:                pVideoRaster += 1 * 2;
                   2057:        }
                   2058: 
                   2059:        /* LineWidth is zero on ST. */
                   2060:        /* On STE, the Shifter skips the given amount of words. */
                   2061:        pVideoRaster += LineWidth*2;
                   2062: 
                   2063:        /* On STE, handle modifications of the video counter address $ff8205/07/09 */
                   2064:        /* that occurred while the display was already ON */
1.1.1.16  root     2065:        if ( VideoCounterDelayedOffset != 0 )
1.1.1.11  root     2066:        {
1.1.1.16  root     2067:                pVideoRaster += ( VideoCounterDelayedOffset & ~1 );
                   2068:                VideoCounterDelayedOffset = 0;
1.1.1.11  root     2069:        }
1.1.1.16  root     2070: 
                   2071:        if ( pVideoRasterDelayed != NULL )
1.1.1.11  root     2072:        {
1.1.1.16  root     2073:                pVideoRaster = pVideoRasterDelayed;
                   2074:                pVideoRasterDelayed = NULL;
1.1.1.11  root     2075:        }
                   2076: 
                   2077:        /* On STE, if we wrote to the hwscroll register, we set the */
                   2078:        /* new value here, once the current line was processed */
                   2079:        if ( NewHWScrollCount >= 0 )
                   2080:        {
                   2081:                HWScrollCount = NewHWScrollCount;
                   2082:                NewHWScrollCount = -1;
                   2083:        }
                   2084: 
                   2085:        /* On STE, if we wrote to the linewidth register, we set the */
                   2086:        /* new value here, once the current line was processed */
                   2087:        if ( NewLineWidth >= 0 )
                   2088:        {
                   2089:                LineWidth = NewLineWidth;
                   2090:                NewLineWidth = -1;
                   2091:        }
                   2092: 
                   2093:        /* Each screen line copied to buffer is always same length */
                   2094:        pSTScreen += SCREENBYTES_MONOLINE;
1.1.1.22! root     2095: 
        !          2096:        /* We must keep the new video address in a 24 bit space */
        !          2097:        /* (in case it pointed to IO space and is now >= 0x1000000) */
        !          2098:        pVideoRaster = ( ( pVideoRaster - STRam ) & 0xffffff ) + STRam;
1.1.1.11  root     2099: }
                   2100: 
                   2101: 
                   2102: /*-----------------------------------------------------------------------*/
                   2103: /**
                   2104:  * Copy one line of color screen into buffer for conversion later.
                   2105:  * Possible lines may be top/bottom border, and/or left/right borders.
                   2106:  */
1.1.1.9   root     2107: static void Video_CopyScreenLineColor(void)
                   2108: {
1.1.1.15  root     2109:        int LineBorderMask;
1.1.1.11  root     2110:        int VideoOffset = 0;
                   2111:        int STF_PixelScroll = 0;
1.1.1.14  root     2112:        int LineRes;
1.1.1.17  root     2113:        Uint8 *pVideoRasterEndLine;                     /* addr of the last byte copied from pVideoRaster to pSTScreen (for HWScrollCount) */
                   2114:        int i;
1.1.1.14  root     2115: 
1.1.1.15  root     2116:        LineBorderMask = ShifterFrame.ShifterLines[ nHBL ].BorderMask;
1.1.1.16  root     2117:        STF_PixelScroll = ShifterFrame.ShifterLines[ nHBL ].DisplayPixelShift;
1.1.1.15  root     2118: 
                   2119:        /* Get resolution for this line (in case of mixed low/med screen) */
1.1.1.20  root     2120:        i = nHBL-nFirstVisibleHbl;
                   2121:        if ( i >= HBL_PALETTE_MASKS )
                   2122:                i = HBL_PALETTE_MASKS - 1;
                   2123:        LineRes = ( HBLPaletteMasks[i] >> 16 ) & 1;             /* 0=low res  1=med res */
1.1.1.11  root     2124: 
1.1.1.16  root     2125:        //fprintf(stderr , "copy line %d start %d end %d 0x%x 0x%x\n" , nHBL, nStartHBL, nEndHBL, LineBorderMask, pVideoRaster - STRam);
1.1.1.11  root     2126: 
1.1.1.22! root     2127:        /* FIXME [NP] : when removing left border and displaying med res at 60 Hz on STE, we have a 3 pixel shift */
        !          2128:        /* to correct to have bitmaps and color changes in sync. */
        !          2129:        /* For now we only shift for med @ 60 Hz, but this should be measured for all */
        !          2130:        /* freq and low / med res combinations on a real STE (fix "HighResMode" demo by Paradox). */
        !          2131:        if ( ( ConfigureParams.System.nMachineType == MACHINE_STE )
        !          2132:          && ( LineBorderMask & BORDERMASK_LEFT_OFF_MED )
        !          2133:          && ( nCyclesPerLine == 508 )
        !          2134:          )
        !          2135:        {
        !          2136:                STF_PixelScroll = 3;                    
        !          2137:        }
        !          2138: 
1.1.1.11  root     2139:        /* If left border is opened, we need to compensate one missing word in low res (1 plan) */
1.1.1.15  root     2140:        /* If overscan is in med res, the offset is variable */
                   2141:        if ( LineBorderMask & BORDERMASK_OVERSCAN_MED_RES )
                   2142:                VideoOffset = - ( ( LineBorderMask >> 20 ) & 0x0f );            /* No Cooper=0  PYM=-2 in med res overscan */
1.1.1.11  root     2143: 
                   2144:        else if ( LineBorderMask & BORDERMASK_LEFT_OFF )
1.1.1.18  root     2145:        {
                   2146: #ifdef SCROLL2_4PX
                   2147:                int     ShiftPixels = 0;
                   2148: 
                   2149:                if      ( STF_PixelScroll == 13 )       { VideoOffset = 2;      ShiftPixels = 8; }
                   2150:                else if ( STF_PixelScroll == 9 )        { VideoOffset = 0;      ShiftPixels = 8; }
                   2151:                else if ( STF_PixelScroll == 5 )        { VideoOffset = -2;     ShiftPixels = 8; }
                   2152:                else if ( STF_PixelScroll == 1 )        { VideoOffset = -4;     ShiftPixels = 8; }
                   2153: 
                   2154:                else                                    VideoOffset = -2;       /* Normal low res left border removal without 4 pixels scrolling */
                   2155: 
                   2156:                STF_PixelScroll -= ShiftPixels;
                   2157: #else
1.1.1.14  root     2158:                VideoOffset = -2;                                               /* always 2 bytes in low res overscan */
1.1.1.18  root     2159: #endif
                   2160:        }
1.1.1.11  root     2161: 
1.1.1.16  root     2162:        else if ( LineBorderMask & BORDERMASK_LEFT_OFF_2_STE )
                   2163:                VideoOffset = -4;                                               /* 4 first bytes of the line are not shown */
                   2164: 
1.1.1.11  root     2165:        /* Handle 4 pixels hardware scrolling ('ST Cnx' demo in 'Punish Your Machine') */
                   2166:        /* Depending on the number of pixels, we need to compensate for some skipped words */
1.1.1.15  root     2167:        else if ( LineBorderMask & BORDERMASK_LEFT_OFF_MED )
1.1.1.11  root     2168:        {
1.1.1.16  root     2169:                if      ( STF_PixelScroll == 13 )       VideoOffset = 2;
                   2170:                else if ( STF_PixelScroll == 9 )        VideoOffset = 0;
                   2171:                else if ( STF_PixelScroll == 5 )        VideoOffset = -2;
                   2172:                else if ( STF_PixelScroll == 1 )        VideoOffset = -4;
1.1.1.11  root     2173:                else                                    VideoOffset = 0;        /* never used ? */
                   2174: 
1.1.1.16  root     2175:                STF_PixelScroll -= 8;                                   /* removing left border in mid res also shifts display to the left */
1.1.1.11  root     2176:                // fprintf(stderr , "scr off %d %d\n" , STF_PixelScroll , VideoOffset);
                   2177:        }
                   2178: 
1.1.1.13  root     2179: 
1.1.1.11  root     2180:        /* Is total blank line? I.e. top/bottom border? */
1.1.1.18  root     2181:        if ((nHBL < nStartHBL) || (nHBL >= nEndHBL + BlankLines)
1.1.1.15  root     2182:            || (LineBorderMask & BORDERMASK_EMPTY_LINE))
1.1.1.11  root     2183:        {
                   2184:                /* Clear line to color '0' */
                   2185:                memset(pSTScreen, 0, SCREENBYTES_LINE);
                   2186:        }
                   2187:        else
                   2188:        {
1.1.1.18  root     2189:                /* Does have left border ? */
1.1.1.16  root     2190:                if ( LineBorderMask & ( BORDERMASK_LEFT_OFF | BORDERMASK_LEFT_OFF_MED ) )       /* bigger line by 26 bytes on the left */
1.1.1.11  root     2191:                {
                   2192:                        pVideoRaster += BORDERBYTES_LEFT-SCREENBYTES_LEFT+VideoOffset;
                   2193:                        memcpy(pSTScreen, pVideoRaster, SCREENBYTES_LEFT);
                   2194:                        pVideoRaster += SCREENBYTES_LEFT;
                   2195:                }
1.1.1.16  root     2196:                else if ( LineBorderMask & BORDERMASK_LEFT_OFF_2_STE )  /* bigger line by 20 bytes on the left (STE specific) */
                   2197:                {                                                       /* bytes 0-3 are not shown, only next 16 bytes (32 pixels, 4 bitplanes) */
                   2198:                        if ( SCREENBYTES_LEFT > BORDERBYTES_LEFT_2_STE )
                   2199:                        {
                   2200:                                memset ( pSTScreen, 0, SCREENBYTES_LEFT-BORDERBYTES_LEFT_2_STE+4 );     /* clear unused pixels + bytes 0-3 */
                   2201:                                memcpy ( pSTScreen+SCREENBYTES_LEFT-BORDERBYTES_LEFT_2_STE+4, pVideoRaster+VideoOffset+4, BORDERBYTES_LEFT_2_STE-4 );
                   2202:                        }
                   2203:                        else
                   2204:                                memcpy ( pSTScreen, pVideoRaster+BORDERBYTES_LEFT_2_STE-SCREENBYTES_LEFT+VideoOffset, SCREENBYTES_LEFT );
                   2205: 
                   2206:                        pVideoRaster += BORDERBYTES_LEFT_2_STE+VideoOffset;
                   2207:                }
                   2208:                else if (LineBorderMask & BORDERMASK_LEFT_PLUS_2)       /* bigger line by 2 bytes on the left */
1.1.1.11  root     2209:                {
1.1.1.16  root     2210:                        if ( SCREENBYTES_LEFT > 2 )
                   2211:                        {
                   2212:                                memset(pSTScreen,0,SCREENBYTES_LEFT-2);         /* clear unused pixels */
                   2213:                                memcpy(pSTScreen+SCREENBYTES_LEFT-2, pVideoRaster, 2);
                   2214:                        }
                   2215:                        else
                   2216:                        {                                               /* nothing to copy, left border is not large enough */
                   2217:                        }
                   2218: 
1.1.1.11  root     2219:                        pVideoRaster += 2;
                   2220:                }
1.1.1.16  root     2221:                else if (bSteBorderFlag)                                /* bigger line by 8 bytes on the left (STE specific) */
1.1.1.12  root     2222:                {
1.1.1.16  root     2223:                        if ( SCREENBYTES_LEFT > 4*2 )
                   2224:                        {
                   2225:                                memset(pSTScreen,0,SCREENBYTES_LEFT-4*2);       /* clear unused pixels */
                   2226:                                memcpy(pSTScreen+SCREENBYTES_LEFT-4*2, pVideoRaster, 4*2);
                   2227:                        }
                   2228:                        else
                   2229:                        {                                               /* nothing to copy, left border is not large enough */
                   2230:                        }
                   2231: 
1.1.1.12  root     2232:                        pVideoRaster += 4*2;
                   2233:                }
1.1.1.11  root     2234:                else
1.1.1.16  root     2235:                        memset(pSTScreen,0,SCREENBYTES_LEFT);           /* left border not removed, clear to color '0' */
1.1.1.11  root     2236: 
                   2237:                /* Short line due to hires in the middle ? */
                   2238:                if (LineBorderMask & BORDERMASK_STOP_MIDDLE)
                   2239:                {
                   2240:                        /* 106 bytes less in the line */
                   2241:                        memcpy(pSTScreen+SCREENBYTES_LEFT, pVideoRaster, SCREENBYTES_MIDDLE-106);
1.1.1.12  root     2242:                        memset(pSTScreen+SCREENBYTES_LEFT+SCREENBYTES_MIDDLE-106, 0, 106);      /* clear unused pixels */
1.1.1.11  root     2243:                        pVideoRaster += (SCREENBYTES_MIDDLE-106);
                   2244:                }
                   2245:                else
                   2246:                {
                   2247:                        /* normal middle part (160 bytes) */
                   2248:                        memcpy(pSTScreen+SCREENBYTES_LEFT, pVideoRaster, SCREENBYTES_MIDDLE);
                   2249:                        pVideoRaster += SCREENBYTES_MIDDLE;
                   2250:                }
                   2251: 
                   2252:                /* Does have right border ? */
                   2253:                if (LineBorderMask & BORDERMASK_RIGHT_OFF)
                   2254:                {
                   2255:                        memcpy(pSTScreen+SCREENBYTES_LEFT+SCREENBYTES_MIDDLE, pVideoRaster, SCREENBYTES_RIGHT);
1.1.1.17  root     2256:                        pVideoRasterEndLine = pVideoRaster + SCREENBYTES_RIGHT;
1.1.1.16  root     2257:                        pVideoRaster += BORDERBYTES_RIGHT;
1.1.1.11  root     2258:                }
                   2259:                else if (LineBorderMask & BORDERMASK_RIGHT_MINUS_2)
                   2260:                {
                   2261:                        /* Shortened line by 2 bytes */
                   2262:                        memset(pSTScreen+SCREENBYTES_LEFT+SCREENBYTES_MIDDLE-2, 0, SCREENBYTES_RIGHT+2);
                   2263:                        pVideoRaster -= 2;
1.1.1.17  root     2264:                        pVideoRasterEndLine = pVideoRaster;
1.1.1.11  root     2265:                }
                   2266:                else
                   2267:                {
                   2268:                        /* Simply clear right border to '0' */
                   2269:                        memset(pSTScreen+SCREENBYTES_LEFT+SCREENBYTES_MIDDLE,0,SCREENBYTES_RIGHT);
1.1.1.17  root     2270:                        pVideoRasterEndLine = pVideoRaster;
1.1.1.11  root     2271:                }
                   2272: 
1.1.1.18  root     2273:                /* Shifter read bytes and borders can change, but display is blank, so finally clear the line with color 0 */
                   2274:                if (LineBorderMask & BORDERMASK_BLANK_LINE)
                   2275:                        memset(pSTScreen, 0, SCREENBYTES_LINE);
                   2276: 
1.1.1.11  root     2277:                /* Full right border removal up to the end of the line (cycle 512) */
                   2278:                if (LineBorderMask & BORDERMASK_RIGHT_OFF_FULL)
                   2279:                        pVideoRaster += BORDERBYTES_RIGHT_FULL;
                   2280: 
                   2281:                /* Correct the offset for pVideoRaster from BORDERMASK_LEFT_OFF above if needed */
                   2282:                pVideoRaster -= VideoOffset;            /* VideoOffset is 0 or -2 */
                   2283: 
                   2284: 
                   2285:                /* STE specific */
1.1.1.13  root     2286:                if (!bSteBorderFlag && HWScrollCount)           /* Handle STE fine scrolling (HWScrollCount is zero on ST) */
1.1.1.11  root     2287:                {
1.1.1.13  root     2288:                        Uint16 *pScrollAdj;     /* Pointer to actual position in line */
1.1.1.11  root     2289:                        int nNegScrollCnt;
1.1.1.13  root     2290:                        Uint16 *pScrollEndAddr; /* Pointer to end of the line */
1.1.1.11  root     2291: 
                   2292:                        nNegScrollCnt = 16 - HWScrollCount;
                   2293:                        if (LineBorderMask & BORDERMASK_LEFT_OFF)
                   2294:                                pScrollAdj = (Uint16 *)pSTScreen;
1.1.1.16  root     2295:                        else if (LineBorderMask & BORDERMASK_LEFT_OFF_2_STE)
                   2296:                        {
                   2297:                                if ( SCREENBYTES_LEFT > BORDERBYTES_LEFT_2_STE )
1.1.1.17  root     2298:                                        pScrollAdj = (Uint16 *)(pSTScreen+8);   /* don't scroll the 8 first bytes (keep color 0)*/
1.1.1.16  root     2299:                                else
                   2300:                                        pScrollAdj = (Uint16 *)pSTScreen;       /* we render less bytes on screen than a real ST, scroll the whole line */
                   2301:                        }
1.1.1.11  root     2302:                        else
                   2303:                                pScrollAdj = (Uint16 *)(pSTScreen + SCREENBYTES_LEFT);
1.1.1.16  root     2304: 
1.1.1.17  root     2305:                        /* When shifting the line to the left, we will have 'HWScrollCount' missing pixels at   */
                   2306:                        /* the end of the line. We must complete these last 16 pixels with pixels from the      */
                   2307:                        /* video counter last accessed value in pVideoRasterEndLine.                            */
                   2308:                        /* There're 2 passes :                                                                  */
                   2309:                        /*  - shift whole line except the last 16 pixels                                        */
                   2310:                        /*  - shift/complete the last 16 pixels                                                 */
                   2311: 
                   2312:                        /* Addr of the last byte to shift in the 1st pass (excluding the last 16 pixels of the line) */
1.1.1.11  root     2313:                        if (LineBorderMask & BORDERMASK_RIGHT_OFF)
                   2314:                                pScrollEndAddr = (Uint16 *)(pSTScreen + SCREENBYTES_LINE - 8);
                   2315:                        else
                   2316:                                pScrollEndAddr = (Uint16 *)(pSTScreen + SCREENBYTES_LEFT + SCREENBYTES_MIDDLE - 8);
                   2317: 
1.1.1.17  root     2318: 
                   2319:                        if ( LineRes == 1 )                             /* med res */
1.1.1.11  root     2320:                        {
1.1.1.15  root     2321:                                /* in med res, 16 pixels are 4 bytes, not 8 as in low res, so only the last 4 bytes need a special case */
1.1.1.17  root     2322:                                pScrollEndAddr += 2;                    /* 2 Uint16 = 4 bytes = 16 pixels */
1.1.1.14  root     2323: 
1.1.1.17  root     2324:                                /* Shift the whole line to the left by the given scroll count (except the last 16 pixels) */
1.1.1.14  root     2325:                                while (pScrollAdj < pScrollEndAddr)
                   2326:                                {
                   2327:                                        do_put_mem_word(pScrollAdj, (do_get_mem_word(pScrollAdj) << HWScrollCount)
                   2328:                                                        | (do_get_mem_word(pScrollAdj+2) >> nNegScrollCnt));
                   2329:                                        ++pScrollAdj;
                   2330:                                }
1.1.1.17  root     2331:                                /* Handle the last 16 pixels of the line (complete the line with pixels from pVideoRasterEndLine) */
                   2332:                                for ( i=0 ; i<2 ; i++ )
                   2333:                                        do_put_mem_word(pScrollAdj+i, (do_get_mem_word(pScrollAdj+i) << HWScrollCount)
                   2334:                                                | (do_get_mem_word(pVideoRasterEndLine+i*2) >> nNegScrollCnt));
1.1.1.14  root     2335: 
                   2336:                                /* Depending on whether $ff8264 or $ff8265 was used to scroll, */
                   2337:                                /* we prefetched 16 pixel (4 bytes) */
1.1.1.13  root     2338:                                if ( HWScrollPrefetch == 1 )            /* $ff8265 prefetches 16 pixels */
                   2339:                                        pVideoRaster += 2 * 2;          /* 2 bitplans */
1.1.1.14  root     2340: 
                   2341:                                /* If scrolling with $ff8264, there's no prefetch, which means display starts */
                   2342:                                /* 16 pixels later but still stops at the normal point (eg we display */
                   2343:                                /* (320-16) pixels in low res). We shift the whole line 4 bytes to the right to */
                   2344:                                /* get the correct result (using memmove, as src/dest are overlapping). */
                   2345:                                else
                   2346:                                {
                   2347:                                        if (LineBorderMask & BORDERMASK_RIGHT_OFF)
                   2348:                                                memmove ( pSTScreen+4 , pSTScreen , SCREENBYTES_LINE - 4 );
                   2349:                                        else
                   2350:                                                memmove ( pSTScreen+4 , pSTScreen , SCREENBYTES_LEFT + SCREENBYTES_MIDDLE - 4 );
                   2351: 
                   2352:                                        memset ( pSTScreen , 0 , 4 );   /* first 16 pixels are color '0' */
                   2353:                                }
1.1.1.11  root     2354:                        }
1.1.1.14  root     2355: 
1.1.1.17  root     2356:                        else                                            /* low res */
1.1.1.11  root     2357:                        {
1.1.1.17  root     2358:                                /* Shift the whole line to the left by the given scroll count (except the last 16 pixels) */
1.1.1.11  root     2359:                                while (pScrollAdj < pScrollEndAddr)
                   2360:                                {
                   2361:                                        do_put_mem_word(pScrollAdj, (do_get_mem_word(pScrollAdj) << HWScrollCount)
                   2362:                                                        | (do_get_mem_word(pScrollAdj+4) >> nNegScrollCnt));
                   2363:                                        ++pScrollAdj;
                   2364:                                }
1.1.1.17  root     2365:                                /* Handle the last 16 pixels of the line (complete the line with pixels from pVideoRasterEndLine) */
                   2366:                                for ( i=0 ; i<4 ; i++ )
                   2367:                                        do_put_mem_word(pScrollAdj+i, (do_get_mem_word(pScrollAdj+i) << HWScrollCount)
                   2368:                                                | (do_get_mem_word(pVideoRasterEndLine+i*2) >> nNegScrollCnt));
1.1.1.13  root     2369: 
                   2370:                                /* Depending on whether $ff8264 or $ff8265 was used to scroll, */
                   2371:                                /* we prefetched 16 pixel (8 bytes) */
                   2372:                                if ( HWScrollPrefetch == 1 )            /* $ff8265 prefetches 16 pixels */
                   2373:                                        pVideoRaster += 4 * 2;          /* 4 bitplans */
                   2374: 
                   2375:                                /* If scrolling with $ff8264, there's no prefetch, which means display starts */
                   2376:                                /* 16 pixels later but still stops at the normal point (eg we display */
                   2377:                                /* (320-16) pixels in low res). We shift the whole line 8 bytes to the right to */
                   2378:                                /* get the correct result (using memmove, as src/dest are overlapping). */
                   2379:                                else
                   2380:                                {
                   2381:                                        if (LineBorderMask & BORDERMASK_RIGHT_OFF)
                   2382:                                                memmove ( pSTScreen+8 , pSTScreen , SCREENBYTES_LINE - 8 );
                   2383:                                        else
                   2384:                                                memmove ( pSTScreen+8 , pSTScreen , SCREENBYTES_LEFT + SCREENBYTES_MIDDLE - 8 );
                   2385: 
                   2386:                                        memset ( pSTScreen , 0 , 8 );   /* first 16 pixels are color '0' */
                   2387:                                }
1.1.1.11  root     2388: 
                   2389:                                /* On STE, when we have a 230 bytes overscan line and HWScrollCount > 0 */
1.1.1.17  root     2390:                                /* we must read 6 bytes less than expected if scrolling is using prefetching ($ff8265) */
                   2391:                                /* (this is not the case for the 224 bytes overscan which is a multiple of 8) */
1.1.1.11  root     2392:                                if ( (LineBorderMask & BORDERMASK_LEFT_OFF) && (LineBorderMask & BORDERMASK_RIGHT_OFF) )
1.1.1.17  root     2393:                                  {
                   2394:                                    if ( HWScrollPrefetch == 1 )
                   2395:                                        pVideoRaster -= 6;              /* we don't add 8 bytes (see above), but 2 */
                   2396:                                    else
                   2397:                                        pVideoRaster -= 0;
                   2398:                                  }
1.1.1.11  root     2399: 
                   2400:                        }
                   2401:                }
                   2402: 
                   2403:                /* LineWidth is zero on ST. */
                   2404:                /* On STE, the Shifter skips the given amount of words. */
                   2405:                pVideoRaster += LineWidth*2;
                   2406: 
                   2407:                /* On STE, handle modifications of the video counter address $ff8205/07/09 */
                   2408:                /* that occurred while the display was already ON */
1.1.1.16  root     2409:                if ( VideoCounterDelayedOffset != 0 )
1.1.1.11  root     2410:                {
1.1.1.16  root     2411:                        pVideoRaster += ( VideoCounterDelayedOffset & ~1 );
                   2412: //               fprintf ( stderr , "adjust video counter offset=%d new video=%x\n" , VideoCounterDelayedOffset , pVideoRaster-STRam );
                   2413:                        VideoCounterDelayedOffset = 0;
1.1.1.11  root     2414:                }
1.1.1.16  root     2415: 
                   2416:                if ( pVideoRasterDelayed != NULL )
1.1.1.11  root     2417:                {
1.1.1.16  root     2418:                        pVideoRaster = pVideoRasterDelayed;
                   2419: //               fprintf ( stderr , "adjust video counter const new video=%x\n" , pVideoRaster-STRam );
                   2420:                        pVideoRasterDelayed = NULL;
1.1.1.11  root     2421:                }
                   2422: 
                   2423:                /* On STE, if we wrote to the hwscroll register, we set the */
                   2424:                /* new value here, once the current line was processed */
                   2425:                if ( NewHWScrollCount >= 0 )
                   2426:                {
                   2427:                        HWScrollCount = NewHWScrollCount;
1.1.1.13  root     2428:                        HWScrollPrefetch = NewHWScrollPrefetch;
1.1.1.11  root     2429:                        NewHWScrollCount = -1;
1.1.1.13  root     2430:                        NewHWScrollPrefetch = -1;
                   2431:                }
                   2432: 
                   2433:                /* On STE, if we trigger the left border + 16 pixels trick, we set the */
                   2434:                /* new value here, once the current line was processed */
                   2435:                if ( NewSteBorderFlag >= 0 )
                   2436:                {
                   2437:                        if ( NewSteBorderFlag == 0 )
1.1.1.15  root     2438:                                bSteBorderFlag = false;
1.1.1.13  root     2439:                        else
1.1.1.15  root     2440:                                bSteBorderFlag = true;
1.1.1.13  root     2441:                        NewSteBorderFlag = -1;
1.1.1.11  root     2442:                }
                   2443: 
                   2444:                /* On STE, if we wrote to the linewidth register, we set the */
                   2445:                /* new value here, once the current line was processed */
                   2446:                if ( NewLineWidth >= 0 )
                   2447:                {
                   2448:                        LineWidth = NewLineWidth;
                   2449:                        NewLineWidth = -1;
                   2450:                }
1.1.1.16  root     2451: 
                   2452: 
                   2453:                /* Handle 4 pixels hardware scrolling ('ST Cnx' demo in 'Punish Your Machine') */
1.1.1.21  root     2454:                /* as well as scrolling occurring when removing the left border. */
1.1.1.16  root     2455:                /* If >0, shift the line by STF_PixelScroll pixels to the right */
                   2456:                /* If <0, shift the line by -STF_PixelScroll pixels to the left */
                   2457:                /* This should be handled after the STE's hardware scrolling as it will scroll */
                   2458:                /* the whole displayed area (while the STE scrolls pixels inside the displayed area) */
                   2459:                if ( STF_PixelScroll > 0 )
                   2460:                {
                   2461:                        Uint16 *pScreenLineEnd;
                   2462:                        int count;
                   2463: 
                   2464:                        pScreenLineEnd = (Uint16 *) ( pSTScreen + SCREENBYTES_LINE - 2 );
1.1.1.17  root     2465:                        if ( LineRes == 0 )                     /* low res */
                   2466:                        {
                   2467:                                for ( count = 0 ; count < ( SCREENBYTES_LINE - 8 ) / 2 ; count++ , pScreenLineEnd-- )
                   2468:                                        do_put_mem_word ( pScreenLineEnd , ( ( do_get_mem_word ( pScreenLineEnd - 4 ) << 16 ) | ( do_get_mem_word ( pScreenLineEnd ) ) ) >> STF_PixelScroll );
                   2469:                                /* Handle the first 16 pixels of the line (add color 0 pixels to the extreme left) */
                   2470:                                do_put_mem_word ( pScreenLineEnd-0 , ( do_get_mem_word ( pScreenLineEnd-0 ) >> STF_PixelScroll ) );
                   2471:                                do_put_mem_word ( pScreenLineEnd-1 , ( do_get_mem_word ( pScreenLineEnd-1 ) >> STF_PixelScroll ) );
                   2472:                                do_put_mem_word ( pScreenLineEnd-2 , ( do_get_mem_word ( pScreenLineEnd-2 ) >> STF_PixelScroll ) );
                   2473:                                do_put_mem_word ( pScreenLineEnd-3 , ( do_get_mem_word ( pScreenLineEnd-3 ) >> STF_PixelScroll ) );
                   2474:                        }
                   2475:                        else                                    /* med res */
                   2476:                        {
                   2477:                                for ( count = 0 ; count < ( SCREENBYTES_LINE - 4 ) / 2 ; count++ , pScreenLineEnd-- )
                   2478:                                        do_put_mem_word ( pScreenLineEnd , ( ( do_get_mem_word ( pScreenLineEnd - 2 ) << 16 ) | ( do_get_mem_word ( pScreenLineEnd ) ) ) >> STF_PixelScroll );
                   2479:                                /* Handle the first 16 pixels of the line (add color 0 pixels to the extreme left) */
                   2480:                                do_put_mem_word ( pScreenLineEnd-0 , ( do_get_mem_word ( pScreenLineEnd-0 ) >> STF_PixelScroll ) );
                   2481:                                do_put_mem_word ( pScreenLineEnd-1 , ( do_get_mem_word ( pScreenLineEnd-1 ) >> STF_PixelScroll ) );
                   2482:                        }
1.1.1.16  root     2483:                }
                   2484:                else if ( STF_PixelScroll < 0 )
                   2485:                {
                   2486:                        Uint16 *pScreenLineStart;
                   2487:                        int count;
                   2488: 
                   2489:                        STF_PixelScroll = -STF_PixelScroll;
                   2490:                        pScreenLineStart = (Uint16 *)pSTScreen;
1.1.1.17  root     2491:                        if ( LineRes == 0 )                     /* low res */
                   2492:                        {
                   2493:                                for ( count = 0 ; count < ( SCREENBYTES_LINE - 8 ) / 2 ; count++ , pScreenLineStart++ )
                   2494:                                        do_put_mem_word ( pScreenLineStart , ( ( do_get_mem_word ( pScreenLineStart ) << STF_PixelScroll ) | ( do_get_mem_word ( pScreenLineStart + 4 ) >> (16-STF_PixelScroll) ) ) );
                   2495:                                /* Handle the last 16 pixels of the line (add color 0 pixels to the extreme right) */
                   2496:                                do_put_mem_word ( pScreenLineStart+0 , ( do_get_mem_word ( pScreenLineStart+0 ) << STF_PixelScroll ) );
                   2497:                                do_put_mem_word ( pScreenLineStart+1 , ( do_get_mem_word ( pScreenLineStart+1 ) << STF_PixelScroll ) );
                   2498:                                do_put_mem_word ( pScreenLineStart+2 , ( do_get_mem_word ( pScreenLineStart+2 ) << STF_PixelScroll ) );
                   2499:                                do_put_mem_word ( pScreenLineStart+3 , ( do_get_mem_word ( pScreenLineStart+3 ) << STF_PixelScroll ) );
                   2500:                        }
                   2501:                        else                                    /* med res */
                   2502:                        {
                   2503:                                for ( count = 0 ; count < ( SCREENBYTES_LINE - 4 ) / 2 ; count++ , pScreenLineStart++ )
                   2504:                                        do_put_mem_word ( pScreenLineStart , ( ( do_get_mem_word ( pScreenLineStart ) << STF_PixelScroll ) | ( do_get_mem_word ( pScreenLineStart + 2 ) >> (16-STF_PixelScroll) ) ) );
                   2505:                                /* Handle the last 16 pixels of the line (add color 0 pixels to the extreme right) */
                   2506:                                do_put_mem_word ( pScreenLineStart+0 , ( do_get_mem_word ( pScreenLineStart+0 ) << STF_PixelScroll ) );
                   2507:                                do_put_mem_word ( pScreenLineStart+1 , ( do_get_mem_word ( pScreenLineStart+1 ) << STF_PixelScroll ) );
                   2508:                        }
1.1.1.16  root     2509:                }
1.1.1.11  root     2510:        }
                   2511: 
                   2512:        /* Each screen line copied to buffer is always same length */
                   2513:        pSTScreen += SCREENBYTES_LINE;
1.1.1.22! root     2514: 
        !          2515:        /* We must keep the new video address in a 24 bit space */
        !          2516:        /* (in case it pointed to IO space and is now >= 0x1000000) */
        !          2517:        pVideoRaster = ( ( pVideoRaster - STRam ) & 0xffffff ) + STRam;
1.1.1.11  root     2518: }
                   2519: 
                   2520: 
                   2521: /*-----------------------------------------------------------------------*/
                   2522: /**
                   2523:  * Copy extended GEM resolution screen
                   2524:  */
1.1.1.9   root     2525: static void Video_CopyVDIScreen(void)
1.1       root     2526: {
1.1.1.11  root     2527:        /* Copy whole screen, don't care about being exact as for GEM only */
                   2528:        memcpy(pSTScreen, pVideoRaster, ((VDIWidth*VDIPlanes)/8)*VDIHeight);
1.1       root     2529: }
                   2530: 
1.1.1.2   root     2531: 
                   2532: /*-----------------------------------------------------------------------*/
1.1.1.11  root     2533: /**
                   2534:  * Clear raster line table to store changes in palette/resolution on a line
                   2535:  * basic. Called once on VBL interrupt.
                   2536:  */
1.1       root     2537: void Video_SetScreenRasters(void)
                   2538: {
1.1.1.11  root     2539:        pHBLPaletteMasks = HBLPaletteMasks;
                   2540:        pHBLPalettes = HBLPalettes;
                   2541:        memset(pHBLPaletteMasks, 0, sizeof(Uint32)*NUM_VISIBLE_LINES);  /* Clear array */
1.1       root     2542: }
                   2543: 
1.1.1.2   root     2544: 
                   2545: /*-----------------------------------------------------------------------*/
1.1.1.11  root     2546: /**
                   2547:  * Set pointers to HBLPalette tables to store correct colours/resolutions
                   2548:  */
1.1.1.9   root     2549: static void Video_SetHBLPaletteMaskPointers(void)
1.1       root     2550: {
1.1.1.15  root     2551:        int FrameCycles, HblCounterVideo, LineCycles;
1.1.1.11  root     2552:        int Line;
                   2553: 
                   2554:        /* FIXME [NP] We should use Cycles_GetCounterOnWriteAccess, but it wouldn't     */
                   2555:        /* work when using multiple accesses instructions like move.l or movem  */
1.1.1.15  root     2556:        /* To correct this, we assume a delay of 8 cycles (should give a good approximation */
1.1.1.11  root     2557:        /* of a move.w or movem.l for example) */
                   2558:        //  FrameCycles = Cycles_GetCounterOnWriteAccess(CYCLES_COUNTER_VIDEO);
                   2559:        FrameCycles = Cycles_GetCounter(CYCLES_COUNTER_VIDEO) + 8;
                   2560: 
                   2561:        /* Find 'line' into palette - screen starts 63 lines down, less 29 for top overscan */
1.1.1.15  root     2562:        Video_ConvertPosition ( FrameCycles , &HblCounterVideo , &LineCycles );
                   2563:        Line = HblCounterVideo - nFirstVisibleHbl;
1.1.1.11  root     2564: 
                   2565:        /* FIXME [NP] if the color change occurs after the last visible pixel of a line */
                   2566:        /* we consider the palette should be modified on the next line. This is quite */
                   2567:        /* a hack, we should handle all color changes through spec512.c to have cycle */
                   2568:        /* accuracy all the time. */
1.1.1.15  root     2569:        if ( LineCycles >= LINE_END_CYCLE_NO_RIGHT )
                   2570:                Line++;
1.1.1.11  root     2571: 
                   2572:        if (Line < 0)        /* Limit to top/bottom of possible visible screen */
                   2573:                Line = 0;
                   2574:        if (Line >= NUM_VISIBLE_LINES)
                   2575:                Line = NUM_VISIBLE_LINES-1;
                   2576: 
                   2577:        /* Store pointers */
                   2578:        pHBLPaletteMasks = &HBLPaletteMasks[Line];  /* Next mask entry */
                   2579:        pHBLPalettes = &HBLPalettes[16*Line];       /* Next colour raster list x16 colours */
1.1       root     2580: }
1.1.1.8   root     2581: 
                   2582: 
1.1.1.9   root     2583: /*-----------------------------------------------------------------------*/
1.1.1.11  root     2584: /**
                   2585:  * Set video shifter timing variables according to screen refresh rate.
                   2586:  * Note: The following equation must be satisfied for correct timings:
                   2587:  *
                   2588:  *   nCyclesPerLine * nScanlinesPerFrame * nScreenRefreshRate = 8 MHz
                   2589:  */
1.1.1.10  root     2590: static void Video_ResetShifterTimings(void)
                   2591: {
1.1.1.11  root     2592:        Uint8 nSyncByte;
1.1.1.10  root     2593: 
1.1.1.11  root     2594:        nSyncByte = IoMem_ReadByte(0xff820a);
1.1.1.10  root     2595: 
1.1.1.19  root     2596:        if ((IoMem_ReadByte(0xff8260) & 3) == 2)
1.1.1.11  root     2597:        {
                   2598:                /* 71 Hz, monochrome */
                   2599:                nScreenRefreshRate = 71;
                   2600:                nScanlinesPerFrame = SCANLINES_PER_FRAME_71HZ;
                   2601:                nCyclesPerLine = CYCLES_PER_LINE_71HZ;
1.1.1.13  root     2602:                nStartHBL = VIDEO_START_HBL_71HZ;
1.1.1.11  root     2603:                nFirstVisibleHbl = FIRST_VISIBLE_HBL_71HZ;
1.1.1.13  root     2604:                nLastVisibleHbl = FIRST_VISIBLE_HBL_71HZ + VIDEO_HEIGHT_HBL_MONO;
1.1.1.11  root     2605:        }
                   2606:        else if (nSyncByte & 2)  /* Check if running in 50 Hz or in 60 Hz */
                   2607:        {
                   2608:                /* 50 Hz */
                   2609:                nScreenRefreshRate = 50;
                   2610:                nScanlinesPerFrame = SCANLINES_PER_FRAME_50HZ;
                   2611:                nCyclesPerLine = CYCLES_PER_LINE_50HZ;
1.1.1.13  root     2612:                nStartHBL = VIDEO_START_HBL_50HZ;
1.1.1.11  root     2613:                nFirstVisibleHbl = FIRST_VISIBLE_HBL_50HZ;
1.1.1.13  root     2614:                nLastVisibleHbl = FIRST_VISIBLE_HBL_50HZ + NUM_VISIBLE_LINES;
1.1.1.11  root     2615:        }
                   2616:        else
                   2617:        {
                   2618:                /* 60 Hz */
                   2619:                nScreenRefreshRate = 60;
                   2620:                nScanlinesPerFrame = SCANLINES_PER_FRAME_60HZ;
                   2621:                nCyclesPerLine = CYCLES_PER_LINE_60HZ;
1.1.1.13  root     2622:                nStartHBL = VIDEO_START_HBL_60HZ;
1.1.1.11  root     2623:                nFirstVisibleHbl = FIRST_VISIBLE_HBL_60HZ;
1.1.1.13  root     2624:                nLastVisibleHbl = FIRST_VISIBLE_HBL_60HZ + NUM_VISIBLE_LINES;
1.1.1.11  root     2625:        }
                   2626: 
                   2627:        if (bUseHighRes)
                   2628:        {
1.1.1.13  root     2629:                nEndHBL = nStartHBL + VIDEO_HEIGHT_HBL_MONO;
1.1.1.11  root     2630:        }
                   2631:        else
                   2632:        {
1.1.1.13  root     2633:                nEndHBL = nStartHBL + VIDEO_HEIGHT_HBL_COLOR;
1.1.1.11  root     2634:        }
                   2635: 
                   2636:        /* Reset freq changes position for the next VBL to come */
1.1.1.13  root     2637:        LastCycleScroll8264 = -1;
                   2638:        LastCycleScroll8265 = -1;
1.1.1.15  root     2639: 
                   2640:        TimerBEventCountCycleStart = -1;                /* reset timer B activation cycle for this VBL */
                   2641: 
1.1.1.18  root     2642:        BlankLines = 0;
1.1.1.11  root     2643: }
1.1.1.10  root     2644: 
                   2645: 
1.1.1.11  root     2646: /*-----------------------------------------------------------------------*/
                   2647: /**
1.1.1.15  root     2648:  * Clear the array indicating the state of each video line.
1.1.1.11  root     2649:  */
1.1.1.15  root     2650: static void Video_InitShifterLines ( void )
1.1.1.11  root     2651: {
1.1.1.15  root     2652:        int     i;
                   2653: 
                   2654:        for ( i=0 ; i<MAX_SCANLINES_PER_FRAME ; i++ )
                   2655:        {
                   2656:                ShifterFrame.ShifterLines[i].BorderMask = 0;
                   2657:                ShifterFrame.ShifterLines[i].DisplayPixelShift = 0;
                   2658:                ShifterFrame.ShifterLines[i].DisplayStartCycle = -1;
                   2659:        }
                   2660: 
                   2661:        ShifterFrame.ShifterLines[0].StartCycle = 0;                    /* 1st HBL starts at cycle 0 */
1.1.1.10  root     2662: }
                   2663: 
                   2664: 
                   2665: /*-----------------------------------------------------------------------*/
1.1.1.11  root     2666: /**
                   2667:  * Called on VBL, set registers ready for frame
                   2668:  */
1.1.1.9   root     2669: static void Video_ClearOnVBL(void)
                   2670: {
1.1.1.11  root     2671:        /* New screen, so first HBL */
                   2672:        nHBL = 0;
                   2673:        OverscanMode = OVERSCANMODE_NONE;
                   2674: 
                   2675:        Video_ResetShifterTimings();
                   2676: 
                   2677:        /* Get screen address pointer, aligned to 256 bytes on ST (ie ignore lowest byte) */
                   2678:        VideoBase = (Uint32)IoMem_ReadByte(0xff8201)<<16 | (Uint32)IoMem_ReadByte(0xff8203)<<8;
                   2679:        if (ConfigureParams.System.nMachineType != MACHINE_ST)
                   2680:        {
1.1.1.20  root     2681:                /* on STe 2 aligned, on TT 8 aligned. We do STe. */
1.1.1.11  root     2682:                VideoBase |= IoMem_ReadByte(0xff820d) & ~1;
                   2683:        }
                   2684:        pVideoRaster = &STRam[VideoBase];
                   2685:        pSTScreen = pFrameBuffer->pSTScreen;
                   2686: 
                   2687:        Video_SetScreenRasters();
1.1.1.15  root     2688:        Video_InitShifterLines();
1.1.1.11  root     2689:        Spec512_StartVBL();
1.1.1.15  root     2690:        Video_StartHBL();                                       /* Init ShifterFrame.ShifterLines[0] */
1.1.1.11  root     2691: }
                   2692: 
                   2693: 
                   2694: /*-----------------------------------------------------------------------*/
                   2695: /**
                   2696:  * Get width, height and bpp according to TT-Resolution
                   2697:  */
                   2698: void Video_GetTTRes(int *width, int *height, int *bpp)
                   2699: {
                   2700:        switch (TTRes)
                   2701:        {
                   2702:         case ST_LOW_RES:   *width = 320;  *height = 200; *bpp = 4; break;
                   2703:         case ST_MEDIUM_RES:*width = 640;  *height = 200; *bpp = 2; break;
                   2704:         case ST_HIGH_RES:  *width = 640;  *height = 400; *bpp = 1; break;
                   2705:         case TT_LOW_RES:   *width = 320;  *height = 480; *bpp = 8; break;
                   2706:         case TT_MEDIUM_RES:*width = 640;  *height = 480; *bpp = 4; break;
                   2707:         case TT_HIGH_RES:  *width = 1280; *height = 960; *bpp = 1; break;
                   2708:         default:
                   2709:                fprintf(stderr, "TT res error!\n");
                   2710:                *width = 320; *height = 200; *bpp = 4;
                   2711:                break;
                   2712:        }
                   2713: }
                   2714: 
                   2715: 
                   2716: /*-----------------------------------------------------------------------*/
                   2717: /**
                   2718:  * Convert TT palette to SDL palette
                   2719:  */
                   2720: static void Video_UpdateTTPalette(int bpp)
                   2721: {
                   2722:        Uint32 ttpalette, src, dst;
                   2723:        Uint8 r,g,b, lowbyte, highbyte;
                   2724:        Uint16 stcolor, ttcolor;
                   2725:        int i, offset, colors;
                   2726: 
                   2727:        ttpalette = 0xff8400;
                   2728: 
                   2729:        if (!bTTColorsSTSync)
                   2730:        {
                   2731:                /* sync TT ST-palette to TT-palette */
                   2732:                src = 0xff8240; /* ST-palette */
                   2733:                offset = (IoMem_ReadWord(0xff8262) & 0x0f);
1.1.1.13  root     2734:                /*fprintf(stderr, "offset: %d\n", offset);*/
1.1.1.11  root     2735:                dst = ttpalette + offset * 16*SIZE_WORD;
                   2736: 
                   2737:                for (i = 0; i < 16; i++)
                   2738:                {
                   2739:                        stcolor = IoMem_ReadWord(src);
1.1.1.21  root     2740:                        ttcolor = ((stcolor&0x777) << 1) | ((stcolor&0x888) >> 3);
1.1.1.11  root     2741:                        IoMem_WriteWord(dst, ttcolor);
                   2742:                        src += SIZE_WORD;
                   2743:                        dst += SIZE_WORD;
                   2744:                }
1.1.1.15  root     2745:                bTTColorsSTSync = true;
1.1.1.11  root     2746:        }
                   2747: 
                   2748:        colors = 1 << bpp;
1.1.1.21  root     2749:        if ((bpp == 1) && (TTRes == TT_HIGH_RES))
1.1.1.11  root     2750:        {
                   2751:                /* Monochrome mode... palette is hardwired (?) */
                   2752:                HostScreen_setPaletteColor(0, 255, 255, 255);
                   2753:                HostScreen_setPaletteColor(1, 0, 0, 0);
                   2754:        }
1.1.1.21  root     2755:        else if (bpp == 1)
                   2756:        {
                   2757:                /* Monochrome mode... palette is taken from first and last TT color */
                   2758:                ttpalette = 0xff8400;
                   2759:                lowbyte = IoMem_ReadByte(ttpalette++);
                   2760:                highbyte = IoMem_ReadByte(ttpalette++);
                   2761:                r = (lowbyte  & 0x0f) << 4;
                   2762:                g = (highbyte & 0xf0);
                   2763:                b = (highbyte & 0x0f) << 4;
                   2764:                //printf("%d: (%d,%d,%d)\n", 0,r,g,b);
                   2765:                if(bTTHypermono)
                   2766:                {
                   2767:                        r = g = b = highbyte;
                   2768:                }
                   2769:                HostScreen_setPaletteColor(0, r,g,b);
                   2770: 
                   2771:                ttpalette = 0xff85fe;
                   2772:                lowbyte = IoMem_ReadByte(ttpalette++);
                   2773:                highbyte = IoMem_ReadByte(ttpalette++);
                   2774:                r = (lowbyte  & 0x0f) << 4;
                   2775:                g = (highbyte & 0xf0);
                   2776:                b = (highbyte & 0x0f) << 4;
                   2777:                if(bTTHypermono)
                   2778:                {
                   2779:                        r = g = b = highbyte;
                   2780:                }
                   2781:                //printf("%d: (%d,%d,%d)\n", 1,r,g,b);
                   2782:                HostScreen_setPaletteColor(1, r,g,b);
                   2783: 
                   2784:        }
1.1.1.11  root     2785:        else
                   2786:        {
                   2787:                for (i = 0; i < colors; i++)
                   2788:                {
                   2789:                        lowbyte = IoMem_ReadByte(ttpalette++);
                   2790:                        highbyte = IoMem_ReadByte(ttpalette++);
                   2791:                        r = (lowbyte  & 0x0f) << 4;
                   2792:                        g = (highbyte & 0xf0);
                   2793:                        b = (highbyte & 0x0f) << 4;
                   2794:                        //printf("%d: (%d,%d,%d)\n", i,r,g,b);
1.1.1.21  root     2795:                        if(bTTHypermono)
                   2796:                        {
                   2797:                                r = g = b = highbyte;
                   2798:                        }
1.1.1.11  root     2799:                        HostScreen_setPaletteColor(i, r,g,b);
                   2800:                }
                   2801:        }
                   2802: 
                   2803:        HostScreen_updatePalette(colors);
1.1.1.15  root     2804:        bTTColorsSync = true;
1.1.1.11  root     2805: }
                   2806: 
                   2807: 
                   2808: /*-----------------------------------------------------------------------*/
                   2809: /**
1.1.1.13  root     2810:  * Update TT palette and blit TT screen using VIDEL code.
                   2811:  * @return  true if the screen contents changed
1.1.1.11  root     2812:  */
1.1.1.16  root     2813: bool Video_RenderTTScreen(void)
1.1.1.11  root     2814: {
                   2815:        static int nPrevTTRes = -1;
                   2816:        int width, height, bpp;
                   2817: 
                   2818:        Video_GetTTRes(&width, &height, &bpp);
                   2819:        if (TTRes != nPrevTTRes)
                   2820:        {
                   2821:                HostScreen_setWindowSize(width, height, 8);
                   2822:                nPrevTTRes = TTRes;
                   2823:                if (bpp == 1)   /* Assert that mono palette will be used in mono mode */
1.1.1.15  root     2824:                        bTTColorsSync = false;
1.1.1.11  root     2825:        }
                   2826: 
                   2827:        /* colors need synching? */
                   2828:        if (!(bTTColorsSync && bTTColorsSTSync))
                   2829:        {
                   2830:                Video_UpdateTTPalette(bpp);
                   2831:        }
1.1.1.21  root     2832:        else if (TTSpecialVideoMode != nPrevTTSpecialVideoMode)
                   2833:        {
                   2834:                Video_UpdateTTPalette(bpp);
                   2835:                nPrevTTSpecialVideoMode = TTSpecialVideoMode;
                   2836:        }
1.1.1.11  root     2837: 
                   2838:        /* Yes, we are abusing the Videl routines for rendering the TT modes! */
                   2839:        if (!HostScreen_renderBegin())
1.1.1.13  root     2840:                return false;
1.1.1.16  root     2841:        if (nScreenZoomX * nScreenZoomY != 1)
1.1.1.11  root     2842:                VIDEL_ConvertScreenZoom(width, height, bpp, width * bpp / 16);
                   2843:        else
1.1.1.22! root     2844:                VIDEL_ConvertScreenNoZoom(width, height, bpp, width * bpp / 16);        
1.1.1.13  root     2845: 
1.1.1.22! root     2846:        HostScreen_update1(HostScreen_renderEnd(), false);
1.1.1.13  root     2847:        return true;
1.1.1.11  root     2848: }
                   2849: 
                   2850: 
                   2851: /*-----------------------------------------------------------------------*/
                   2852: /**
                   2853:  * Draw screen (either with ST/STE shifter drawing functions or with
                   2854:  * Videl drawing functions)
                   2855:  */
                   2856: static void Video_DrawScreen(void)
                   2857: {
                   2858:        /* Skip frame if need to */
1.1.1.13  root     2859:        if (nVBLs % (nFrameSkips+1))
1.1.1.11  root     2860:                return;
                   2861: 
                   2862:        /* Use extended VDI resolution?
                   2863:         * If so, just copy whole screen on VBL rather than per HBL */
                   2864:        if (bUseVDIRes)
                   2865:                Video_CopyVDIScreen();
                   2866: 
                   2867:        /* Now draw the screen! */
                   2868:        if (ConfigureParams.System.nMachineType == MACHINE_FALCON && !bUseVDIRes)
                   2869:        {
1.1.1.17  root     2870:                VIDEL_renderScreen();
1.1.1.11  root     2871:        }
                   2872:        else if (ConfigureParams.System.nMachineType == MACHINE_TT && !bUseVDIRes)
                   2873:        {
1.1.1.17  root     2874:                Video_RenderTTScreen();
1.1.1.11  root     2875:        }
                   2876:        else
1.1.1.13  root     2877:        {
                   2878:                /* Before drawing the screen, ensure all unused lines are cleared to color 0 */
                   2879:                /* (this can happen in 60 Hz when hatari is displaying the screen's border) */
                   2880:                /* pSTScreen was set during Video_CopyScreenLineColor */
1.1.1.14  root     2881:                if (!bUseVDIRes && nHBL < nLastVisibleHbl)
1.1.1.13  root     2882:                        memset(pSTScreen, 0, SCREENBYTES_LINE * ( nLastVisibleHbl - nHBL ) );
                   2883: 
1.1.1.17  root     2884:                Screen_Draw();
1.1.1.13  root     2885:        }
1.1.1.11  root     2886: }
                   2887: 
                   2888: 
                   2889: /*-----------------------------------------------------------------------*/
                   2890: /**
1.1.1.15  root     2891:  * Start HBL, Timer B and VBL interrupts.
                   2892:  */
                   2893: 
                   2894: 
                   2895: /**
                   2896:  * Start HBL or Timer B interrupt at position Pos. If position Pos was
                   2897:  * already reached, then the interrupt is set on the next line.
                   2898:  */
                   2899: 
                   2900: static void Video_AddInterrupt ( int Pos , interrupt_id Handler )
                   2901: {
                   2902:        int FrameCycles , HblCounterVideo , LineCycles;
                   2903: 
                   2904:        if ( nHBL >= nScanlinesPerFrame )
                   2905:          return;                               /* don't set a new hbl/timer B if we're on the last line, as the vbl will happen first */
                   2906:        
                   2907:        Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
1.1.1.16  root     2908: //fprintf ( stderr , "add int pos=%d handler=%d LineCycles=%d nCyclesPerLine=%d \n" , Pos , Handler , LineCycles , nCyclesPerLine );
1.1.1.15  root     2909: 
                   2910:        if ( LineCycles < Pos )                 /* changed before reaching the new Pos on the current line */
1.1.1.16  root     2911:                CycInt_AddRelativeInterrupt ( Pos - LineCycles , INT_CPU_CYCLE, Handler );
1.1.1.15  root     2912:        else                                    /* Pos will be applied on next line */
1.1.1.16  root     2913:                CycInt_AddRelativeInterrupt ( Pos - LineCycles + nCyclesPerLine , INT_CPU_CYCLE, Handler );
1.1.1.15  root     2914: }
                   2915: 
                   2916: 
                   2917: static void Video_AddInterruptHBL ( int Pos )
                   2918: {
1.1.1.16  root     2919: //fprintf ( stderr , "add hbl pos=%d\n" , Pos );
1.1.1.15  root     2920:        if ( !bUseVDIRes )
                   2921:                Video_AddInterrupt ( Pos , INTERRUPT_VIDEO_HBL );
                   2922: }
                   2923: 
                   2924: 
                   2925: void Video_AddInterruptTimerB ( int Pos )
                   2926: {
1.1.1.16  root     2927: //fprintf ( stderr , "add timerb pos=%d\n" , Pos );
1.1.1.15  root     2928:        if ( !bUseVDIRes )
                   2929:                Video_AddInterrupt ( Pos , INTERRUPT_VIDEO_ENDLINE );
                   2930: }
                   2931: 
                   2932: 
                   2933: /**
                   2934:  * Add some video interrupts to handle the first HBL and the first Timer B
                   2935:  * in a new VBL. Also add an interrupt to trigger the next VBL.
                   2936:  * This function is called from the VBL, so we use PendingCycleOver to take into account
                   2937:  * the possible delay occurring when the VBL was executed.
1.1.1.16  root     2938:  * In monochrome mode (71 Hz) a line is 224 cycles, which means if VBL is delayed
                   2939:  * by a DIVS, FrameCycles can already be > 224 and we need to add an immediate
                   2940:  * interrupt for hbl/timer in the next 4/8 cycles (else crash might happen as
                   2941:  * line 0 processing would be skipped).
1.1.1.11  root     2942:  */
1.1.1.15  root     2943: void Video_StartInterrupts ( int PendingCyclesOver )
1.1.1.11  root     2944: {
1.1.1.16  root     2945:        int FrameCycles , HblCounterVideo , LineCycles;
                   2946:        int Pos;
                   2947: 
1.1.1.15  root     2948:        /* HBL/Timer B are not emulated in VDI mode */
1.1.1.14  root     2949:        if (!bUseVDIRes)
                   2950:        {
1.1.1.16  root     2951:                Video_GetPosition ( &FrameCycles , &HblCounterVideo , &LineCycles );
1.1.1.15  root     2952: 
1.1.1.16  root     2953:                /* Set Timer B interrupt for line 0 */
                   2954:                Pos = Video_TimerB_GetPos ( 0 );
                   2955:                if ( Pos > FrameCycles )                /* check Pos for line 0 was not already reached */
                   2956:                        Video_AddInterruptTimerB ( Pos );
                   2957:                else                                    /* the VBL was delayed by more than 1 HBL, add an immediate timer B */
                   2958:                {
                   2959:                        LOG_TRACE(TRACE_VIDEO_VBL , "VBL %d delayed too much video_cyc=%d >= pos=%d for first timer B, add immediate timer B\n" ,
                   2960:                                nVBLs , FrameCycles , Pos );
                   2961:                        CycInt_AddRelativeInterrupt ( 4 , INT_CPU_CYCLE, INTERRUPT_VIDEO_ENDLINE );
                   2962:                }
                   2963: 
                   2964:                /* Set HBL interrupt for line 0 */
                   2965:                Pos = Video_HBL_GetPos();
                   2966:                if ( Pos > FrameCycles )                /* check Pos for line 0 was not already reached */
                   2967:                        Video_AddInterruptHBL ( Pos );
                   2968:                else                                    /* the VBL was delayed by more than 1 HBL, add an immediate HBL */
                   2969:                {
                   2970:                        LOG_TRACE(TRACE_VIDEO_VBL , "VBL %d delayed too much video_cyc=%d >= pos=%d for first HBL, add immediate HBL\n" ,
                   2971:                                nVBLs , FrameCycles , Pos );
                   2972:                        CycInt_AddRelativeInterrupt ( 8 , INT_CPU_CYCLE, INTERRUPT_VIDEO_HBL );         /* use 8 instead of 4 to happen after immediate timer b */
                   2973:                }
1.1.1.14  root     2974:        }
1.1.1.15  root     2975: 
                   2976:        /* TODO replace CYCLES_PER_FRAME */
1.1.1.16  root     2977:        CyclesPerVBL = CYCLES_PER_FRAME;
1.1.1.18  root     2978:        /* Note: Refresh rate less than 50 Hz does not make sense! */
                   2979:        assert(CyclesPerVBL <= CPU_FREQ/49);
                   2980:        /* Add new VBL interrupt: */
1.1.1.16  root     2981:        CycInt_AddRelativeInterrupt(CyclesPerVBL - PendingCyclesOver, INT_CPU_CYCLE, INTERRUPT_VIDEO_VBL);
1.1.1.11  root     2982: }
                   2983: 
                   2984: 
                   2985: /*-----------------------------------------------------------------------*/
                   2986: /**
1.1.1.15  root     2987:  * VBL interrupt : set new interrupts, draw screen, generate sound,
                   2988:  * reset counters, ...
1.1.1.11  root     2989:  */
1.1.1.15  root     2990: void Video_InterruptHandler_VBL ( void )
1.1.1.11  root     2991: {
                   2992:        int PendingCyclesOver;
1.1.1.9   root     2993: 
1.1.1.21  root     2994:        /* Store cycles we went over for this frame(this is our initial count) */
1.1.1.11  root     2995:        PendingCyclesOver = -INT_CONVERT_FROM_INTERNAL ( PendingInterruptCount , INT_CPU_CYCLE );    /* +ve */
1.1.1.10  root     2996: 
1.1.1.11  root     2997:        /* Remove this interrupt from list and re-order */
1.1.1.16  root     2998:        CycInt_AcknowledgeInterrupt();
1.1.1.9   root     2999: 
1.1.1.14  root     3000:        /* Increment the vbl jitter index */
                   3001:        VblJitterIndex++;
1.1.1.20  root     3002:        VblJitterIndex %= VBL_JITTER_ARRAY_SIZE;
1.1.1.14  root     3003:        
1.1.1.11  root     3004:        /* Set frame cycles, used for Video Address */
1.1.1.13  root     3005:        Cycles_SetCounter(CYCLES_COUNTER_VIDEO, PendingCyclesOver + VblVideoCycleOffset);
1.1.1.9   root     3006: 
1.1.1.11  root     3007:        /* Clear any key presses which are due to be de-bounced (held for one ST frame) */
                   3008:        Keymap_DebounceAllKeys();
1.1.1.9   root     3009: 
1.1.1.11  root     3010:        Video_DrawScreen();
1.1.1.9   root     3011: 
1.1.1.11  root     3012:        /* Check printer status */
                   3013:        Printer_CheckIdleStatus();
                   3014: 
                   3015:        /* Update counter for number of screen refreshes per second */
                   3016:        nVBLs++;
                   3017:        /* Set video registers for frame */
                   3018:        Video_ClearOnVBL();
1.1.1.15  root     3019: 
1.1.1.17  root     3020:        /* Videl Vertical counter reset (To be removed when Videl emulation is finished) */
                   3021:        if (ConfigureParams.System.nMachineType == MACHINE_FALCON) {
                   3022:                vfc_counter = 0;
                   3023:        }
                   3024:        
1.1.1.15  root     3025:        /* Since we don't execute HBL functions in VDI mode, we've got to
                   3026:         * initialize the first HBL palette here when VDI mode is enabled. */
                   3027:        if (bUseVDIRes)
                   3028:                Video_StoreFirstLinePalette();
                   3029: 
                   3030:        /* Start VBL, HBL and Timer B interrupts (this must be done after resetting
                   3031:          * video cycle counter setting default freq values in Video_ClearOnVBL) */
                   3032:        Video_StartInterrupts(PendingCyclesOver);
                   3033: 
                   3034:        /* Act on shortcut keys */
                   3035:        ShortCut_ActKey();
                   3036: 
1.1.1.21  root     3037:        /* Update the IKBD's internal clock */
                   3038:        IKBD_UpdateClockOnVBL ();
                   3039: 
1.1.1.16  root     3040:        /* Record video frame is necessary */
                   3041:        if ( bRecordingAvi )
                   3042:                Avi_RecordVideoStream ();
                   3043: 
1.1.1.11  root     3044:        /* Store off PSG registers for YM file, is enabled */
                   3045:        YMFormat_UpdateRecording();
                   3046:        /* Generate 1/50th second of sound sample data, to be played by sound thread */
                   3047:        Sound_Update_VBL();
                   3048: 
1.1.1.15  root     3049:        LOG_TRACE(TRACE_VIDEO_VBL , "VBL %d video_cyc=%d pending_cyc=%d jitter=%d\n" ,
1.1.1.14  root     3050:                       nVBLs , Cycles_GetCounter(CYCLES_COUNTER_VIDEO) , PendingCyclesOver , VblJitterArray[ VblJitterIndex ] );
1.1.1.11  root     3051: 
1.1.1.21  root     3052:        /* Print traces if pending VBL bit changed just before IACK when VBL interrupt is allowed */
                   3053:        if ( ( CPU_IACK == true ) && ( regs.intmask < 4 ) )
                   3054:        {
                   3055:                if ( pendingInterrupts & ( 1 << 4 ) )
                   3056:                {
                   3057:                        LOG_TRACE ( TRACE_VIDEO_VBL , "VBL %d, pending set again just before iack, skip one VBL interrupt video_cyc=%d pending_cyc=%d jitter=%d\n" ,
                   3058:                                nVBLs , Cycles_GetCounter(CYCLES_COUNTER_VIDEO) , PendingCyclesOver , VblJitterArray[ VblJitterIndex ] );
                   3059:                }
                   3060:                else
                   3061:                {
                   3062:                        LOG_TRACE ( TRACE_VIDEO_VBL , "VBL %d, new pending VBL set just before iack video_cyc=%d pending_cyc=%d jitter=%d\n" ,
                   3063:                                nVBLs , Cycles_GetCounter(CYCLES_COUNTER_VIDEO) , PendingCyclesOver , VblJitterArray[ VblJitterIndex ] );
                   3064:                }
                   3065:        }
                   3066: 
                   3067:        /* Set pending bit for VBL interrupt in the CPU IPL */
                   3068:        M68000_Exception(EXCEPTION_VBLANK, M68000_EXC_SRC_AUTOVEC);     /* Vertical blank interrupt, level 4 */
1.1.1.11  root     3069: 
                   3070:        Main_WaitOnVbl();
1.1.1.9   root     3071: }
                   3072: 
                   3073: 
                   3074: /*-----------------------------------------------------------------------*/
1.1.1.11  root     3075: /**
1.1.1.13  root     3076:  * Write to video address base high, med and low register (0xff8201/03/0d).
                   3077:  * On STE, when a program writes to high or med registers, base low register
                   3078:  * is reset to zero.
1.1.1.11  root     3079:  */
1.1.1.9   root     3080: void Video_ScreenBaseSTE_WriteByte(void)
                   3081: {
1.1.1.13  root     3082:        if ( ( IoAccessCurrentAddress == 0xff8201 ) || ( IoAccessCurrentAddress == 0xff8203 ) )
                   3083:                IoMem[0xff820d] = 0;          /* Reset screen base low register */
1.1.1.8   root     3084: 
1.1.1.15  root     3085:        if (LOG_TRACE_LEVEL(TRACE_VIDEO_STE))
1.1.1.11  root     3086:        {
1.1.1.15  root     3087:                int FrameCycles, HblCounterVideo, LineCycles;
                   3088: 
                   3089:                Video_GetPosition_OnWriteAccess ( &FrameCycles , &HblCounterVideo , &LineCycles );
                   3090:                
                   3091:                LOG_TRACE_PRINT ( "write ste video base=0x%x video_cyc_w=%d line_cyc_w=%d @ nHBL=%d/video_hbl_w=%d pc=%x instr_cyc=%d\n" ,
1.1.1.13  root     3092:                        (IoMem[0xff8201]<<16)+(IoMem[0xff8203]<<8)+IoMem[0xff820d] ,
1.1.1.15  root     3093:                        FrameCycles, LineCycles, nHBL, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles );
1.1.1.11  root     3094:        }
1.1.1.8   root     3095: }
                   3096: 
                   3097: /*-----------------------------------------------------------------------*/
1.1.1.11  root     3098: /**
                   3099:  * Read video address counter and update ff8205/07/09
                   3100:  */
                   3101: void Video_ScreenCounter_ReadByte(void)
1.1.1.8   root     3102: {
1.1.1.11  root     3103:        Uint32 addr;
1.1.1.8   root     3104: 
1.1.1.11  root     3105:        addr = Video_CalculateAddress();                /* get current video address */
                   3106:        IoMem[0xff8205] = ( addr >> 16 ) & 0xff;
                   3107:        IoMem[0xff8207] = ( addr >> 8 ) & 0xff;
                   3108:        IoMem[0xff8209] = addr & 0xff;
1.1.1.8   root     3109: }
                   3110: 
1.1.1.9   root     3111: /*-----------------------------------------------------------------------*/
1.1.1.11  root     3112: /**
                   3113:  * Write to video address counter (0xff8205, 0xff8207 and 0xff8209).
                   3114:  * Called on STE only and like with base address, you cannot set lowest bit.
1.1.1.16  root     3115:  *
                   3116:  * As Hatari processes/converts one complete video line at a time, we have 3 cases :
                   3117:  * - If display has not started yet for this line (left border), we can change pVideoRaster now.
                   3118: *    We must take into account that the MMU starts 16 cycles earlier when hscroll is used.
                   3119:  * - If display has stopped for this line (right border), we will change pVideoRaster
                   3120:  *   in Video_CopyScreenLineColor using pVideoRasterDelayed once the line has been processed.
                   3121:  * - If the write is made while display is on, then we must compute an offset of what
                   3122:  *   the new address should have been, to correctly emulate the video address at the
                   3123:  *   end of the line while taking into account the fact that the video pointer is incrementing
                   3124:  *   during the active part of the line (this is the most "tricky" case)
                   3125:  *
                   3126:  * To compute the new address, we must change only the byte that was modified and keep the two others ones.
1.1.1.11  root     3127:  */
1.1.1.9   root     3128: void Video_ScreenCounter_WriteByte(void)
                   3129: {
1.1.1.11  root     3130:        Uint8 AddrByte;
1.1.1.16  root     3131:        Uint32 addr_cur;
                   3132:        Uint32 addr_new = 0;
1.1.1.15  root     3133:        int FrameCycles, HblCounterVideo, LineCycles;
1.1.1.13  root     3134:        int Delayed;
1.1.1.16  root     3135:        int MMUStartCycle;
1.1.1.11  root     3136: 
1.1.1.15  root     3137:        Video_GetPosition_OnWriteAccess ( &FrameCycles , &HblCounterVideo , &LineCycles );
1.1.1.11  root     3138: 
                   3139:        AddrByte = IoMem[ IoAccessCurrentAddress ];
                   3140: 
1.1.1.16  root     3141:        /* Get current video address from the shifter */
                   3142:        addr_cur = Video_CalculateAddress();
                   3143:        /* Correct the address in case a modification of ff8205/07/09 was already delayed */
                   3144:        addr_new = addr_cur + VideoCounterDelayedOffset;
                   3145:        /* Correct the address in case video counter was already modified in the right border */
                   3146:        if ( pVideoRasterDelayed != NULL )
                   3147:                addr_new = pVideoRasterDelayed - STRam;
                   3148:        
                   3149:        /* addr_new should now be the same as on a real STE */
                   3150:        /* Compute the new video address with one modified byte */
                   3151:        if ( IoAccessCurrentAddress == 0xff8205 )
1.1.1.20  root     3152:                addr_new = ( addr_new & 0x00ffff ) | ( ( AddrByte & 0x3f ) << 16 );
1.1.1.16  root     3153:        else if ( IoAccessCurrentAddress == 0xff8207 )
                   3154:                addr_new = ( addr_new & 0xff00ff ) | ( AddrByte << 8 );
                   3155:        else if ( IoAccessCurrentAddress == 0xff8209 )
                   3156:                addr_new = ( addr_new & 0xffff00 ) | ( AddrByte );
                   3157: 
                   3158:        MMUStartCycle = Video_GetMMUStartCycle ( ShifterFrame.ShifterLines[ nHBL ].DisplayStartCycle );
                   3159: 
1.1.1.11  root     3160:        /* If display has not started, we can still modify pVideoRaster */
1.1.1.16  root     3161:        /* We must also check the write does not overlap the end of the line (to be sure Video_EndHBL is called first) */
                   3162:        if ( ( ( LineCycles <= MMUStartCycle ) && ( nHBL == HblCounterVideo ) )
1.1.1.18  root     3163:                || ( nHBL < nStartHBL ) || ( nHBL >= nEndHBL + BlankLines ) )
1.1.1.11  root     3164:        {
1.1.1.16  root     3165:                pVideoRaster = &STRam[addr_new & ~1];           /* set new video address */
                   3166:                VideoCounterDelayedOffset = 0;
                   3167:                pVideoRasterDelayed = NULL;
1.1.1.15  root     3168:                Delayed = false;
1.1.1.11  root     3169:        }
                   3170: 
1.1.1.16  root     3171:        /* Display is OFF (right border) but we can't change pVideoRaster now, we must process Video_CopyScreenLineColor first */
1.1.1.18  root     3172:        else if ( ( nHBL >= nStartHBL ) && ( nHBL < nEndHBL + BlankLines )      /* line should be active */
1.1.1.16  root     3173:                && ( ( LineCycles > ShifterFrame.ShifterLines[ nHBL ].DisplayEndCycle )         /* we're in the right border */
                   3174:                  || ( HblCounterVideo == nHBL+1 ) ) )          /* or the write overlaps the next line and Video_EndHBL was not called yet */
                   3175:        {
                   3176:                VideoCounterDelayedOffset = 0;
                   3177:                pVideoRasterDelayed = &STRam[addr_new & ~1];    /* new value for pVideoRaster at the end of Video_CopyScreenLineColor */
                   3178:                Delayed = true;
                   3179:        }
                   3180: 
                   3181:        /* Counter is modified while display is ON, store the bytes offset for Video_CopyScreenLineColor */
                   3182:        /* Even on a real STE, modifying video address in this case will cause artefacts */
1.1.1.11  root     3183:        else
                   3184:        {
1.1.1.16  root     3185:                VideoCounterDelayedOffset = addr_new - addr_cur;
                   3186:                pVideoRasterDelayed = NULL;
1.1.1.15  root     3187:                Delayed = true;
1.1.1.11  root     3188:        }
                   3189: 
1.1.1.16  root     3190:        LOG_TRACE(TRACE_VIDEO_STE , "write ste video %x val=0x%x video_old=%x video_new=%x offset=%x delayed=%s"
                   3191:                                " video_cyc_w=%d line_cyc_w=%d @ nHBL=%d/video_hbl_w=%d pc=%x instr_cyc=%d\n" ,
                   3192:                                IoAccessCurrentAddress, AddrByte, addr_cur , addr_new , VideoCounterDelayedOffset , Delayed ? "yes" : "no" ,
1.1.1.15  root     3193:                                FrameCycles, LineCycles, nHBL, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles );
1.1.1.9   root     3194: }
1.1.1.8   root     3195: 
                   3196: /*-----------------------------------------------------------------------*/
1.1.1.11  root     3197: /**
                   3198:  * Read video sync register (0xff820a)
                   3199:  */
1.1.1.8   root     3200: void Video_Sync_ReadByte(void)
                   3201: {
1.1.1.17  root     3202:        if ( (ConfigureParams.System.nMachineType == MACHINE_ST)
                   3203:          || (ConfigureParams.System.nMachineType == MACHINE_STE)
                   3204:          || (ConfigureParams.System.nMachineType == MACHINE_MEGA_STE) )
1.1.1.14  root     3205:                IoMem[0xff820a] |= 0xfc;                /* set unused bits 2-7 to 1 */
1.1.1.8   root     3206: }
                   3207: 
                   3208: /*-----------------------------------------------------------------------*/
1.1.1.11  root     3209: /**
                   3210:  * Read video base address low byte (0xff820d). A plain ST can only store
                   3211:  * screen addresses rounded to 256 bytes (i.e. no lower byte).
                   3212:  */
1.1.1.8   root     3213: void Video_BaseLow_ReadByte(void)
                   3214: {
1.1.1.11  root     3215:        if (ConfigureParams.System.nMachineType == MACHINE_ST)
                   3216:                IoMem[0xff820d] = 0;        /* On ST this is always 0 */
1.1.1.9   root     3217: 
1.1.1.11  root     3218:        /* Note that you should not do anything here for STe because
                   3219:         * VideoBase address is set in an interrupt and would be wrong
                   3220:         * here.   It's fine like this.
                   3221:         */
1.1.1.8   root     3222: }
                   3223: 
                   3224: /*-----------------------------------------------------------------------*/
1.1.1.11  root     3225: /**
                   3226:  * Read video line width register (0xff820f)
                   3227:  */
1.1.1.8   root     3228: void Video_LineWidth_ReadByte(void)
                   3229: {
1.1.1.11  root     3230:        if (ConfigureParams.System.nMachineType == MACHINE_ST)
                   3231:                IoMem[0xff820f] = 0;        /* On ST this is always 0 */
1.1.1.18  root     3232: 
                   3233:        /* If we're not in STF mode, we use the value already stored in $ff820f */
1.1.1.8   root     3234: }
                   3235: 
                   3236: /*-----------------------------------------------------------------------*/
1.1.1.11  root     3237: /**
                   3238:  * Read video shifter mode register (0xff8260)
                   3239:  */
1.1.1.8   root     3240: void Video_ShifterMode_ReadByte(void)
                   3241: {
1.1.1.11  root     3242:        if (bUseHighRes)
1.1.1.14  root     3243:                IoMem[0xff8260] = 2;                    /* If mono monitor, force to high resolution */
                   3244: 
1.1.1.17  root     3245:        if (ConfigureParams.System.nMachineType == MACHINE_ST)
                   3246:                IoMem[0xff8260] |= 0xfc;                /* On STF, set unused bits 2-7 to 1 */
1.1.1.20  root     3247:        else
                   3248:                IoMem[0xff8260] &= 0x03;                /* Only use bits 0 and 1, unused bits 2-7 are set to 0 */
1.1.1.8   root     3249: }
                   3250: 
1.1.1.10  root     3251: /*-----------------------------------------------------------------------*/
1.1.1.11  root     3252: /**
                   3253:  * Read horizontal scroll register (0xff8265)
                   3254:  */
1.1.1.10  root     3255: void Video_HorScroll_Read(void)
                   3256: {
1.1.1.11  root     3257:        IoMem[0xff8265] = HWScrollCount;
1.1.1.10  root     3258: }
1.1.1.8   root     3259: 
                   3260: /*-----------------------------------------------------------------------*/
1.1.1.11  root     3261: /**
                   3262:  * Write video line width register (0xff820f) - STE only.
                   3263:  * Content of LineWidth is added to the shifter counter when display is
                   3264:  * turned off (start of the right border, usually at cycle 376)
                   3265:  */
1.1.1.10  root     3266: void Video_LineWidth_WriteByte(void)
                   3267: {
1.1.1.11  root     3268:        Uint8 NewWidth;
1.1.1.15  root     3269:        int FrameCycles, HblCounterVideo, LineCycles;
1.1.1.13  root     3270:        int Delayed;
1.1.1.11  root     3271: 
1.1.1.15  root     3272:        Video_GetPosition_OnWriteAccess ( &FrameCycles , &HblCounterVideo , &LineCycles );
1.1.1.11  root     3273: 
                   3274:        NewWidth = IoMem_ReadByte(0xff820f);
                   3275: 
                   3276:        /* We must also check the write does not overlap the end of the line */
1.1.1.15  root     3277:        if ( ( ( nHBL == HblCounterVideo ) && ( LineCycles <= ShifterFrame.ShifterLines[ HblCounterVideo ].DisplayEndCycle ) )
1.1.1.18  root     3278:                || ( nHBL < nStartHBL ) || ( nHBL >= nEndHBL + BlankLines ) )
1.1.1.13  root     3279:        {
1.1.1.11  root     3280:                LineWidth = NewWidth;           /* display is on, we can still change */
1.1.1.13  root     3281:                NewLineWidth = -1;              /* cancel 'pending' change */
1.1.1.15  root     3282:                Delayed = false;
1.1.1.13  root     3283:        }
1.1.1.11  root     3284:        else
1.1.1.13  root     3285:        {
1.1.1.11  root     3286:                NewLineWidth = NewWidth;        /* display is off, can't change LineWidth once in right border */
1.1.1.15  root     3287:                Delayed = true;
1.1.1.13  root     3288:        }
1.1.1.11  root     3289: 
1.1.1.15  root     3290:        LOG_TRACE(TRACE_VIDEO_STE , "write ste linewidth=0x%x delayed=%s video_cyc_w=%d line_cyc_w=%d @ nHBL=%d/video_hbl_w=%d pc=%x instr_cyc=%d\n",
1.1.1.13  root     3291:                                        NewWidth, Delayed ? "yes" : "no" ,
1.1.1.15  root     3292:                                        FrameCycles, LineCycles, nHBL, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles );
1.1.1.10  root     3293: }
                   3294: 
                   3295: /*-----------------------------------------------------------------------*/
1.1.1.11  root     3296: /**
                   3297:  * Write to video shifter palette registers (0xff8240-0xff825e)
1.1.1.14  root     3298:  *
                   3299:  * Note that there's a special "strange" case when writing only to the upper byte
                   3300:  * of the color reg (instead of writing 16 bits at once with .W/.L).
                   3301:  * In that case, the byte written to address x is automatically written
                   3302:  * to address x+1 too (but we shouldn't copy x in x+1 after masking x ; we apply the mask at the end)
1.1.1.22! root     3303:  * Similarly, when writing a byte to address x+1, it's also written to address x
1.1.1.14  root     3304:  * So :        move.w #0,$ff8240       -> color 0 is now $000
                   3305:  *     move.b #7,$ff8240       -> color 0 is now $707 !
1.1.1.22! root     3306:  *     move.b #$55,$ff8241     -> color 0 is now $555 !
1.1.1.14  root     3307:  *     move.b #$71,$ff8240     -> color 0 is now $171 (bytes are first copied, then masked)
1.1.1.11  root     3308:  */
1.1.1.22! root     3309: void Video_ColorReg_WriteWord(void)
1.1.1.8   root     3310: {
1.1.1.15  root     3311:        if (!bUseHighRes && !bUseVDIRes)               /* Don't store if hi-res or VDI resolution */
1.1.1.11  root     3312:        {
                   3313:                int idx;
                   3314:                Uint16 col;
1.1.1.22! root     3315:                Uint32 addr;
        !          3316:                addr = IoAccessCurrentAddress;
        !          3317: 
1.1.1.11  root     3318:                Video_SetHBLPaletteMaskPointers();     /* Set 'pHBLPalettes' etc.. according cycles into frame */
1.1.1.14  root     3319: 
                   3320:                /* Handle special case when writing only to the upper byte of the color reg */
                   3321:                if ( ( nIoMemAccessSize == SIZE_BYTE ) && ( ( IoAccessCurrentAddress & 1 ) == 0 ) )
                   3322:                        col = ( IoMem_ReadByte(addr) << 8 ) + IoMem_ReadByte(addr);             /* copy upper byte into lower byte */
1.1.1.22! root     3323:                /* Same when writing only to the lower byte of the color reg */
        !          3324:                else if ( ( nIoMemAccessSize == SIZE_BYTE ) && ( ( IoAccessCurrentAddress & 1 ) == 1 ) )
        !          3325:                        col = ( IoMem_ReadByte(addr) << 8 ) + IoMem_ReadByte(addr);             /* copy lower byte into upper byte */
        !          3326:                /* Usual case, writing a word or a long (2 words) */
        !          3327:                else
        !          3328:                        col = IoMem_ReadWord(addr);
1.1.1.14  root     3329: 
1.1.1.11  root     3330:                if (ConfigureParams.System.nMachineType == MACHINE_ST)
1.1.1.22! root     3331:                        col &= 0x777;                   /* Mask off to ST 512 palette */
1.1.1.11  root     3332:                else
1.1.1.22! root     3333:                        col &= 0xfff;                   /* Mask off to STe 4096 palette */
        !          3334: 
        !          3335:                addr &= 0xfffffffe;                     /* Ensure addr is even to store the 16 bit color */
        !          3336:                        
1.1.1.11  root     3337:                IoMem_WriteWord(addr, col);            /* (some games write 0xFFFF and read back to see if STe) */
                   3338:                Spec512_StoreCyclePalette(col, addr);  /* Store colour into CyclePalettes[] */
                   3339:                idx = (addr-0xff8240)/2;               /* words */
                   3340:                pHBLPalettes[idx] = col;               /* Set colour x */
                   3341:                *pHBLPaletteMasks |= 1 << idx;         /* And mask */
                   3342: 
1.1.1.15  root     3343:                if (LOG_TRACE_LEVEL(TRACE_VIDEO_COLOR))
1.1.1.11  root     3344:                {
1.1.1.15  root     3345:                        int FrameCycles, HblCounterVideo, LineCycles;
                   3346: 
                   3347:                        Video_GetPosition_OnWriteAccess ( &FrameCycles , &HblCounterVideo , &LineCycles );
                   3348: 
                   3349:                        LOG_TRACE_PRINT ( "write col addr=%x col=%x video_cyc_w=%d line_cyc_w=%d @ nHBL=%d/video_hbl_w=%d pc=%x instr_cyc=%d\n" ,
1.1.1.22! root     3350:                                IoAccessCurrentAddress, col,
1.1.1.15  root     3351:                                FrameCycles, LineCycles, nHBL, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles );
1.1.1.11  root     3352:                }
                   3353: 
                   3354:        }
1.1.1.8   root     3355: }
                   3356: 
1.1.1.22! root     3357: /*
        !          3358:  * Read from video shifter palette registers (0xff8240-0xff825e)
        !          3359:  *
        !          3360:  * NOTE [NP] : On STF, only 3 bits are used for RGB (instead of 4 on STE) ;
        !          3361:  * the content of bits 3, 7 and 11 is not defined and will be 0 or 1
        !          3362:  * depending on the latest activity on the BUS (last word access by the CPU or
        !          3363:  * the shifter). As precisely emulating these bits is quite complicated,
        !          3364:  * we use random values for now.
        !          3365:  * NOTE [NP] : When executing code from the IO addresses between 0xff8240-0xff825e
        !          3366:  * the unused bits on STF are set to '0' (used in "The Union Demo" protection).
        !          3367:  * So we use rand() only if PC is located in RAM.
        !          3368:  */
        !          3369: void Video_ColorReg_ReadWord(void)
        !          3370: {
        !          3371:        Uint16 col;
        !          3372:        Uint32 addr;
        !          3373:        addr = IoAccessCurrentAddress;
        !          3374: 
        !          3375:        col = IoMem_ReadWord(addr);
        !          3376: 
        !          3377:        if ( (ConfigureParams.System.nMachineType == MACHINE_ST)
        !          3378:          && ( M68000_GetPC() < 0x400000 ) )                            /* PC in RAM < 4MB */
        !          3379:        {
        !          3380:                col = ( col & 0x777 ) | ( rand() & 0x888 );
        !          3381:                IoMem_WriteWord ( addr , col );
        !          3382:        }
        !          3383: 
        !          3384:        if (LOG_TRACE_LEVEL(TRACE_VIDEO_COLOR))
        !          3385:        {
        !          3386:                int FrameCycles, HblCounterVideo, LineCycles;
        !          3387: 
        !          3388:                Video_GetPosition_OnReadAccess ( &FrameCycles , &HblCounterVideo , &LineCycles );
        !          3389: 
        !          3390:                LOG_TRACE_PRINT ( "read col addr=%x col=%x video_cyc_w=%d line_cyc_w=%d @ nHBL=%d/video_hbl_w=%d pc=%x instr_cyc=%d\n" ,
        !          3391:                        IoAccessCurrentAddress, col,
        !          3392:                        FrameCycles, LineCycles, nHBL, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles );
        !          3393:        }
        !          3394: }
        !          3395: 
        !          3396: /*
        !          3397:  * [NP] TODO : due to how .L accesses are handled in ioMem.c, we can't call directly
        !          3398:  * Video_ColorReg_WriteWord from ioMemTabST.c / ioMemTabSTE.c, we must use an intermediate
        !          3399:  * function, else .L accesses will not change 2 .W color regs, but only one.
        !          3400:  * This should be changed in ioMem.c to do 2 separate .W accesses, as would do a real 68000
        !          3401:  */
        !          3402: 
1.1.1.8   root     3403: void Video_Color0_WriteWord(void)
                   3404: {
1.1.1.22! root     3405:        Video_ColorReg_WriteWord();
1.1.1.8   root     3406: }
                   3407: 
                   3408: void Video_Color1_WriteWord(void)
                   3409: {
1.1.1.22! root     3410:        Video_ColorReg_WriteWord();
1.1.1.8   root     3411: }
                   3412: 
                   3413: void Video_Color2_WriteWord(void)
                   3414: {
1.1.1.22! root     3415:        Video_ColorReg_WriteWord();
1.1.1.8   root     3416: }
                   3417: 
                   3418: void Video_Color3_WriteWord(void)
                   3419: {
1.1.1.22! root     3420:        Video_ColorReg_WriteWord();
1.1.1.8   root     3421: }
                   3422: 
                   3423: void Video_Color4_WriteWord(void)
                   3424: {
1.1.1.22! root     3425:        Video_ColorReg_WriteWord();
1.1.1.8   root     3426: }
                   3427: 
                   3428: void Video_Color5_WriteWord(void)
                   3429: {
1.1.1.22! root     3430:        Video_ColorReg_WriteWord();
1.1.1.8   root     3431: }
                   3432: 
                   3433: void Video_Color6_WriteWord(void)
                   3434: {
1.1.1.22! root     3435:        Video_ColorReg_WriteWord();
1.1.1.8   root     3436: }
                   3437: 
                   3438: void Video_Color7_WriteWord(void)
                   3439: {
1.1.1.22! root     3440:        Video_ColorReg_WriteWord();
1.1.1.8   root     3441: }
                   3442: 
                   3443: void Video_Color8_WriteWord(void)
                   3444: {
1.1.1.22! root     3445:        Video_ColorReg_WriteWord();
1.1.1.8   root     3446: }
                   3447: 
                   3448: void Video_Color9_WriteWord(void)
                   3449: {
1.1.1.22! root     3450:        Video_ColorReg_WriteWord();
1.1.1.8   root     3451: }
                   3452: 
                   3453: void Video_Color10_WriteWord(void)
                   3454: {
1.1.1.22! root     3455:        Video_ColorReg_WriteWord();
1.1.1.8   root     3456: }
                   3457: 
                   3458: void Video_Color11_WriteWord(void)
                   3459: {
1.1.1.22! root     3460:        Video_ColorReg_WriteWord();
1.1.1.8   root     3461: }
                   3462: 
                   3463: void Video_Color12_WriteWord(void)
                   3464: {
1.1.1.22! root     3465:        Video_ColorReg_WriteWord();
1.1.1.8   root     3466: }
                   3467: 
                   3468: void Video_Color13_WriteWord(void)
                   3469: {
1.1.1.22! root     3470:        Video_ColorReg_WriteWord();
1.1.1.8   root     3471: }
                   3472: 
                   3473: void Video_Color14_WriteWord(void)
                   3474: {
1.1.1.22! root     3475:        Video_ColorReg_WriteWord();
1.1.1.8   root     3476: }
                   3477: 
                   3478: void Video_Color15_WriteWord(void)
                   3479: {
1.1.1.22! root     3480:        Video_ColorReg_WriteWord();
        !          3481: }
        !          3482: 
        !          3483: 
        !          3484: void Video_Color0_ReadWord(void)
        !          3485: {
        !          3486:        Video_ColorReg_ReadWord();
        !          3487: }
        !          3488: 
        !          3489: void Video_Color1_ReadWord(void)
        !          3490: {
        !          3491:        Video_ColorReg_ReadWord();
        !          3492: }
        !          3493: 
        !          3494: void Video_Color2_ReadWord(void)
        !          3495: {
        !          3496:        Video_ColorReg_ReadWord();
        !          3497: }
        !          3498: 
        !          3499: void Video_Color3_ReadWord(void)
        !          3500: {
        !          3501:        Video_ColorReg_ReadWord();
        !          3502: }
        !          3503: 
        !          3504: void Video_Color4_ReadWord(void)
        !          3505: {
        !          3506:        Video_ColorReg_ReadWord();
        !          3507: }
        !          3508: 
        !          3509: void Video_Color5_ReadWord(void)
        !          3510: {
        !          3511:        Video_ColorReg_ReadWord();
        !          3512: }
        !          3513: 
        !          3514: void Video_Color6_ReadWord(void)
        !          3515: {
        !          3516:        Video_ColorReg_ReadWord();
        !          3517: }
        !          3518: 
        !          3519: void Video_Color7_ReadWord(void)
        !          3520: {
        !          3521:        Video_ColorReg_ReadWord();
        !          3522: }
        !          3523: 
        !          3524: void Video_Color8_ReadWord(void)
        !          3525: {
        !          3526:        Video_ColorReg_ReadWord();
        !          3527: }
        !          3528: 
        !          3529: void Video_Color9_ReadWord(void)
        !          3530: {
        !          3531:        Video_ColorReg_ReadWord();
        !          3532: }
        !          3533: 
        !          3534: void Video_Color10_ReadWord(void)
        !          3535: {
        !          3536:        Video_ColorReg_ReadWord();
        !          3537: }
        !          3538: 
        !          3539: void Video_Color11_ReadWord(void)
        !          3540: {
        !          3541:        Video_ColorReg_ReadWord();
        !          3542: }
        !          3543: 
        !          3544: void Video_Color12_ReadWord(void)
        !          3545: {
        !          3546:        Video_ColorReg_ReadWord();
        !          3547: }
        !          3548: 
        !          3549: void Video_Color13_ReadWord(void)
        !          3550: {
        !          3551:        Video_ColorReg_ReadWord();
        !          3552: }
        !          3553: 
        !          3554: void Video_Color14_ReadWord(void)
        !          3555: {
        !          3556:        Video_ColorReg_ReadWord();
        !          3557: }
        !          3558: 
        !          3559: void Video_Color15_ReadWord(void)
        !          3560: {
        !          3561:        Video_ColorReg_ReadWord();
1.1.1.8   root     3562: }
                   3563: 
                   3564: 
                   3565: /*-----------------------------------------------------------------------*/
1.1.1.11  root     3566: /**
                   3567:  * Write video shifter mode register (0xff8260)
                   3568:  */
1.1.1.8   root     3569: void Video_ShifterMode_WriteByte(void)
                   3570: {
1.1.1.20  root     3571:        Uint8 VideoShifterByte;
                   3572: 
                   3573:        if (ConfigureParams.System.nMachineType == MACHINE_TT)
                   3574:        {
                   3575:                TTRes = IoMem_ReadByte(0xff8260) & 7;
                   3576:                /* Copy to TT shifter mode register: */
                   3577:                IoMem_WriteByte(0xff8262, TTRes);
1.1.1.21  root     3578: 
                   3579:                bTTSampleHold = false;
                   3580:                bTTHypermono = false;
1.1.1.20  root     3581:        }
                   3582:        else if (!bUseVDIRes)   /* ST and STE mode */
1.1.1.11  root     3583:        {
1.1.1.18  root     3584:                /* We only care for lower 2-bits */
                   3585:                VideoShifterByte = IoMem[0xff8260] & 3;
1.1.1.22! root     3586:                /* 3 is not a valid resolution, use high res instead */
1.1.1.18  root     3587:                if ( VideoShifterByte == 3 )
1.1.1.13  root     3588:                {
1.1.1.22! root     3589:                        VideoShifterByte = 2;
        !          3590:                        IoMem_WriteByte(0xff8260,2);
1.1.1.13  root     3591:                }
1.1.1.18  root     3592: 
1.1.1.11  root     3593:                Video_WriteToShifter(VideoShifterByte);
                   3594:                Video_SetHBLPaletteMaskPointers();
                   3595:                *pHBLPaletteMasks &= 0xff00ffff;
                   3596:                /* Store resolution after palette mask and set resolution write bit: */
                   3597:                *pHBLPaletteMasks |= (((Uint32)VideoShifterByte|0x04)<<16);
                   3598:        }
1.1.1.10  root     3599: }
                   3600: 
                   3601: /*-----------------------------------------------------------------------*/
1.1.1.11  root     3602: /**
1.1.1.13  root     3603:  * Handle horizontal scrolling to the left.
                   3604:  * On STE, there're 2 registers that can scroll the line :
                   3605:  *  - $ff8264 : scroll without prefetch
1.1.1.14  root     3606:  *  - $ff8265 : scroll with prefetch
1.1.1.13  root     3607:  * Both registers will scroll the line to the left by skipping the amount
                   3608:  * of pixels in $ff8264 or $ff8265 (from 0 to 15).
                   3609:  * As some pixels will be skipped, this means the shifter needs to read
                   3610:  * 16 other pixels in advance in some internal registers to have an uninterrupted flow of pixels.
                   3611:  *
1.1.1.14  root     3612:  * These 16 pixels can be prefetched before the display starts (on cycle 56 for example) when using
1.1.1.13  root     3613:  * $ff8265 to scroll the line. In that case 8 more bytes per line (low res) will be read. Most programs
                   3614:  * are using $ff8265 to scroll the line.
                   3615:  *
                   3616:  * When using $ff8264, the next 16 pixels will not be prefetched before the display
                   3617:  * starts, they will be read when the display normally starts (cycle 56). While
                   3618:  * reading these 16 pixels, the shifter won't be able to display anything, which will
                   3619:  * result in 16 pixels having the color 0. So, reading the 16 pixels will in fact delay
                   3620:  * the real start of the line, which will look as if it started 16 pixels later. As the
                   3621:  * shifter will stop the display at cycle 56+320 anyway, this means the last 16 pixels
                   3622:  * of each line won't be displayed and you get the equivalent of a shorter 304 pixels line.
                   3623:  * As a consequence, this register is rarely used to scroll the line.
                   3624:  *
1.1.1.21  root     3625:  * By writing a value > 0 in $ff8265 (to start prefetching) and immediately after a value of 0
1.1.1.13  root     3626:  * in $ff8264 (no scroll and no prefetch), it's possible to fill the internal registers used
                   3627:  * for the scrolling even if scrolling is set to 0. In that case, the shifter will start displaying
                   3628:  * each line 16 pixels earlier (as the data are already available in the internal registers).
                   3629:  * This allows to have 336 pixels per line (instead of 320) for all the remaining lines on the screen.
                   3630:  *
                   3631:  * Although some programs are using this sequence :
                   3632:  *     move.w  #1,$ffff8264            ; Word access!
                   3633:  *     clr.b   $ffff8264               ; Byte access!
                   3634:  * It is also possible to add 16 pixels by doing :
                   3635:  *     move.b  #X,$ff8265              ; with X > 0
                   3636:  *     move.b  #0,$ff8264
1.1.1.11  root     3637:  * Some games (Obsession, Skulls) and demos (Pacemaker by Paradox) use this
                   3638:  * feature to increase the resolution, so we have to emulate this bug, too!
1.1.1.13  root     3639:  *
                   3640:  * So considering a low res line of 320 pixels (160 bytes) :
                   3641:  *     - if both $ff8264/65 are 0, no scrolling happens, the shifter reads 160 bytes and displays 320 pixels (same as STF)
                   3642:  *     - if $ff8265 > 0, line is scrolled, the shifter reads 168 bytes and displays 320 pixels.
                   3643:  *     - if $ff8264 > 0, line is scrolled, the shifter reads 160 bytes and displays 304 pixels,
                   3644:  *             the display starts 16 pixels later.
                   3645:  *     - if $ff8265 > 0 and then $ff8264 = 0, there's no scrolling, the shifter reads 168 bytes and displays 336 pixels,
                   3646:  *             the display starts 16 pixels earlier.
1.1.1.11  root     3647:  */
1.1.1.13  root     3648: 
                   3649: void Video_HorScroll_Write_8264(void)
                   3650: {
                   3651:        Video_HorScroll_Write();
                   3652: }
                   3653: 
                   3654: void Video_HorScroll_Write_8265(void)
                   3655: {
                   3656:        Video_HorScroll_Write();
                   3657: }
                   3658: 
1.1.1.10  root     3659: void Video_HorScroll_Write(void)
                   3660: {
1.1.1.13  root     3661:        Uint32 RegAddr;
1.1.1.11  root     3662:        Uint8 ScrollCount;
1.1.1.13  root     3663:        Uint8 Prefetch;
1.1.1.15  root     3664:        int FrameCycles, HblCounterVideo, LineCycles;
                   3665:        bool Add16px = false;
1.1.1.13  root     3666:        static Uint8 LastVal8265 = 0;
                   3667:        int Delayed;
                   3668: 
1.1.1.15  root     3669:        Video_GetPosition_OnWriteAccess ( &FrameCycles , &HblCounterVideo , &LineCycles );
1.1.1.10  root     3670: 
1.1.1.13  root     3671:        RegAddr = IoAccessCurrentAddress;               /* 0xff8264 or 0xff8265 */
                   3672:        ScrollCount = IoMem[ RegAddr ];
1.1.1.11  root     3673:        ScrollCount &= 0x0f;
1.1.1.10  root     3674: 
1.1.1.13  root     3675:        if ( RegAddr == 0xff8264 )
                   3676:        {
                   3677:                Prefetch = 0;                           /* scroll without prefetch */
1.1.1.15  root     3678:                LastCycleScroll8264 = FrameCycles;
1.1.1.11  root     3679: 
1.1.1.15  root     3680:                ShifterFrame.Scroll8264Pos.VBL = nVBLs;
                   3681:                ShifterFrame.Scroll8264Pos.FrameCycles = FrameCycles;
                   3682:                ShifterFrame.Scroll8264Pos.HBL = HblCounterVideo;
                   3683:                ShifterFrame.Scroll8264Pos.LineCycles = LineCycles;
                   3684: 
                   3685:                if ( ( ScrollCount == 0 ) && ( LastVal8265 > 0 )
                   3686:                        && ( ShifterFrame.Scroll8265Pos.VBL > 0 )               /* a write to ff8265 has been made */
                   3687:                        && ( ShifterFrame.Scroll8265Pos.VBL == ShifterFrame.Scroll8264Pos.VBL )         /* during the same VBL */
                   3688:                        && ( ShifterFrame.Scroll8264Pos.FrameCycles - ShifterFrame.Scroll8265Pos.FrameCycles <= 40 ) )
1.1.1.13  root     3689:                {
1.1.1.15  root     3690:                        LOG_TRACE(TRACE_VIDEO_BORDER_H , "detect ste left+16 pixels\n" );
                   3691:                        Add16px = true;
1.1.1.13  root     3692:                }
                   3693:        }
                   3694:        else
1.1.1.11  root     3695:        {
1.1.1.13  root     3696:                Prefetch = 1;                           /* scroll with prefetch */
1.1.1.15  root     3697:                LastCycleScroll8265 = FrameCycles;
                   3698: 
                   3699:                ShifterFrame.Scroll8265Pos.VBL = nVBLs;
                   3700:                ShifterFrame.Scroll8265Pos.FrameCycles = FrameCycles;
                   3701:                ShifterFrame.Scroll8265Pos.HBL = HblCounterVideo;
                   3702:                ShifterFrame.Scroll8265Pos.LineCycles = LineCycles;
                   3703: 
1.1.1.13  root     3704:                LastVal8265 = ScrollCount;
1.1.1.15  root     3705:                Add16px = false;
1.1.1.11  root     3706:        }
1.1.1.13  root     3707: 
                   3708: 
                   3709:        /* If the write was made before display starts on the current line, then */
                   3710:        /* we can still change the value now. Else, the new values will be used */
                   3711:        /* for line n+1. */
                   3712:        /* We must also check the write does not overlap the end of the line */
1.1.1.15  root     3713:        if ( ( ( LineCycles <= LINE_START_CYCLE_50 ) && ( nHBL == HblCounterVideo ) )
1.1.1.18  root     3714:                || ( nHBL < nStartHBL ) || ( nHBL >= nEndHBL + BlankLines ) )
1.1.1.11  root     3715:        {
1.1.1.13  root     3716:                HWScrollCount = ScrollCount;            /* display has not started, we can still change */
                   3717:                HWScrollPrefetch = Prefetch;
                   3718:                bSteBorderFlag = Add16px;
                   3719:                NewHWScrollCount = -1;                  /* cancel 'pending' change */
1.1.1.15  root     3720:                Delayed = false;
1.1.1.11  root     3721:        }
                   3722:        else
                   3723:        {
1.1.1.13  root     3724:                NewHWScrollCount = ScrollCount;         /* display has started, can't change HWScrollCount now */
                   3725:                NewHWScrollPrefetch = Prefetch;
                   3726:                if ( Add16px )
                   3727:                        NewSteBorderFlag = 1;
                   3728:                else
                   3729:                        NewSteBorderFlag = 0;
1.1.1.15  root     3730:                Delayed = true;
1.1.1.11  root     3731:        }
                   3732: 
1.1.1.15  root     3733:        LOG_TRACE(TRACE_VIDEO_STE , "write ste %x hwscroll=%x delayed=%s video_cyc_w=%d line_cyc_w=%d @ nHBL=%d/video_hbl_w=%d pc=%x instr_cyc=%d\n" ,
1.1.1.13  root     3734:                RegAddr , ScrollCount, Delayed ? "yes" : "no" ,
1.1.1.15  root     3735:                FrameCycles, LineCycles, nHBL, HblCounterVideo, M68000_GetPC(), CurrentInstrCycles );
1.1.1.13  root     3736: }
1.1.1.11  root     3737: 
                   3738: /*-----------------------------------------------------------------------*/
                   3739: /**
                   3740:  * Write to TT shifter mode register (0xff8262)
                   3741:  */
                   3742: void Video_TTShiftMode_WriteWord(void)
                   3743: {
                   3744:        TTRes = IoMem_ReadByte(0xff8262) & 7;
1.1.1.21  root     3745:        TTSpecialVideoMode = IoMem_ReadByte(0xff8262) & 0x90;
1.1.1.11  root     3746: 
                   3747:        /*fprintf(stderr, "Write to FF8262: %x, res=%i\n", IoMem_ReadWord(0xff8262), TTRes);*/
                   3748: 
                   3749:        /* Is it an ST compatible resolution? */
                   3750:        if (TTRes <= 2)
                   3751:        {
                   3752:                IoMem_WriteByte(0xff8260, TTRes);
                   3753:                Video_ShifterMode_WriteByte();
1.1.1.21  root     3754:                IoMem_WriteByte(0xff8262, TTRes | TTSpecialVideoMode);
                   3755:        }
                   3756: 
                   3757:        if(TTSpecialVideoMode & 0x80)
                   3758:        {
                   3759:                bTTSampleHold = true;
                   3760:        }
                   3761:        else
                   3762:        {
                   3763:                bTTSampleHold = false;
                   3764:        }
                   3765: 
                   3766:        if(TTSpecialVideoMode & 0x10)
                   3767:        {
                   3768:                bTTHypermono = true;
                   3769:        }
                   3770:        else
                   3771:        {
                   3772:                bTTHypermono = false;
1.1.1.11  root     3773:        }
                   3774: }
                   3775: 
                   3776: /*-----------------------------------------------------------------------*/
                   3777: /**
                   3778:  * Write to TT color register (0xff8400)
                   3779:  */
                   3780: void Video_TTColorRegs_WriteWord(void)
                   3781: {
1.1.1.15  root     3782:        bTTColorsSync = false;
1.1.1.11  root     3783: }
                   3784: 
                   3785: /*-----------------------------------------------------------------------*/
                   3786: /**
1.1.1.15  root     3787:  * Write to ST color register on TT (0xff8240)
1.1.1.11  root     3788:  */
                   3789: void Video_TTColorSTRegs_WriteWord(void)
                   3790: {
1.1.1.15  root     3791:        bTTColorsSTSync = false;
1.1.1.8   root     3792: }
1.1.1.22! root     3793: 
        !          3794: 
        !          3795: /*-----------------------------------------------------------------------*/
        !          3796: /**
        !          3797:  * display video related information (for debugger info command)
        !          3798:  */
        !          3799: void Video_Info(Uint32 dummy)
        !          3800: {
        !          3801:        const char *mode;
        !          3802:        switch (OverscanMode) {
        !          3803:        case OVERSCANMODE_NONE:
        !          3804:                mode = "none";
        !          3805:                break;
        !          3806:        case OVERSCANMODE_TOP:
        !          3807:                mode = "top";
        !          3808:                break;
        !          3809:        case OVERSCANMODE_BOTTOM:
        !          3810:                mode = "bottom";
        !          3811:                break;
        !          3812:        case OVERSCANMODE_TOP|OVERSCANMODE_BOTTOM:
        !          3813:                mode = "top+bottom";
        !          3814:                break;
        !          3815:        default:
        !          3816:                mode = "unknown";
        !          3817:        }
        !          3818:        fprintf(stderr, "Video base   : 0x%x\n", VideoBase);
        !          3819:        fprintf(stderr, "VBL counter  : %d\n", nVBLs);
        !          3820:        fprintf(stderr, "HBL line     : %d\n", nHBL);
        !          3821:        fprintf(stderr, "V-overscan   : %s\n", mode);
        !          3822:        fprintf(stderr, "Refresh rate : %d Hz\n", nScreenRefreshRate);
        !          3823:        fprintf(stderr, "Frame skips  : %d\n", nFrameSkips);
        !          3824: 
        !          3825:        /* TODO: any other information that would be useful to show? */
        !          3826: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.