1 /*
2     Copyright (C) 1998-2004  Charles MacDonald
3 
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8 
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13 
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 */
18 
19 #include "smsshared.h"
20 #include "z80_intf.h"
21 #include "sn76496.h"
22 #include "burn_ym2413.h"
23 
24 bitmap_t bitmap;
25 cart_t cart;
26 input_t input;
27 
28 /* Run the virtual console emulation for one frame */
system_frame(INT32 skip_render)29 void system_frame(INT32 skip_render)
30 {
31     static INT32 iline_table[] = {0xC0, 0xE0, 0xF0};
32     INT32 lpf = (sms.display == DISPLAY_NTSC) ? 262 : 313;
33     INT32 iline, z80cnt = 0;;
34 	INT32 nSoundBufferPos = 0;
35 
36     /* Debounce pause key */
37     if(input.system & INPUT_PAUSE)
38     {
39         if(!sms.paused)
40         {
41             sms.paused = 1;
42 
43 			ZetNmi();
44         }
45     }
46     else
47     {
48          sms.paused = 0;
49     }
50 
51 	ZetNewFrame();
52 
53     text_counter = 0;
54 
55     /* End of frame, parse sprites for line 0 on line 261 (VCount=$FF) */
56     if(vdp.mode <= 7)
57         parse_line(0);
58 
59     for(vdp.line = 0; vdp.line < lpf;)
60     {
61         iline = iline_table[vdp.extended];
62 		z80cnt = 0;
63 
64         if(!skip_render)
65         {
66             render_line(vdp.line);
67         }
68 
69         if(vdp.line <= iline)
70         {
71             vdp.left -= 1;
72             if(vdp.left == -1)
73             {
74                 vdp.left = vdp.reg[0x0A];
75                 vdp.hint_pending = 1;
76 
77                 if(vdp.reg[0x00] & 0x10)
78 				{
79 					/*if (!(ZetTotalCycles() % CYCLES_PER_LINE)) {
80 						ZetRun(1);
81 						z80cnt++;
82 						}*/
83 					ZetRun(16);
84 					ZetSetIRQLine(0, CPU_IRQSTATUS_ACK);
85                 }
86             }
87         }
88         else
89         {
90             vdp.left = vdp.reg[0x0A];
91         }
92 
93 		ZetRun(228 - z80cnt);
94 
95         if(vdp.line == iline)
96         {
97             vdp.status |= 0x80;
98             vdp.vint_pending = 1;
99 
100             if(vdp.reg[0x01] & 0x20)
101             {
102 				ZetRun(16); // Fixes Zool, Monster Truck Wars, Chicago Syndacite, Terminator 2 (SMS)
103                 ZetSetIRQLine(0, CPU_IRQSTATUS_ACK);
104             }
105         }
106 
107 		// Render Sound Segment
108 		if (pBurnSoundOut) {
109 			INT32 nSegmentLength = nBurnSoundLen / lpf;
110 			INT16* pSoundBuf = pBurnSoundOut + (nSoundBufferPos << 1);
111 			if (sms.use_fm)	{
112 				BurnYM2413Render(pSoundBuf, nSegmentLength);
113 			} else {
114 				memset(pSoundBuf, 0, nSegmentLength * 2 * sizeof(INT16));
115 			}
116 			SN76496Update(0, pSoundBuf, nSegmentLength);
117 
118 			nSoundBufferPos += nSegmentLength;
119 		}
120 
121         ++vdp.line;
122 
123         if(vdp.mode <= 7)
124             parse_line(vdp.line);
125     }
126 
127 	// Make sure the buffer is entirely filled.
128 	if (pBurnSoundOut) {
129 		INT32 nSegmentLength = nBurnSoundLen - nSoundBufferPos;
130 		INT16* pSoundBuf = pBurnSoundOut + (nSoundBufferPos << 1);
131 		if (nSegmentLength) {
132 			if (sms.use_fm)	{
133 				BurnYM2413Render(pSoundBuf, nSegmentLength);
134 			} else {
135 				memset(pSoundBuf, 0, nSegmentLength * 2 * sizeof(INT16));
136 			}
137 			SN76496Update(0, pSoundBuf, nSegmentLength);
138 		}
139 	}
140 }
141 
142 
143 
144 
system_init(void)145 void system_init(void)
146 {
147     sms_init();
148     pio_init();
149     vdp_init();
150     render_init();
151     sound_init();
152 
153     sms.save = 0;
154 }
155 
system_shutdown(void)156 void system_shutdown(void)
157 {
158 #ifdef DEBUG
159     INT32 i;
160 
161     /*error("PC:%04X\tSP:%04X\n", z80_get_reg(Z80_PC), z80_get_reg(Z80_SP));
162     error("AF:%04X\tAF:%04X\n", z80_get_reg(Z80_AF), z80_get_reg(Z80_AF2));
163     error("BC:%04X\tBC:%04X\n", z80_get_reg(Z80_BC), z80_get_reg(Z80_BC2));
164     error("DE:%04X\tDE:%04X\n", z80_get_reg(Z80_DE), z80_get_reg(Z80_DE2));
165     error("HL:%04X\tHL:%04X\n", z80_get_reg(Z80_HL), z80_get_reg(Z80_HL2));
166     error("IX:%04X\tIY:%04X\n", z80_get_reg(Z80_IX), z80_get_reg(Z80_IY));
167 
168     for(i = 0; i <= 0x0A; i++)
169         error("%02X ", vdp.reg[i]);
170     error("\n");
171 
172     error("MODE:%02X\n", vdp.mode & 7);
173     error("PN:%04X\n", vdp.pn);
174     error("CT:%04X\n", vdp.ct);
175     error("PG:%04X\n", vdp.pg);
176     error("SA:%04X\n", vdp.sa);
177     error("SG:%04X\n", vdp.sg);
178 
179     error("\n");*/
180 #endif
181 
182     sms_shutdown();
183     pio_shutdown();
184     vdp_shutdown();
185     render_shutdown();
186     sound_shutdown();
187 }
188 
system_reset(void)189 void system_reset(void)
190 {
191     sms_reset();
192     pio_reset();
193     vdp_reset();
194     render_reset();
195     sound_reset();
196 //    system_manage_sram(cart.sram, SLOT_CART, SRAM_LOAD);
197 }
198 
system_poweron(void)199 void system_poweron(void)
200 {
201     system_reset();
202 }
203 
system_poweroff(void)204 void system_poweroff(void)
205 {
206 //    system_manage_sram(cart.sram, SLOT_CART, SRAM_SAVE);
207 }
208 
209 
210