File: ted-badline.c

package info (click to toggle)
vice 1.19-1etch1
  • links: PTS
  • area: contrib
  • in suites: etch
  • size: 27,132 kB
  • ctags: 33,406
  • sloc: ansic: 257,145; cpp: 13,395; sh: 3,674; makefile: 3,380; perl: 1,801; yacc: 622; lex: 258; asm: 4
file content (186 lines) | stat: -rw-r--r-- 6,533 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * ted-badline.c - Bad line handling for the TED emulation.
 *
 * Written by
 *  Andreas Boose <viceteam@t-online.de>
 *
 * This file is part of VICE, the Versatile Commodore Emulator.
 * See README for copyright notice.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 *  02111-1307  USA.
 *
 */

#include "vice.h"

#include <string.h>

#include "dma.h"
#include "maincpu.h"
#include "mem.h"
#include "ted-badline.h"
#include "ted-fetch.h"
#include "tedtypes.h"
#include "types.h"


inline static void line_becomes_good(int cycle)
{
    /* Bad line becomes good.  */
    ted.bad_line = 0;

    /* By changing the values in the registers, one can make the TED
       switch from idle to display state, but not from display to
       idle state.  So we are always in display state if this
       happens.  This is only true if the value changes in some
       cycle > 0, though; otherwise, the line never becomes bad.  */
    if (cycle > 0) {
        ted.raster.draw_idle_state = ted.idle_state = 0;
        ted.idle_data_location = IDLE_NONE;
        if (cycle > TED_FETCH_CYCLE + 2
            && !ted.ycounter_reset_checked) {
            ted.raster.ycounter = 0;
            ted.ycounter_reset_checked = 1;
        }
    }
}

inline static void line_becomes_bad(int cycle)
{
    if (cycle >= TED_FETCH_CYCLE
        && cycle <= TED_FETCH_CYCLE + TED_SCREEN_TEXTCOLS + 3) {
        int pos;            /* Value of line counter when this happens.  */
        int inc;            /* Total increment for the line counter.  */
        int num_chars;      /* Total number of characters to fetch.  */
        int num_0xff_fetches; /* Number of 0xff fetches to do.  */

        ted.bad_line = 1;

        if (cycle <= TED_FETCH_CYCLE + 2)
            ted.raster.ycounter = 0;

        ted.ycounter_reset_checked = 1;

        num_chars = (TED_SCREEN_TEXTCOLS
                    - (cycle - (TED_FETCH_CYCLE + 3)));

        /* Take over the bus until the memory fetch is done.  */
        dma_maincpu_steal_cycles(maincpu_clk, num_chars, 0);
        ted_delay_oldclk(num_chars);

        if (num_chars <= TED_SCREEN_TEXTCOLS) {
            /* Matrix fetches starts immediately, but the TED needs
               at least 3 cycles to become the bus master.  Before
               this happens, it fetches 0xff.  */
            num_0xff_fetches = 3;

            /* If we were in idle state before creating the bad
               line, the counters have not been incremented.  */
            if (ted.idle_state) {
                pos = 0;
                inc = num_chars;
                if (inc < 0)
                    inc = 0;
            } else {
                pos = cycle - (TED_FETCH_CYCLE + 3);
                if (pos > TED_SCREEN_TEXTCOLS - 1)
                    pos = TED_SCREEN_TEXTCOLS - 1;
                inc = TED_SCREEN_TEXTCOLS;
            }
        } else {
            pos = 0;
            num_chars = inc = TED_SCREEN_TEXTCOLS;
            num_0xff_fetches = cycle - TED_FETCH_CYCLE;
        }
        /* This is normally done at cycle `TED_FETCH_CYCLE + 2'.  */
        ted.mem_counter = ted.memptr;

        /* Force the DMA.  */
        /* Note that `ted.cbuf' is loaded from the value of
           the next opcode as the VIC-II is not the bus master yet.  */
        if (num_chars <= num_0xff_fetches) {
            memset(ted.vbuf + pos, 0xff, num_chars);
            memset(ted.cbuf + pos, mem_ram[reg_pc] & 0xf,
                   num_chars);
        } else {
            memset(ted.vbuf + pos, 0xff, num_0xff_fetches);
            memset(ted.cbuf + pos, mem_ram[reg_pc] & 0xf,
                   num_0xff_fetches);
            ted_fetch_matrix(pos + num_0xff_fetches,
                             num_chars - num_0xff_fetches);
            ted_fetch_color(pos + num_0xff_fetches,
                            num_chars - num_0xff_fetches);
        }

        /* Set the value by which `ted.mem_counter' is incremented on
           this line.  */
        ted.mem_counter_inc = inc;

        /* Remember we have done a DMA.  */
        ted.memory_fetch_done = 2;

        /* As we are on a bad line, switch to display state.  */
        ted.idle_state = 0;

        /* Try to display things correctly.  This is not exact,
           but should be OK for most cases (FIXME?).  */
        if (inc == TED_SCREEN_TEXTCOLS) {
            ted.raster.draw_idle_state = 0;
            ted.idle_data_location = IDLE_NONE;
        }
    } else if (cycle <= TED_FETCH_CYCLE + TED_SCREEN_TEXTCOLS + 6) {
        /* Bad line has been generated after fetch interval, but
           before `ted.raster.ycounter' is incremented.  */

        ted.bad_line = 1;

        /* If in idle state, counter is not incremented.  */
        if (ted.idle_state)
            ted.mem_counter_inc = 0;

        /* We are not in idle state anymore.  */
        /* This is not 100% correct, but should be OK for most cases.
           (FIXME?)  */
        ted.raster.draw_idle_state = ted.idle_state = 0;
        ted.idle_data_location = IDLE_NONE;

    } else {
        /* Line is now bad, so we must switch to display state.
           Anyway, we cannot do it here as the `ycounter' handling
           must happen in as in idle state.  */
        ted.force_display_state = 1;
    }
}

void ted_badline_check_state(BYTE value, const int cycle,
                             const unsigned int line)
{
    int was_bad_line, now_bad_line;

    /* Check whether bad line state has changed.  */
    was_bad_line = (ted.allow_bad_lines
                    && (ted.raster.ysmooth == (int)(line & 7)));
    now_bad_line = (ted.allow_bad_lines
                    && ((int)(value & 7) == (int)(line & 7)));

    if (was_bad_line && !now_bad_line) {
        line_becomes_good(cycle);
    } else {
        if (!was_bad_line && now_bad_line)
            line_becomes_bad(cycle);
    }
}