Previous 15994 Revisions Next

r12102 Tuesday 28th June, 2011 at 19:22:33 UTC by Justin Kerk
Fix svn:eol-style
[hash]plus4_cart.xml super6.xml
[src/emu/cpu/psx]mdec.c mdec.h
[src/emu/video]psx.h
[src/ldplayer]ldplayer.lst
[src/lib/cothread/doc]amd64.asm style.css targets.html usage.html x86.asm
[src/lib/cothread/test]cc.bat cc.sh test_args.cpp test_timing.cpp
[src/mess/layout]pmi80.lay sdk85.lay sdk86.lay
[src/mess/machine]pce220_ser.c pce220_ser.h psion_pack.c psion_pack.h segasms.c

hash/plus4_cart.xml
r12101r12102
1<?xml version="1.0"?>
2<!DOCTYPE softwarelist SYSTEM "softwarelist.dtd">
3<!--
4   Missing dumps:
5   
6   325201-01    C16/C116/plus4 Burn In
7   325057-01   C16/C116/plus4 DIAGNOSE
8   325062-01   C16/C116/plus4 Diagnostic Test
9-->
10<softwarelist name="plus4_cart" description="Commodore Plus/4 cartridges">
11   
12   <!-- Games -->
13   
14   <software name="atomicmi">
15      <description>Atomic Mission</description>
16      <year>1984</year>
17      <publisher>Commodore</publisher>
18      <info name="serial" value="T116005" />
19      <part name="cart" interface="plus4_cart">
20         <dataarea name="c1l" size="0x4000">
21            <rom name="317042-01.u1" size="0x4000" crc="a059fccb" sha1="e919531d016596ec1a2fd349bbe6691716297f35" offset="0" />
22         </dataarea>
23      </part>
24   </software>
25   
26   <software name="jackattk">
27      <description>Jack Attack</description>
28      <year>1984</year>
29      <publisher>Commodore</publisher>
30      <info name="serial" value="T116002" />
31      <!--
32      ARTWORK NO. 310189 REV. 1
33      |===================================|
34      |=|                                 |
35      |=|                                 |
36      |=|      ROM                        |
37      |=|                                 |
38      |=|                                 |
39      |=|                                 |
40      |=|                                 |
41      |=|                                 |
42      |===================================|
43       
44      ROM     - MOS 23128 "317040-01 (C) 1984 CBM 26011D-722"
45      -->
46      <part name="cart" interface="plus4_cart">
47         <dataarea name="c1l" size="0x4000">
48            <rom name="317040-01.u1" size="0x4000" crc="6b624fec" sha1="ba54810ef1089fdf56b74536f9d679a80ace2dc1" offset="0" />
49         </dataarea>
50      </part>
51   </software>
52   
53   <software name="piratead">
54      <description>Pirate Adventure</description>
55      <year>1984</year>
56      <publisher>Commodore</publisher>
57      <info name="serial" value="T116004" />
58      <part name="cart" interface="plus4_cart">
59         <dataarea name="c1l" size="0x4000">
60            <rom name="317041-01.u1" size="0x4000" crc="a903dc53" sha1="410ccb6b304ca30e852fdc6ec33d9dacd7de3493" offset="0" />
61         </dataarea>
62      </part>
63   </software>
64   
65   <software name="strngeod">
66      <description>Strange Odyssey</description>
67      <year>1984</year>
68      <publisher>Commodore</publisher>
69      <info name="serial" value="T116006" />
70      <!--
71      ARTWORK NO. 310189 REV. 1
72      |===================================|
73      |=|                                 |
74      |=|                                 |
75      |=|      ROM                        |
76      |=|                                 |
77      |=|                                 |
78      |=|                                 |
79      |=|                                 |
80      |=|                                 |
81      |===================================|
82       
83      ROM     - MOS 23128 "317043-01 (C) 1984 CBM 26011D-787"
84      -->
85      <part name="cart" interface="plus4_cart">
86         <dataarea name="c1l" size="0x4000">
87            <rom name="317043-01.u1" size="0x4000" crc="994c9b98" sha1="30a0707efe24f58525bffc228085f412d574fa3b" offset="0" />
88         </dataarea>
89      </part>
90   </software>
91 
92   <software name="viduzzle">
93      <description>Viduzzles</description>
94      <year>1984</year>
95      <publisher>Commodore</publisher>
96      <info name="serial" value="T116001" />
97      <!--
98      ARTWORK NO. 310189 REV. 1
99      |===================================|
100      |=|                                 |
101      |=|                                 |
102      |=|      ROM                        |
103      |=|                                 |
104      |=|                                 |
105      |=|                                 |
106      |=|                                 |
107      |=|                                 |
108      |===================================|
109       
110      ROM     - MOS 23128 "317050-02 (C) 1984 CBM"
111      -->
112      <part name="cart" interface="plus4_cart">
113         <dataarea name="c1l" size="0x4000">
114            <rom name="317050-02.u1" size="0x4000" crc="eaaa44ed" sha1="97c269ab7b57a5ddfe65a06c29986c6ccb3aeb2b" offset="0" />
115         </dataarea>
116      </part>
117   </software>
118   
119   <!-- Applications -->
120   
121   <software name="calcplus">
122      <description>Calc/Plus</description>
123      <year>1984</year>
124      <publisher>Commodore</publisher>
125      <info name="serial" value="T112003" />
126      <part name="cart" interface="plus4_cart">
127         <dataarea name="c1l" size="0x4000">
128            <rom name="calc-lo" size="0x4000" crc="8a085df6" sha1="2b8c748d7be725a1e4106b9cca167bd85786b809" offset="0" />
129         </dataarea>
130         <dataarea name="c1h" size="0x4000">
131            <rom name="calc-hi" size="0x4000" crc="772803a9" sha1="49810afc29476cc2b2750d96beabd003cffbd5a6" offset="0" />
132         </dataarea>
133      </part>
134   </software>
135   
136   <software name="c16kbtut">
137      <description>Commodore 16 Keyboard Tutorial</description>
138      <year>1984</year>
139      <publisher>Commodore</publisher>
140      <info name="serial" value="T113016" />
141      <part name="cart" interface="plus4_cart">
142         <dataarea name="c1l" size="0x4000">
143            <rom name="kb-lo" size="0x4000" crc="c999369c" sha1="fea0b7df5ccec5ce05390c650ff5f4ea878bd468" offset="0" />
144         </dataarea>
145         <dataarea name="c1h" size="0x4000">
146            <rom name="kb-hi" size="0x4000" crc="a617e188" sha1="8754ecc8b6ce2652eea4b997fb1f3ff9d3d7c9cb" offset="0" />
147         </dataarea>
148      </part>
149   </software>
150   
151   <software name="finadvis">
152      <description>Financial Advisor</description>
153      <year>1984</year>
154      <publisher>Commodore</publisher>
155      <info name="serial" value="T232001" />
156      <part name="cart" interface="plus4_cart">
157         <dataarea name="c1l" size="0x4000">
158            <rom name="317048-01.u1" size="0x4000" crc="5b0e82ae" sha1="0f1b8b20331c8c502fed507eca5d66683667757b" offset="0" />
159         </dataarea>
160      </part>
161   </software>
162   
163   <software name="logo">
164      <description>LOGO: A Language for Learning</description>
165      <year>1984</year>
166      <publisher>Commodore</publisher>
167      <info name="serial" value="T263001" />
168      <info name="video" value="NTSC" />
169      <part name="cart" interface="plus4_cart">
170         <dataarea name="c1l" size="0x4000">
171            <rom name="317046-01.u1" size="0x4000" crc="216fc5a8" sha1="6e22c082ef2659af4eb10f8acb14175941c9edc9" offset="0" />
172         </dataarea>
173         <dataarea name="c1h" size="0x4000">
174            <rom name="317047-01.u2" size="0x4000" crc="4b7e5c5a" sha1="f2d4fdafef12762c96b88f06bb5d91644f54e5c7" offset="0" />
175         </dataarea>
176      </part>
177      <part name="flop" interface="floppy_5_25">
178         <dataarea name="flop" size="174848">
179            <rom name="utility.d64" size="174848" crc="f4d84692" sha1="2850fb13b27b400167d80f2d755481835e3033b5" offset="0" />
180         </dataarea>
181      </part>
182   </software>
183   
184   <software name="basic7">
185      <description>Octasoft BASIC 7.0 (Hun)</description>
186      <year>1988</year>
187      <publisher>Novotrade</publisher>
188      <part name="cart" interface="plus4_cart">
189         <dataarea name="c2l" size="0x4000">
190            <rom name="basic7" size="0x4000" crc="e900a1d5" sha1="5f96bdeffa42c18e869d9d045df65d98a4e561eb" offset="0" />
191         </dataarea>
192      </part>
193   </software>
194   
195   <software name="scriptpl">
196      <description>Script/Plus</description>
197      <year>1984</year>
198      <publisher>Commodore</publisher>
199      <info name="serial" value="T112001" />
200      <!--
201      ARTWORK NO. 310189 REV. 1
202      |===================================|
203      |=|                                 |
204      |=|                                 |
205      |=|      ROM0                       |
206      |=|                                 |
207      |=|                                 |
208      |=|      ROM1                       |
209      |=|                                 |
210      |=|                                 |
211      |===================================|
212       
213      ROM0    - MOS 23128 "317051-01 (C) 1984 CBM 26011D-723"
214      ROM1    - MOS 23128 "317052-01 (C) 1984 CBM 26011D-724"
215      -->
216      <part name="cart" interface="plus4_cart">
217         <dataarea name="c1l" size="0x4000">
218            <rom name="317051-01.u1" size="0x4000" crc="d1679eea" sha1="d6d0d8f7feb16cdba650696683c02fcf62422f4a" offset="0" />
219         </dataarea>
220         <dataarea name="c1h" size="0x4000">
221            <rom name="317052-01.u2" size="0x4000" crc="ad0696d9" sha1="5d7c6d0f2c732c92ca8f8f98486e08137824a4a4" offset="0" />
222         </dataarea>
223      </part>
224   </software>
225   
226   <software name="speedy">
227      <description>Speedy</description>
228      <year>1988</year>
229      <publisher>&lt;unknown&gt;</publisher>
230      <part name="cart" interface="plus4_cart">
231         <dataarea name="c1l" size="0x4000">
232            <rom name="speedy.u1" size="0x2000" crc="74f180bc" sha1="79a4b19c50bc9a85c0356a3d2d7453b2d1d8266f" offset="0" />
233            <rom size="0x2000" offset="0x2000" loadflag="reload" />
234         </dataarea>
235      </part>
236   </software>
237   
238   <software name="turbopls">
239      <description>TurboPlus</description>
240      <year>1987</year>
241      <publisher>Kingsoft</publisher>
242      <info name="author" value="Andreas Arens" />
243      <part name="cart" interface="plus4_cart">
244         <dataarea name="c1l" size="0x4000">
245            <rom name="turboplus.u1" size="0x2000" crc="88b40daf" sha1="bb02e863193b9f7731e9bf5b554bb1e743a0a573" offset="0" />
246            <rom size="0x2000" offset="0x2000" loadflag="reload" />
247         </dataarea>
248      </part>
249   </software>
250 
251</softwarelist>
1<?xml version="1.0"?>
2<!DOCTYPE softwarelist SYSTEM "softwarelist.dtd">
3<!--
4   Missing dumps:
5   
6   325201-01    C16/C116/plus4 Burn In
7   325057-01   C16/C116/plus4 DIAGNOSE
8   325062-01   C16/C116/plus4 Diagnostic Test
9-->
10<softwarelist name="plus4_cart" description="Commodore Plus/4 cartridges">
11   
12   <!-- Games -->
13   
14   <software name="atomicmi">
15      <description>Atomic Mission</description>
16      <year>1984</year>
17      <publisher>Commodore</publisher>
18      <info name="serial" value="T116005" />
19      <part name="cart" interface="plus4_cart">
20         <dataarea name="c1l" size="0x4000">
21            <rom name="317042-01.u1" size="0x4000" crc="a059fccb" sha1="e919531d016596ec1a2fd349bbe6691716297f35" offset="0" />
22         </dataarea>
23      </part>
24   </software>
25   
26   <software name="jackattk">
27      <description>Jack Attack</description>
28      <year>1984</year>
29      <publisher>Commodore</publisher>
30      <info name="serial" value="T116002" />
31      <!--
32      ARTWORK NO. 310189 REV. 1
33      |===================================|
34      |=|                                 |
35      |=|                                 |
36      |=|      ROM                        |
37      |=|                                 |
38      |=|                                 |
39      |=|                                 |
40      |=|                                 |
41      |=|                                 |
42      |===================================|
43     
44      ROM     - MOS 23128 "317040-01 (C) 1984 CBM 26011D-722"
45      -->
46      <part name="cart" interface="plus4_cart">
47         <dataarea name="c1l" size="0x4000">
48            <rom name="317040-01.u1" size="0x4000" crc="6b624fec" sha1="ba54810ef1089fdf56b74536f9d679a80ace2dc1" offset="0" />
49         </dataarea>
50      </part>
51   </software>
52   
53   <software name="piratead">
54      <description>Pirate Adventure</description>
55      <year>1984</year>
56      <publisher>Commodore</publisher>
57      <info name="serial" value="T116004" />
58      <part name="cart" interface="plus4_cart">
59         <dataarea name="c1l" size="0x4000">
60            <rom name="317041-01.u1" size="0x4000" crc="a903dc53" sha1="410ccb6b304ca30e852fdc6ec33d9dacd7de3493" offset="0" />
61         </dataarea>
62      </part>
63   </software>
64   
65   <software name="strngeod">
66      <description>Strange Odyssey</description>
67      <year>1984</year>
68      <publisher>Commodore</publisher>
69      <info name="serial" value="T116006" />
70      <!--
71      ARTWORK NO. 310189 REV. 1
72      |===================================|
73      |=|                                 |
74      |=|                                 |
75      |=|      ROM                        |
76      |=|                                 |
77      |=|                                 |
78      |=|                                 |
79      |=|                                 |
80      |=|                                 |
81      |===================================|
82     
83      ROM     - MOS 23128 "317043-01 (C) 1984 CBM 26011D-787"
84      -->
85      <part name="cart" interface="plus4_cart">
86         <dataarea name="c1l" size="0x4000">
87            <rom name="317043-01.u1" size="0x4000" crc="994c9b98" sha1="30a0707efe24f58525bffc228085f412d574fa3b" offset="0" />
88         </dataarea>
89      </part>
90   </software>
91
92   <software name="viduzzle">
93      <description>Viduzzles</description>
94      <year>1984</year>
95      <publisher>Commodore</publisher>
96      <info name="serial" value="T116001" />
97      <!--
98      ARTWORK NO. 310189 REV. 1
99      |===================================|
100      |=|                                 |
101      |=|                                 |
102      |=|      ROM                        |
103      |=|                                 |
104      |=|                                 |
105      |=|                                 |
106      |=|                                 |
107      |=|                                 |
108      |===================================|
109     
110      ROM     - MOS 23128 "317050-02 (C) 1984 CBM"
111      -->
112      <part name="cart" interface="plus4_cart">
113         <dataarea name="c1l" size="0x4000">
114            <rom name="317050-02.u1" size="0x4000" crc="eaaa44ed" sha1="97c269ab7b57a5ddfe65a06c29986c6ccb3aeb2b" offset="0" />
115         </dataarea>
116      </part>
117   </software>
118   
119   <!-- Applications -->
120   
121   <software name="calcplus">
122      <description>Calc/Plus</description>
123      <year>1984</year>
124      <publisher>Commodore</publisher>
125      <info name="serial" value="T112003" />
126      <part name="cart" interface="plus4_cart">
127         <dataarea name="c1l" size="0x4000">
128            <rom name="calc-lo" size="0x4000" crc="8a085df6" sha1="2b8c748d7be725a1e4106b9cca167bd85786b809" offset="0" />
129         </dataarea>
130         <dataarea name="c1h" size="0x4000">
131            <rom name="calc-hi" size="0x4000" crc="772803a9" sha1="49810afc29476cc2b2750d96beabd003cffbd5a6" offset="0" />
132         </dataarea>
133      </part>
134   </software>
135   
136   <software name="c16kbtut">
137      <description>Commodore 16 Keyboard Tutorial</description>
138      <year>1984</year>
139      <publisher>Commodore</publisher>
140      <info name="serial" value="T113016" />
141      <part name="cart" interface="plus4_cart">
142         <dataarea name="c1l" size="0x4000">
143            <rom name="kb-lo" size="0x4000" crc="c999369c" sha1="fea0b7df5ccec5ce05390c650ff5f4ea878bd468" offset="0" />
144         </dataarea>
145         <dataarea name="c1h" size="0x4000">
146            <rom name="kb-hi" size="0x4000" crc="a617e188" sha1="8754ecc8b6ce2652eea4b997fb1f3ff9d3d7c9cb" offset="0" />
147         </dataarea>
148      </part>
149   </software>
150   
151   <software name="finadvis">
152      <description>Financial Advisor</description>
153      <year>1984</year>
154      <publisher>Commodore</publisher>
155      <info name="serial" value="T232001" />
156      <part name="cart" interface="plus4_cart">
157         <dataarea name="c1l" size="0x4000">
158            <rom name="317048-01.u1" size="0x4000" crc="5b0e82ae" sha1="0f1b8b20331c8c502fed507eca5d66683667757b" offset="0" />
159         </dataarea>
160      </part>
161   </software>
162   
163   <software name="logo">
164      <description>LOGO: A Language for Learning</description>
165      <year>1984</year>
166      <publisher>Commodore</publisher>
167      <info name="serial" value="T263001" />
168      <info name="video" value="NTSC" />
169      <part name="cart" interface="plus4_cart">
170         <dataarea name="c1l" size="0x4000">
171            <rom name="317046-01.u1" size="0x4000" crc="216fc5a8" sha1="6e22c082ef2659af4eb10f8acb14175941c9edc9" offset="0" />
172         </dataarea>
173         <dataarea name="c1h" size="0x4000">
174            <rom name="317047-01.u2" size="0x4000" crc="4b7e5c5a" sha1="f2d4fdafef12762c96b88f06bb5d91644f54e5c7" offset="0" />
175         </dataarea>
176      </part>
177      <part name="flop" interface="floppy_5_25">
178         <dataarea name="flop" size="174848">
179            <rom name="utility.d64" size="174848" crc="f4d84692" sha1="2850fb13b27b400167d80f2d755481835e3033b5" offset="0" />
180         </dataarea>
181      </part>
182   </software>
183   
184   <software name="basic7">
185      <description>Octasoft BASIC 7.0 (Hun)</description>
186      <year>1988</year>
187      <publisher>Novotrade</publisher>
188      <part name="cart" interface="plus4_cart">
189         <dataarea name="c2l" size="0x4000">
190            <rom name="basic7" size="0x4000" crc="e900a1d5" sha1="5f96bdeffa42c18e869d9d045df65d98a4e561eb" offset="0" />
191         </dataarea>
192      </part>
193   </software>
194   
195   <software name="scriptpl">
196      <description>Script/Plus</description>
197      <year>1984</year>
198      <publisher>Commodore</publisher>
199      <info name="serial" value="T112001" />
200      <!--
201      ARTWORK NO. 310189 REV. 1
202      |===================================|
203      |=|                                 |
204      |=|                                 |
205      |=|      ROM0                       |
206      |=|                                 |
207      |=|                                 |
208      |=|      ROM1                       |
209      |=|                                 |
210      |=|                                 |
211      |===================================|
212     
213      ROM0    - MOS 23128 "317051-01 (C) 1984 CBM 26011D-723"
214      ROM1    - MOS 23128 "317052-01 (C) 1984 CBM 26011D-724"
215      -->
216      <part name="cart" interface="plus4_cart">
217         <dataarea name="c1l" size="0x4000">
218            <rom name="317051-01.u1" size="0x4000" crc="d1679eea" sha1="d6d0d8f7feb16cdba650696683c02fcf62422f4a" offset="0" />
219         </dataarea>
220         <dataarea name="c1h" size="0x4000">
221            <rom name="317052-01.u2" size="0x4000" crc="ad0696d9" sha1="5d7c6d0f2c732c92ca8f8f98486e08137824a4a4" offset="0" />
222         </dataarea>
223      </part>
224   </software>
225   
226   <software name="speedy">
227      <description>Speedy</description>
228      <year>1988</year>
229      <publisher>&lt;unknown&gt;</publisher>
230      <part name="cart" interface="plus4_cart">
231         <dataarea name="c1l" size="0x4000">
232            <rom name="speedy.u1" size="0x2000" crc="74f180bc" sha1="79a4b19c50bc9a85c0356a3d2d7453b2d1d8266f" offset="0" />
233            <rom size="0x2000" offset="0x2000" loadflag="reload" />
234         </dataarea>
235      </part>
236   </software>
237   
238   <software name="turbopls">
239      <description>TurboPlus</description>
240      <year>1987</year>
241      <publisher>Kingsoft</publisher>
242      <info name="author" value="Andreas Arens" />
243      <part name="cart" interface="plus4_cart">
244         <dataarea name="c1l" size="0x4000">
245            <rom name="turboplus.u1" size="0x2000" crc="88b40daf" sha1="bb02e863193b9f7731e9bf5b554bb1e743a0a573" offset="0" />
246            <rom size="0x2000" offset="0x2000" loadflag="reload" />
247         </dataarea>
248      </part>
249   </software>
250
251</softwarelist>
hash/super6.xml
r12101r12102
1<?xml version="1.0"?>
2<!DOCTYPE softwarelist SYSTEM "softwarelist.dtd">
3<softwarelist name="super6" description="ADC Super Six diskettes">
4   
5   <software name="cpm22">
6      <description>CP/M 2.2</description>
7      <year>198?</year>
8      <publisher>Advanced Digital Corporation</publisher>
9      <info name="protection" value="none" />
10      <part name="flop1" interface="floppy_5_25">
11         <dataarea name="flop" size="175545">
12            <rom name="cpm22.imd" size="175545" crc="86aea137" sha1="61fdfaaedaf5f4945f51cfd7550efc3957860316" offset="0" />
13         </dataarea>
14      </part>
15      <part name="flop2" interface="floppy_5_25">
16         <dataarea name="flop" size="242555">
17            <rom name="cpm22r.imd" size="242555" crc="6df9024e" sha1="9bed3f5742fb712a1af53a4b9bb10f7f9a5873b4" offset="0" />
18         </dataarea>
19      </part>
20      <part name="flop3" interface="floppy_5_25">
21         <dataarea name="flop" size="206359">
22            <rom name="sbios256.imd" size="206359" crc="cef80a0c" sha1="9184c84a5d540f7d4a8a7fe4ef0b6c913e0af542" offset="0" />
23         </dataarea>
24      </part>
25      <part name="flop4" interface="floppy_5_25">
26         <dataarea name="flop" size="451102">
27            <rom name="sbios_23.imd" size="451102" crc="eb4cdd6d" sha1="64585b03ea5a154caee00de08ef09dbd02415d9c" offset="0" />
28         </dataarea>
29      </part>
30      <part name="flop5" interface="floppy_5_25">
31         <dataarea name="flop" size="527061">
32            <rom name="sbios_s6.imd" size="527061" crc="891cd9a9" sha1="cd9cfea2e1178084fd4c85fb873dedfbb4f41706" offset="0" />
33         </dataarea>
34      </part>
35   </software>
36   
37   <software name="digi56">
38      <description>digi56</description>
39      <year>198?</year>
40      <publisher>Advanced Digital Corporation</publisher>
41      <info name="protection" value="none" />
42      <part name="flop" interface="floppy_5_25">
43         <dataarea name="flop" size="472732">
44            <rom name="digi56.imd" size="472732" crc="b31a174c" sha1="7e7e99e24032d96c5823c1345c55101cf1d2ca2c" offset="0" />
45         </dataarea>
46      </part>
47   </software>
48   
49   <software name="tdos122p">
50      <description>TurboDOS v1.22p</description>
51      <year>198?</year>
52      <publisher>Advanced Digital Corporation</publisher>
53      <info name="protection" value="none" />
54      <part name="flop1" interface="floppy_5_25">
55         <feature name="part_id" value="Operating system disk"/>
56         <dataarea name="flop" size="205576">
57            <rom name="12_1.imd" size="205576" crc="50ba328b" sha1="c40427b7286f8faec207e2a403c6a02e6172636d" offset="0" />
58         </dataarea>
59      </part>
60      <part name="flop2" interface="floppy_5_25">
61         <feature name="part_id" value="Bootable disk"/>
62         <dataarea name="flop" size="197706">
63            <rom name="12_2.imd" size="197706" crc="88811af4" sha1="374b8e3b6463e19817fd55c2b57329487265fc85" offset="0" />
64         </dataarea>
65      </part>
66      <part name="flop3" interface="floppy_5_25">
67         <feature name="part_id" value="Disk drivers disk"/>
68         <dataarea name="flop" size="228565">
69            <rom name="12_3.imd" size="228565" crc="928904ca" sha1="04667695851e30b89293a15fea702c1ab714fd33" offset="0" />
70         </dataarea>
71      </part>
72      <part name="flop4" interface="floppy_5_25">
73         <feature name="part_id" value="Non-disk drivers disk"/>
74         <dataarea name="flop" size="168239">
75            <rom name="12_4.imd" size="168239" crc="917266c3" sha1="ec69d986abff8bf72289df3ad5e189a31715edb5" offset="0" />
76         </dataarea>
77      </part>
78      <part name="flop5" interface="floppy_5_25">
79         <feature name="part_id" value="GEN, PAR and DOC files"/>
80         <dataarea name="flop" size="180559">
81            <rom name="12_5.imd" size="180559" crc="dcb7a3d5" sha1="7a0935bc8d18d7345eae52b055ed2cfd4137f7b5" offset="0" />
82         </dataarea>
83      </part>
84   </software>
85   
86   <software name="tdos14">
87      <description>TurboDOS v1.4</description>
88      <year>1985</year>
89      <publisher>Advanced Digital Corporation</publisher>
90      <info name="protection" value="none" />
91      <part name="flop1" interface="floppy_5_25">
92         <feature name="part_id" value="Primary"/>
93         <dataarea name="flop" size="1033649">
94            <rom name="14_1.imd" size="1033649" crc="082d934c" sha1="eb8ded42f87acb943ca970d692bc963f259c0be1" offset="0" />
95         </dataarea>
96      </part>
97      <part name="flop2" interface="floppy_5_25">
98         <feature name="part_id" value="GEN, PAR and DOC files"/>
99         <dataarea name="flop" size="220201">
100            <rom name="14_2.imd" size="220201" crc="3beb825f" sha1="70d6cefaae69bab3ef96ca84e662fdc81b2c7e6c" offset="0" />
101         </dataarea>
102      </part>
103      <part name="flop3" interface="floppy_5_25">
104         <feature name="part_id" value="Disk drivers disk"/>
105         <dataarea name="flop" size="351139">
106            <rom name="14_3.imd" size="351139" crc="cd7f9d60" sha1="07b2d33ceb2241c8d045120a50a77d18281d9797" offset="0" />
107         </dataarea>
108      </part>
109      <part name="flop4" interface="floppy_5_25">
110         <feature name="part_id" value="DPROG"/>
111         <dataarea name="flop" size="407719">
112            <rom name="14_4.imd" size="407719" crc="61bf5d11" sha1="2307ebd1ea7783780966aefde56c361e708ba549" offset="0" />
113         </dataarea>
114      </part>
115      <part name="flop5" interface="floppy_5_25">
116         <feature name="part_id" value="Backup"/>
117         <dataarea name="flop" size="896541">
118            <rom name="14_5.imd" size="896541" crc="1e1f7818" sha1="0f3a2f2ac6fd77e6e9ccf14a990e8e4f7dc527e1" offset="0" />
119         </dataarea>
120      </part>
121      <part name="flop6" interface="floppy_5_25">
122         <feature name="part_id" value="System disk"/>
123         <dataarea name="flop" size="217116">
124            <rom name="14_6.imd" size="217116" crc="aff49cce" sha1="f4bc3ba45a522082058bdc209d61c33fcd8466d3" offset="0" />
125         </dataarea>
126      </part>
127      <part name="flop7" interface="floppy_5_25">
128         <feature name="part_id" value="Generation disk"/>
129         <dataarea name="flop" size="396137">
130            <rom name="14_7.imd" size="396137" crc="5a81c9ce" sha1="e89281786973fd778656bfc72a4a4e174ea9efcd" offset="0" />
131         </dataarea>
132      </part>
133      <part name="flop8" interface="floppy_5_25">
134         <feature name="part_id" value="Deltasoft Multi User BASIC"/>
135         <dataarea name="flop" size="152879">
136            <rom name="14_8.imd" size="152879" crc="28e55937" sha1="23e1999e5a71ee36e04e142e28414d20182f6401" offset="0" />
137         </dataarea>
138      </part>
139      <part name="flop9" interface="floppy_5_25">
140         <dataarea name="flop" size="354469">
141            <rom name="bios.imd" size="354469" crc="98188a65" sha1="5ab78191022cdfaf4aa28319770e7ab6e91eb737" offset="0" />
142         </dataarea>
143      </part>
144      <part name="flop10" interface="floppy_5_25">
145         <feature name="part_id" value="COM files, TurboSlave PAR+GEN files, boot tracks on disk"/>
146         <dataarea name="flop" size="1036746">
147            <rom name="slave.imd" size="1036746" crc="1602ab7c" sha1="5f75c227fb3b9a3838d0a60efab8c1b162638652" offset="0" />
148         </dataarea>
149      </part>
150   </software>
151   
152</softwarelist>
1<?xml version="1.0"?>
2<!DOCTYPE softwarelist SYSTEM "softwarelist.dtd">
3<softwarelist name="super6" description="ADC Super Six diskettes">
4   
5   <software name="cpm22">
6      <description>CP/M 2.2</description>
7      <year>198?</year>
8      <publisher>Advanced Digital Corporation</publisher>
9      <info name="protection" value="none" />
10      <part name="flop1" interface="floppy_5_25">
11         <dataarea name="flop" size="175545">
12            <rom name="cpm22.imd" size="175545" crc="86aea137" sha1="61fdfaaedaf5f4945f51cfd7550efc3957860316" offset="0" />
13         </dataarea>
14      </part>
15      <part name="flop2" interface="floppy_5_25">
16         <dataarea name="flop" size="242555">
17            <rom name="cpm22r.imd" size="242555" crc="6df9024e" sha1="9bed3f5742fb712a1af53a4b9bb10f7f9a5873b4" offset="0" />
18         </dataarea>
19      </part>
20      <part name="flop3" interface="floppy_5_25">
21         <dataarea name="flop" size="206359">
22            <rom name="sbios256.imd" size="206359" crc="cef80a0c" sha1="9184c84a5d540f7d4a8a7fe4ef0b6c913e0af542" offset="0" />
23         </dataarea>
24      </part>
25      <part name="flop4" interface="floppy_5_25">
26         <dataarea name="flop" size="451102">
27            <rom name="sbios_23.imd" size="451102" crc="eb4cdd6d" sha1="64585b03ea5a154caee00de08ef09dbd02415d9c" offset="0" />
28         </dataarea>
29      </part>
30      <part name="flop5" interface="floppy_5_25">
31         <dataarea name="flop" size="527061">
32            <rom name="sbios_s6.imd" size="527061" crc="891cd9a9" sha1="cd9cfea2e1178084fd4c85fb873dedfbb4f41706" offset="0" />
33         </dataarea>
34      </part>
35   </software>
36   
37   <software name="digi56">
38      <description>digi56</description>
39      <year>198?</year>
40      <publisher>Advanced Digital Corporation</publisher>
41      <info name="protection" value="none" />
42      <part name="flop" interface="floppy_5_25">
43         <dataarea name="flop" size="472732">
44            <rom name="digi56.imd" size="472732" crc="b31a174c" sha1="7e7e99e24032d96c5823c1345c55101cf1d2ca2c" offset="0" />
45         </dataarea>
46      </part>
47   </software>
48   
49   <software name="tdos122p">
50      <description>TurboDOS v1.22p</description>
51      <year>198?</year>
52      <publisher>Advanced Digital Corporation</publisher>
53      <info name="protection" value="none" />
54      <part name="flop1" interface="floppy_5_25">
55         <feature name="part_id" value="Operating system disk"/>
56         <dataarea name="flop" size="205576">
57            <rom name="12_1.imd" size="205576" crc="50ba328b" sha1="c40427b7286f8faec207e2a403c6a02e6172636d" offset="0" />
58         </dataarea>
59      </part>
60      <part name="flop2" interface="floppy_5_25">
61         <feature name="part_id" value="Bootable disk"/>
62         <dataarea name="flop" size="197706">
63            <rom name="12_2.imd" size="197706" crc="88811af4" sha1="374b8e3b6463e19817fd55c2b57329487265fc85" offset="0" />
64         </dataarea>
65      </part>
66      <part name="flop3" interface="floppy_5_25">
67         <feature name="part_id" value="Disk drivers disk"/>
68         <dataarea name="flop" size="228565">
69            <rom name="12_3.imd" size="228565" crc="928904ca" sha1="04667695851e30b89293a15fea702c1ab714fd33" offset="0" />
70         </dataarea>
71      </part>
72      <part name="flop4" interface="floppy_5_25">
73         <feature name="part_id" value="Non-disk drivers disk"/>
74         <dataarea name="flop" size="168239">
75            <rom name="12_4.imd" size="168239" crc="917266c3" sha1="ec69d986abff8bf72289df3ad5e189a31715edb5" offset="0" />
76         </dataarea>
77      </part>
78      <part name="flop5" interface="floppy_5_25">
79         <feature name="part_id" value="GEN, PAR and DOC files"/>
80         <dataarea name="flop" size="180559">
81            <rom name="12_5.imd" size="180559" crc="dcb7a3d5" sha1="7a0935bc8d18d7345eae52b055ed2cfd4137f7b5" offset="0" />
82         </dataarea>
83      </part>
84   </software>
85   
86   <software name="tdos14">
87      <description>TurboDOS v1.4</description>
88      <year>1985</year>
89      <publisher>Advanced Digital Corporation</publisher>
90      <info name="protection" value="none" />
91      <part name="flop1" interface="floppy_5_25">
92         <feature name="part_id" value="Primary"/>
93         <dataarea name="flop" size="1033649">
94            <rom name="14_1.imd" size="1033649" crc="082d934c" sha1="eb8ded42f87acb943ca970d692bc963f259c0be1" offset="0" />
95         </dataarea>
96      </part>
97      <part name="flop2" interface="floppy_5_25">
98         <feature name="part_id" value="GEN, PAR and DOC files"/>
99         <dataarea name="flop" size="220201">
100            <rom name="14_2.imd" size="220201" crc="3beb825f" sha1="70d6cefaae69bab3ef96ca84e662fdc81b2c7e6c" offset="0" />
101         </dataarea>
102      </part>
103      <part name="flop3" interface="floppy_5_25">
104         <feature name="part_id" value="Disk drivers disk"/>
105         <dataarea name="flop" size="351139">
106            <rom name="14_3.imd" size="351139" crc="cd7f9d60" sha1="07b2d33ceb2241c8d045120a50a77d18281d9797" offset="0" />
107         </dataarea>
108      </part>
109      <part name="flop4" interface="floppy_5_25">
110         <feature name="part_id" value="DPROG"/>
111         <dataarea name="flop" size="407719">
112            <rom name="14_4.imd" size="407719" crc="61bf5d11" sha1="2307ebd1ea7783780966aefde56c361e708ba549" offset="0" />
113         </dataarea>
114      </part>
115      <part name="flop5" interface="floppy_5_25">
116         <feature name="part_id" value="Backup"/>
117         <dataarea name="flop" size="896541">
118            <rom name="14_5.imd" size="896541" crc="1e1f7818" sha1="0f3a2f2ac6fd77e6e9ccf14a990e8e4f7dc527e1" offset="0" />
119         </dataarea>
120      </part>
121      <part name="flop6" interface="floppy_5_25">
122         <feature name="part_id" value="System disk"/>
123         <dataarea name="flop" size="217116">
124            <rom name="14_6.imd" size="217116" crc="aff49cce" sha1="f4bc3ba45a522082058bdc209d61c33fcd8466d3" offset="0" />
125         </dataarea>
126      </part>
127      <part name="flop7" interface="floppy_5_25">
128         <feature name="part_id" value="Generation disk"/>
129         <dataarea name="flop" size="396137">
130            <rom name="14_7.imd" size="396137" crc="5a81c9ce" sha1="e89281786973fd778656bfc72a4a4e174ea9efcd" offset="0" />
131         </dataarea>
132      </part>
133      <part name="flop8" interface="floppy_5_25">
134         <feature name="part_id" value="Deltasoft Multi User BASIC"/>
135         <dataarea name="flop" size="152879">
136            <rom name="14_8.imd" size="152879" crc="28e55937" sha1="23e1999e5a71ee36e04e142e28414d20182f6401" offset="0" />
137         </dataarea>
138      </part>
139      <part name="flop9" interface="floppy_5_25">
140         <dataarea name="flop" size="354469">
141            <rom name="bios.imd" size="354469" crc="98188a65" sha1="5ab78191022cdfaf4aa28319770e7ab6e91eb737" offset="0" />
142         </dataarea>
143      </part>
144      <part name="flop10" interface="floppy_5_25">
145         <feature name="part_id" value="COM files, TurboSlave PAR+GEN files, boot tracks on disk"/>
146         <dataarea name="flop" size="1036746">
147            <rom name="slave.imd" size="1036746" crc="1602ab7c" sha1="5f75c227fb3b9a3838d0a60efab8c1b162638652" offset="0" />
148         </dataarea>
149      </part>
150   </software>
151   
152</softwarelist>
src/emu/cpu/psx/mdec.c
r12101r12102
1/*
2 * PlayStation Motion Decoder emulator
3 *
4 * Copyright 2003-2011 smf
5 *
6 * Thanks to Oliver Galibert for help figuring out IDCT
7 *
8 */
9 
10#include "emu.h"
11#include "dma.h"
12#include "mdec.h"
13#include "includes/psx.h"
14 
15#define VERBOSE_LEVEL ( 0 )
16 
17INLINE void ATTR_PRINTF(3,4) verboselog( running_machine& machine, int n_level, const char *s_fmt, ... )
18{
19   if( VERBOSE_LEVEL >= n_level )
20   {
21      va_list v;
22      char buf[ 32768 ];
23      va_start( v, s_fmt );
24      vsprintf( buf, s_fmt, v );
25      va_end( v );
26      logerror( "%s: %s", machine.describe_context(), buf );
27   }
28}
29 
30const device_type PSX_MDEC = &device_creator<psxmdec_device>;
31 
32psxmdec_device::psxmdec_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
33   : device_t(mconfig, PSX_MDEC, "PSX MDEC", tag, owner, clock)
34{
35}
36 
37void psxmdec_device::device_reset()
38{
39   n_0_command = 0;
40   n_0_address = 0;
41   n_0_size = 0;
42   n_1_command = 0;
43   n_1_status = 0;
44   n_offset = 0;
45   n_decoded = 0;
46}
47 
48void psxmdec_device::device_post_load()
49{
50   mdec_cos_precalc();
51}
52 
53void psxmdec_device::device_start()
54{
55   for( int n = 0; n < 256; n++ )
56   {
57      p_n_clamp8[ n ] = 0;
58      p_n_clamp8[ n + 256 ] = n;
59      p_n_clamp8[ n + 512 ] = 255;
60 
61      p_n_r5[ n ] = 0;
62      p_n_r5[ n + 256 ] = ( n >> 3 );
63      p_n_r5[ n + 512 ] = ( 255 >> 3 );
64 
65      p_n_g5[ n ] = 0;
66      p_n_g5[ n + 256 ] = ( n >> 3 ) << 5;
67      p_n_g5[ n + 512 ] = ( 255 >> 3 ) << 5;
68 
69      p_n_b5[ n ] = 0;
70      p_n_b5[ n + 256 ] = ( n >> 3 ) << 10;
71      p_n_b5[ n + 512 ] = ( 255 >> 3 ) << 10;
72   }
73 
74   save_item( NAME( n_0_command ) );
75   save_item( NAME( n_0_address ) );
76   save_item( NAME( n_0_size ) );
77   save_item( NAME( n_1_command ) );
78   save_item( NAME( n_1_status ) );
79   save_item( NAME( p_n_quantize_y ) );
80   save_item( NAME( p_n_quantize_uv ) );
81   save_item( NAME( p_n_cos ) );
82}
83 
84#ifdef UNUSED_FUNCTION
85INLINE void psxwriteword( UINT32 *p_n_psxram, UINT32 n_address, UINT16 n_data )
86{
87   *( (UINT16 *)( (UINT8 *)p_n_psxram + WORD_XOR_LE( n_address ) ) ) = n_data;
88}
89#endif
90 
91INLINE UINT16 psxreadword( UINT32 *p_n_psxram, UINT32 n_address )
92{
93   return *( (UINT16 *)( (UINT8 *)p_n_psxram + WORD_XOR_LE( n_address ) ) );
94}
95 
96static const UINT32 m_p_n_mdec_zigzag[ DCTSIZE2 ] =
97{
98    0,  1,  8, 16,  9,  2,  3, 10,
99   17, 24, 32, 25, 18, 11,  4,  5,
100   12, 19, 26, 33, 40, 48, 41, 34,
101   27, 20, 13,  6,  7, 14, 21, 28,
102   35, 42, 49, 56, 57, 50, 43, 36,
103   29, 22, 15, 23, 30, 37, 44, 51,
104   58, 59, 52, 45, 38, 31, 39, 46,
105   53, 60, 61, 54, 47, 55, 62, 63
106};
107 
108void psxmdec_device::mdec_cos_precalc()
109{
110   UINT32 n_x;
111   UINT32 n_y;
112   UINT32 n_u;
113   UINT32 n_v;
114   INT32 *p_n_precalc = p_n_cos_precalc;
115 
116   for( n_y = 0; n_y < 8; n_y++ )
117   {
118      for( n_x = 0; n_x < 8; n_x++ )
119      {
120         for( n_v = 0; n_v < 8; n_v++ )
121         {
122            for( n_u = 0; n_u < 8; n_u++ )
123            {
124               *( p_n_precalc++ ) =
125                  ( ( p_n_cos[ ( n_u * 8 ) + n_x ] *
126                  p_n_cos[ ( n_v * 8 ) + n_y ] ) >> ( 30 - MDEC_COS_PRECALC_BITS ) );
127            }
128         }
129      }
130   }
131}
132 
133void psxmdec_device::mdec_idct( INT32 *p_n_src, INT32 *p_n_dst )
134{
135   INT32 *p_n_precalc = p_n_cos_precalc;
136 
137   for( UINT32 n_yx = 0; n_yx < DCTSIZE2; n_yx++ )
138   {
139      INT32 p_n_z[ 8 ];
140      INT32 *p_n_data = p_n_src;
141 
142      memset( p_n_z, 0, sizeof( p_n_z ) );
143 
144      for( UINT32 n_vu = 0; n_vu < DCTSIZE2 / 8; n_vu++ )
145      {
146         p_n_z[ 0 ] += p_n_data[ 0 ] * p_n_precalc[ 0 ];
147         p_n_z[ 1 ] += p_n_data[ 1 ] * p_n_precalc[ 1 ];
148         p_n_z[ 2 ] += p_n_data[ 2 ] * p_n_precalc[ 2 ];
149         p_n_z[ 3 ] += p_n_data[ 3 ] * p_n_precalc[ 3 ];
150         p_n_z[ 4 ] += p_n_data[ 4 ] * p_n_precalc[ 4 ];
151         p_n_z[ 5 ] += p_n_data[ 5 ] * p_n_precalc[ 5 ];
152         p_n_z[ 6 ] += p_n_data[ 6 ] * p_n_precalc[ 6 ];
153         p_n_z[ 7 ] += p_n_data[ 7 ] * p_n_precalc[ 7 ];
154         p_n_data += 8;
155         p_n_precalc += 8;
156      }
157 
158      *( p_n_dst++ ) = ( p_n_z[ 0 ] + p_n_z[ 1 ] + p_n_z[ 2 ] + p_n_z[ 3 ] +
159         p_n_z[ 4 ] + p_n_z[ 5 ] + p_n_z[ 6 ] + p_n_z[ 7 ] ) >> ( MDEC_COS_PRECALC_BITS + 2 );
160   }
161}
162 
163INLINE UINT16 mdec_unpack_run( UINT16 n_packed )
164{
165   return n_packed >> 10;
166}
167 
168INLINE INT32 mdec_unpack_val( UINT16 n_packed )
169{
170   return ( ( (INT32)n_packed ) << 22 ) >> 22;
171}
172 
173UINT32 psxmdec_device::mdec_unpack( UINT32 n_address )
174{
175   psx_state *p_psx = machine().driver_data<psx_state>();
176   UINT32 *p_n_psxram = p_psx->m_p_n_psxram;
177 
178   UINT8 n_z;
179   INT32 n_qscale;
180   UINT16 n_packed;
181   INT32 *p_n_block;
182   INT32 p_n_unpacked[ 64 ];
183   INT32 *p_n_q;
184 
185   p_n_q = p_n_quantize_uv;
186   p_n_block = m_p_n_unpacked;
187 
188   for( UINT32 n_block = 0; n_block < 6; n_block++ )
189   {
190      memset( p_n_unpacked, 0, sizeof( p_n_unpacked ) );
191 
192      if( n_block == 2 )
193      {
194         p_n_q = p_n_quantize_y;
195      }
196      n_packed = psxreadword( p_n_psxram, n_address );
197      n_address += 2;
198      if( n_packed == 0xfe00 )
199      {
200         break;
201      }
202 
203      n_qscale = mdec_unpack_run( n_packed );
204      p_n_unpacked[ 0 ] = mdec_unpack_val( n_packed ) * p_n_q[ 0 ];
205 
206      n_z = 0;
207      for( ;; )
208      {
209         n_packed = psxreadword( p_n_psxram, n_address );
210         n_address += 2;
211 
212         if( n_packed == 0xfe00 )
213         {
214            break;
215         }
216         n_z += mdec_unpack_run( n_packed ) + 1;
217         if( n_z > 63 )
218         {
219            break;
220         }
221         p_n_unpacked[ m_p_n_mdec_zigzag[ n_z ] ] = ( mdec_unpack_val( n_packed ) * p_n_q[ n_z ] * n_qscale ) / 8;
222      }
223      mdec_idct( p_n_unpacked, p_n_block );
224      p_n_block += DCTSIZE2;
225   }
226   return n_address;
227}
228 
229INLINE INT32 mdec_cr_to_r( INT32 n_cr )
230{
231   return ( 1435 * n_cr ) >> 10;
232}
233 
234INLINE INT32 mdec_cr_to_g( INT32 n_cr )
235{
236   return ( -731 * n_cr ) >> 10;
237}
238 
239INLINE INT32 mdec_cb_to_g( INT32 n_cb )
240{
241   return ( -351 * n_cb ) >> 10;
242}
243 
244INLINE INT32 mdec_cb_to_b( INT32 n_cb )
245{
246   return ( 1814 * n_cb ) >> 10;
247}
248 
249UINT16 psxmdec_device::mdec_clamp_r5( INT32 n_r ) const
250{
251   return p_n_r5[ n_r + 128 + 256 ];
252}
253 
254UINT16 psxmdec_device::mdec_clamp_g5( INT32 n_g ) const
255{
256   return p_n_g5[ n_g + 128 + 256 ];
257}
258 
259UINT16 psxmdec_device::mdec_clamp_b5( INT32 n_b ) const
260{
261   return p_n_b5[ n_b + 128 + 256 ];
262}
263 
264void psxmdec_device::mdec_makergb15( UINT32 n_address, INT32 n_r, INT32 n_g, INT32 n_b, INT32 *p_n_y, UINT16 n_stp )
265{
266   p_n_output[ WORD_XOR_LE( n_address + 0 ) / 2 ] = n_stp |
267      mdec_clamp_r5( p_n_y[ 0 ] + n_r ) |
268      mdec_clamp_g5( p_n_y[ 0 ] + n_g ) |
269      mdec_clamp_b5( p_n_y[ 0 ] + n_b );
270 
271   p_n_output[ WORD_XOR_LE( n_address + 2 ) / 2 ] = n_stp |
272      mdec_clamp_r5( p_n_y[ 1 ] + n_r ) |
273      mdec_clamp_g5( p_n_y[ 1 ] + n_g ) |
274      mdec_clamp_b5( p_n_y[ 1 ] + n_b );
275}
276 
277void psxmdec_device::mdec_yuv2_to_rgb15( void )
278{
279   INT32 n_r;
280   INT32 n_g;
281   INT32 n_b;
282   INT32 n_cb;
283   INT32 n_cr;
284   INT32 *p_n_cb;
285   INT32 *p_n_cr;
286   INT32 *p_n_y;
287   UINT32 n_x;
288   UINT32 n_y;
289   UINT32 n_z;
290   UINT16 n_stp;
291   int n_address = 0;
292 
293   if( ( n_0_command & ( 1L << 25 ) ) != 0 )
294   {
295      n_stp = 0x8000;
296   }
297   else
298   {
299      n_stp = 0x0000;
300   }
301 
302   p_n_cr = &m_p_n_unpacked[ 0 ];
303   p_n_cb = &m_p_n_unpacked[ DCTSIZE2 ];
304   p_n_y = &m_p_n_unpacked[ DCTSIZE2 * 2 ];
305 
306   for( n_z = 0; n_z < 2; n_z++ )
307   {
308      for( n_y = 0; n_y < 4; n_y++ )
309      {
310         for( n_x = 0; n_x < 4; n_x++ )
311         {
312            n_cr = *( p_n_cr );
313            n_cb = *( p_n_cb );
314            n_r = mdec_cr_to_r( n_cr );
315            n_g = mdec_cr_to_g( n_cr ) + mdec_cb_to_g( n_cb );
316            n_b = mdec_cb_to_b( n_cb );
317 
318            mdec_makergb15( ( n_address +  0 ), n_r, n_g, n_b, p_n_y, n_stp );
319            mdec_makergb15( ( n_address + 32 ), n_r, n_g, n_b, p_n_y + 8, n_stp );
320 
321            n_cr = *( p_n_cr + 4 );
322            n_cb = *( p_n_cb + 4 );
323            n_r = mdec_cr_to_r( n_cr );
324            n_g = mdec_cr_to_g( n_cr ) + mdec_cb_to_g( n_cb );
325            n_b = mdec_cb_to_b( n_cb );
326 
327            mdec_makergb15( ( n_address + 16 ), n_r, n_g, n_b, p_n_y + DCTSIZE2, n_stp );
328            mdec_makergb15( ( n_address + 48 ), n_r, n_g, n_b, p_n_y + DCTSIZE2 + 8, n_stp );
329 
330            p_n_cr++;
331            p_n_cb++;
332            p_n_y += 2;
333            n_address += 4;
334         }
335         p_n_cr += 4;
336         p_n_cb += 4;
337         p_n_y += 8;
338         n_address += 48;
339      }
340      p_n_y += DCTSIZE2;
341   }
342   n_decoded = ( 16 * 16 ) / 2;
343}
344 
345UINT16 psxmdec_device::mdec_clamp8( INT32 n_r ) const
346{
347   return p_n_clamp8[ n_r + 128 + 256 ];
348}
349 
350void psxmdec_device::mdec_makergb24( UINT32 n_address, INT32 n_r, INT32 n_g, INT32 n_b, INT32 *p_n_y, UINT32 n_stp )
351{
352   p_n_output[ WORD_XOR_LE( n_address + 0 ) / 2 ] = ( mdec_clamp8( p_n_y[ 0 ] + n_g ) << 8 ) | mdec_clamp8( p_n_y[ 0 ] + n_r );
353   p_n_output[ WORD_XOR_LE( n_address + 2 ) / 2 ] = ( mdec_clamp8( p_n_y[ 1 ] + n_r ) << 8 ) | mdec_clamp8( p_n_y[ 0 ] + n_b );
354   p_n_output[ WORD_XOR_LE( n_address + 4 ) / 2 ] = ( mdec_clamp8( p_n_y[ 1 ] + n_b ) << 8 ) | mdec_clamp8( p_n_y[ 1 ] + n_g );
355}
356 
357void psxmdec_device::mdec_yuv2_to_rgb24( void )
358{
359   INT32 n_r;
360   INT32 n_g;
361   INT32 n_b;
362   INT32 n_cb;
363   INT32 n_cr;
364   INT32 *p_n_cb;
365   INT32 *p_n_cr;
366   INT32 *p_n_y;
367   UINT32 n_x;
368   UINT32 n_y;
369   UINT32 n_z;
370   UINT32 n_stp;
371   int n_address = 0;
372 
373   if( ( n_0_command & ( 1L << 25 ) ) != 0 )
374   {
375      n_stp = 0x80008000;
376   }
377   else
378   {
379      n_stp = 0x00000000;
380   }
381 
382   p_n_cr = &m_p_n_unpacked[ 0 ];
383   p_n_cb = &m_p_n_unpacked[ DCTSIZE2 ];
384   p_n_y = &m_p_n_unpacked[ DCTSIZE2 * 2 ];
385 
386   for( n_z = 0; n_z < 2; n_z++ )
387   {
388      for( n_y = 0; n_y < 4; n_y++ )
389      {
390         for( n_x = 0; n_x < 4; n_x++ )
391         {
392            n_cr = *( p_n_cr );
393            n_cb = *( p_n_cb );
394            n_r = mdec_cr_to_r( n_cr );
395            n_g = mdec_cr_to_g( n_cr ) + mdec_cb_to_g( n_cb );
396            n_b = mdec_cb_to_b( n_cb );
397 
398            mdec_makergb24( ( n_address +  0 ), n_r, n_g, n_b, p_n_y, n_stp );
399            mdec_makergb24( ( n_address + 48 ), n_r, n_g, n_b, p_n_y + 8, n_stp );
400 
401            n_cr = *( p_n_cr + 4 );
402            n_cb = *( p_n_cb + 4 );
403            n_r = mdec_cr_to_r( n_cr );
404            n_g = mdec_cr_to_g( n_cr ) + mdec_cb_to_g( n_cb );
405            n_b = mdec_cb_to_b( n_cb );
406 
407            mdec_makergb24( ( n_address + 24 ), n_r, n_g, n_b, p_n_y + DCTSIZE2, n_stp );
408            mdec_makergb24( ( n_address + 72 ), n_r, n_g, n_b, p_n_y + DCTSIZE2 + 8, n_stp );
409 
410            p_n_cr++;
411            p_n_cb++;
412            p_n_y += 2;
413            n_address += 6;
414         }
415         p_n_cr += 4;
416         p_n_cb += 4;
417         p_n_y += 8;
418         n_address += 72;
419      }
420      p_n_y += DCTSIZE2;
421   }
422   n_decoded = ( 24 * 16 ) / 2;
423}
424 
425void psxmdec_device::dma_write( UINT32 n_address, INT32 n_size )
426{
427   psx_state *p_psx = machine().driver_data<psx_state>();
428   UINT32 *p_n_psxram = p_psx->m_p_n_psxram;
429   int n_index;
430 
431   verboselog( machine(), 2, "mdec0_write( %08x, %08x )\n", n_address, n_size );
432 
433   switch( n_0_command >> 28 )
434   {
435   case 0x3:
436      verboselog( machine(), 1, "mdec decode %08x %08x %08x\n", n_0_command, n_address, n_size );
437      n_0_address = n_address;
438      n_0_size = n_size * 4;
439      n_1_status |= ( 1L << 29 );
440      break;
441   case 0x4:
442      verboselog( machine(), 1, "mdec quantize table %08x %08x %08x\n", n_0_command, n_address, n_size );
443      n_index = 0;
444      while( n_size > 0 )
445      {
446         if( n_index < DCTSIZE2 )
447         {
448            p_n_quantize_y[ n_index + 0 ] = ( p_n_psxram[ n_address / 4 ] >> 0 ) & 0xff;
449            p_n_quantize_y[ n_index + 1 ] = ( p_n_psxram[ n_address / 4 ] >> 8 ) & 0xff;
450            p_n_quantize_y[ n_index + 2 ] = ( p_n_psxram[ n_address / 4 ] >> 16 ) & 0xff;
451            p_n_quantize_y[ n_index + 3 ] = ( p_n_psxram[ n_address / 4 ] >> 24 ) & 0xff;
452         }
453         else if( n_index < DCTSIZE2 * 2 )
454         {
455            p_n_quantize_uv[ n_index + 0 - DCTSIZE2 ] = ( p_n_psxram[ n_address / 4 ] >> 0 ) & 0xff;
456            p_n_quantize_uv[ n_index + 1 - DCTSIZE2 ] = ( p_n_psxram[ n_address / 4 ] >> 8 ) & 0xff;
457            p_n_quantize_uv[ n_index + 2 - DCTSIZE2 ] = ( p_n_psxram[ n_address / 4 ] >> 16 ) & 0xff;
458            p_n_quantize_uv[ n_index + 3 - DCTSIZE2 ] = ( p_n_psxram[ n_address / 4 ] >> 24 ) & 0xff;
459         }
460         n_index += 4;
461         n_address += 4;
462         n_size--;
463      }
464      break;
465   case 0x6:
466      verboselog( machine(), 1, "mdec cosine table %08x %08x %08x\n", n_0_command, n_address, n_size );
467      n_index = 0;
468      while( n_size > 0 )
469      {
470         p_n_cos[ n_index + 0 ] = (INT16)( ( p_n_psxram[ n_address / 4 ] >> 0 ) & 0xffff );
471         p_n_cos[ n_index + 1 ] = (INT16)( ( p_n_psxram[ n_address / 4 ] >> 16 ) & 0xffff );
472         n_index += 2;
473         n_address += 4;
474         n_size--;
475      }
476      mdec_cos_precalc();
477      break;
478   default:
479      verboselog( machine(), 0, "mdec unknown command %08x %08x %08x\n", n_0_command, n_address, n_size );
480      break;
481   }
482}
483 
484void psxmdec_device::dma_read( UINT32 n_address, INT32 n_size )
485{
486   psx_state *p_psx = machine().driver_data<psx_state>();
487   UINT32 *p_n_psxram = p_psx->m_p_n_psxram;
488   UINT32 n_this;
489   UINT32 n_nextaddress;
490 
491   verboselog( machine(), 2, "mdec1_read( %08x, %08x )\n", n_address, n_size );
492   if( ( n_0_command & ( 1L << 29 ) ) != 0 && n_0_size != 0 )
493   {
494      while( n_size > 0 )
495      {
496         if( n_decoded == 0 )
497         {
498            if( (int)n_0_size <= 0 )
499            {
500               mame_printf_debug( "ran out of data %08x\n", n_size );
501               n_0_size = 0;
502               break;
503            }
504 
505            n_nextaddress = mdec_unpack( n_0_address );
506            n_0_size -= n_nextaddress - n_0_address;
507            n_0_address = n_nextaddress;
508 
509            if( ( n_0_command & ( 1L << 27 ) ) != 0 )
510            {
511               mdec_yuv2_to_rgb15();
512            }
513            else
514            {
515               mdec_yuv2_to_rgb24();
516            }
517            n_offset = 0;
518         }
519 
520         n_this = n_decoded;
521         if( n_this > n_size )
522         {
523            n_this = n_size;
524         }
525         n_decoded -= n_this;
526 
527         memcpy( (UINT8 *)p_n_psxram + n_address, (UINT8 *)p_n_output + n_offset, n_this * 4 );
528         n_offset += n_this * 4;
529         n_address += n_this * 4;
530         n_size -= n_this;
531      }
532 
533      if( (int)n_0_size < 0 )
534      {
535         mame_printf_debug( "ran out of data %d\n", n_0_size );
536      }
537   }
538   else
539   {
540      mame_printf_debug( "mdec1_read no conversion :%08x:%08x:\n", n_0_command, n_0_size );
541   }
542   n_1_status &= ~( 1L << 29 );
543}
544 
545WRITE32_MEMBER( psxmdec_device::write )
546{
547   switch( offset )
548   {
549   case 0:
550      verboselog( machine(), 2, "mdec 0 command %08x\n", data );
551      n_0_command = data;
552      break;
553   case 1:
554      verboselog( machine(), 2, "mdec 1 command %08x\n", data );
555      n_1_command = data;
556      break;
557   }
558}
559 
560READ32_MEMBER( psxmdec_device::read )
561{
562   switch( offset )
563   {
564   case 0:
565      verboselog( machine(), 2, "mdec 0 status %08x\n", 0 );
566      return 0;
567   case 1:
568      verboselog( machine(), 2, "mdec 1 status %08x\n", n_1_status );
569      return n_1_status;
570   }
571   return 0;
572}
1/*
2 * PlayStation Motion Decoder emulator
3 *
4 * Copyright 2003-2011 smf
5 *
6 * Thanks to Oliver Galibert for help figuring out IDCT
7 *
8 */
9
10#include "emu.h"
11#include "dma.h"
12#include "mdec.h"
13#include "includes/psx.h"
14
15#define VERBOSE_LEVEL ( 0 )
16
17INLINE void ATTR_PRINTF(3,4) verboselog( running_machine& machine, int n_level, const char *s_fmt, ... )
18{
19   if( VERBOSE_LEVEL >= n_level )
20   {
21      va_list v;
22      char buf[ 32768 ];
23      va_start( v, s_fmt );
24      vsprintf( buf, s_fmt, v );
25      va_end( v );
26      logerror( "%s: %s", machine.describe_context(), buf );
27   }
28}
29
30const device_type PSX_MDEC = &device_creator<psxmdec_device>;
31
32psxmdec_device::psxmdec_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
33   : device_t(mconfig, PSX_MDEC, "PSX MDEC", tag, owner, clock)
34{
35}
36
37void psxmdec_device::device_reset()
38{
39   n_0_command = 0;
40   n_0_address = 0;
41   n_0_size = 0;
42   n_1_command = 0;
43   n_1_status = 0;
44   n_offset = 0;
45   n_decoded = 0;
46}
47
48void psxmdec_device::device_post_load()
49{
50   mdec_cos_precalc();
51}
52
53void psxmdec_device::device_start()
54{
55   for( int n = 0; n < 256; n++ )
56   {
57      p_n_clamp8[ n ] = 0;
58      p_n_clamp8[ n + 256 ] = n;
59      p_n_clamp8[ n + 512 ] = 255;
60
61      p_n_r5[ n ] = 0;
62      p_n_r5[ n + 256 ] = ( n >> 3 );
63      p_n_r5[ n + 512 ] = ( 255 >> 3 );
64
65      p_n_g5[ n ] = 0;
66      p_n_g5[ n + 256 ] = ( n >> 3 ) << 5;
67      p_n_g5[ n + 512 ] = ( 255 >> 3 ) << 5;
68
69      p_n_b5[ n ] = 0;
70      p_n_b5[ n + 256 ] = ( n >> 3 ) << 10;
71      p_n_b5[ n + 512 ] = ( 255 >> 3 ) << 10;
72   }
73
74   save_item( NAME( n_0_command ) );
75   save_item( NAME( n_0_address ) );
76   save_item( NAME( n_0_size ) );
77   save_item( NAME( n_1_command ) );
78   save_item( NAME( n_1_status ) );
79   save_item( NAME( p_n_quantize_y ) );
80   save_item( NAME( p_n_quantize_uv ) );
81   save_item( NAME( p_n_cos ) );
82}
83
84#ifdef UNUSED_FUNCTION
85INLINE void psxwriteword( UINT32 *p_n_psxram, UINT32 n_address, UINT16 n_data )
86{
87   *( (UINT16 *)( (UINT8 *)p_n_psxram + WORD_XOR_LE( n_address ) ) ) = n_data;
88}
89#endif
90
91INLINE UINT16 psxreadword( UINT32 *p_n_psxram, UINT32 n_address )
92{
93   return *( (UINT16 *)( (UINT8 *)p_n_psxram + WORD_XOR_LE( n_address ) ) );
94}
95
96static const UINT32 m_p_n_mdec_zigzag[ DCTSIZE2 ] =
97{
98    0,  1,  8, 16,  9,  2,  3, 10,
99   17, 24, 32, 25, 18, 11,  4,  5,
100   12, 19, 26, 33, 40, 48, 41, 34,
101   27, 20, 13,  6,  7, 14, 21, 28,
102   35, 42, 49, 56, 57, 50, 43, 36,
103   29, 22, 15, 23, 30, 37, 44, 51,
104   58, 59, 52, 45, 38, 31, 39, 46,
105   53, 60, 61, 54, 47, 55, 62, 63
106};
107
108void psxmdec_device::mdec_cos_precalc()
109{
110   UINT32 n_x;
111   UINT32 n_y;
112   UINT32 n_u;
113   UINT32 n_v;
114   INT32 *p_n_precalc = p_n_cos_precalc;
115
116   for( n_y = 0; n_y < 8; n_y++ )
117   {
118      for( n_x = 0; n_x < 8; n_x++ )
119      {
120         for( n_v = 0; n_v < 8; n_v++ )
121         {
122            for( n_u = 0; n_u < 8; n_u++ )
123            {
124               *( p_n_precalc++ ) =
125                  ( ( p_n_cos[ ( n_u * 8 ) + n_x ] *
126                  p_n_cos[ ( n_v * 8 ) + n_y ] ) >> ( 30 - MDEC_COS_PRECALC_BITS ) );
127            }
128         }
129      }
130   }
131}
132
133void psxmdec_device::mdec_idct( INT32 *p_n_src, INT32 *p_n_dst )
134{
135   INT32 *p_n_precalc = p_n_cos_precalc;
136
137   for( UINT32 n_yx = 0; n_yx < DCTSIZE2; n_yx++ )
138   {
139      INT32 p_n_z[ 8 ];
140      INT32 *p_n_data = p_n_src;
141
142      memset( p_n_z, 0, sizeof( p_n_z ) );
143
144      for( UINT32 n_vu = 0; n_vu < DCTSIZE2 / 8; n_vu++ )
145      {
146         p_n_z[ 0 ] += p_n_data[ 0 ] * p_n_precalc[ 0 ];
147         p_n_z[ 1 ] += p_n_data[ 1 ] * p_n_precalc[ 1 ];
148         p_n_z[ 2 ] += p_n_data[ 2 ] * p_n_precalc[ 2 ];
149         p_n_z[ 3 ] += p_n_data[ 3 ] * p_n_precalc[ 3 ];
150         p_n_z[ 4 ] += p_n_data[ 4 ] * p_n_precalc[ 4 ];
151         p_n_z[ 5 ] += p_n_data[ 5 ] * p_n_precalc[ 5 ];
152         p_n_z[ 6 ] += p_n_data[ 6 ] * p_n_precalc[ 6 ];
153         p_n_z[ 7 ] += p_n_data[ 7 ] * p_n_precalc[ 7 ];
154         p_n_data += 8;
155         p_n_precalc += 8;
156      }
157
158      *( p_n_dst++ ) = ( p_n_z[ 0 ] + p_n_z[ 1 ] + p_n_z[ 2 ] + p_n_z[ 3 ] +
159         p_n_z[ 4 ] + p_n_z[ 5 ] + p_n_z[ 6 ] + p_n_z[ 7 ] ) >> ( MDEC_COS_PRECALC_BITS + 2 );
160   }
161}
162
163INLINE UINT16 mdec_unpack_run( UINT16 n_packed )
164{
165   return n_packed >> 10;
166}
167
168INLINE INT32 mdec_unpack_val( UINT16 n_packed )
169{
170   return ( ( (INT32)n_packed ) << 22 ) >> 22;
171}
172
173UINT32 psxmdec_device::mdec_unpack( UINT32 n_address )
174{
175   psx_state *p_psx = machine().driver_data<psx_state>();
176   UINT32 *p_n_psxram = p_psx->m_p_n_psxram;
177
178   UINT8 n_z;
179   INT32 n_qscale;
180   UINT16 n_packed;
181   INT32 *p_n_block;
182   INT32 p_n_unpacked[ 64 ];
183   INT32 *p_n_q;
184
185   p_n_q = p_n_quantize_uv;
186   p_n_block = m_p_n_unpacked;
187
188   for( UINT32 n_block = 0; n_block < 6; n_block++ )
189   {
190      memset( p_n_unpacked, 0, sizeof( p_n_unpacked ) );
191
192      if( n_block == 2 )
193      {
194         p_n_q = p_n_quantize_y;
195      }
196      n_packed = psxreadword( p_n_psxram, n_address );
197      n_address += 2;
198      if( n_packed == 0xfe00 )
199      {
200         break;
201      }
202
203      n_qscale = mdec_unpack_run( n_packed );
204      p_n_unpacked[ 0 ] = mdec_unpack_val( n_packed ) * p_n_q[ 0 ];
205
206      n_z = 0;
207      for( ;; )
208      {
209         n_packed = psxreadword( p_n_psxram, n_address );
210         n_address += 2;
211
212         if( n_packed == 0xfe00 )
213         {
214            break;
215         }
216         n_z += mdec_unpack_run( n_packed ) + 1;
217         if( n_z > 63 )
218         {
219            break;
220         }
221         p_n_unpacked[ m_p_n_mdec_zigzag[ n_z ] ] = ( mdec_unpack_val( n_packed ) * p_n_q[ n_z ] * n_qscale ) / 8;
222      }
223      mdec_idct( p_n_unpacked, p_n_block );
224      p_n_block += DCTSIZE2;
225   }
226   return n_address;
227}
228
229INLINE INT32 mdec_cr_to_r( INT32 n_cr )
230{
231   return ( 1435 * n_cr ) >> 10;
232}
233
234INLINE INT32 mdec_cr_to_g( INT32 n_cr )
235{
236   return ( -731 * n_cr ) >> 10;
237}
238
239INLINE INT32 mdec_cb_to_g( INT32 n_cb )
240{
241   return ( -351 * n_cb ) >> 10;
242}
243
244INLINE INT32 mdec_cb_to_b( INT32 n_cb )
245{
246   return ( 1814 * n_cb ) >> 10;
247}
248
249UINT16 psxmdec_device::mdec_clamp_r5( INT32 n_r ) const
250{
251   return p_n_r5[ n_r + 128 + 256 ];
252}
253
254UINT16 psxmdec_device::mdec_clamp_g5( INT32 n_g ) const
255{
256   return p_n_g5[ n_g + 128 + 256 ];
257}
258
259UINT16 psxmdec_device::mdec_clamp_b5( INT32 n_b ) const
260{
261   return p_n_b5[ n_b + 128 + 256 ];
262}
263
264void psxmdec_device::mdec_makergb15( UINT32 n_address, INT32 n_r, INT32 n_g, INT32 n_b, INT32 *p_n_y, UINT16 n_stp )
265{
266   p_n_output[ WORD_XOR_LE( n_address + 0 ) / 2 ] = n_stp |
267      mdec_clamp_r5( p_n_y[ 0 ] + n_r ) |
268      mdec_clamp_g5( p_n_y[ 0 ] + n_g ) |
269      mdec_clamp_b5( p_n_y[ 0 ] + n_b );
270
271   p_n_output[ WORD_XOR_LE( n_address + 2 ) / 2 ] = n_stp |
272      mdec_clamp_r5( p_n_y[ 1 ] + n_r ) |
273      mdec_clamp_g5( p_n_y[ 1 ] + n_g ) |
274      mdec_clamp_b5( p_n_y[ 1 ] + n_b );
275}
276
277void psxmdec_device::mdec_yuv2_to_rgb15( void )
278{
279   INT32 n_r;
280   INT32 n_g;
281   INT32 n_b;
282   INT32 n_cb;
283   INT32 n_cr;
284   INT32 *p_n_cb;
285   INT32 *p_n_cr;
286   INT32 *p_n_y;
287   UINT32 n_x;
288   UINT32 n_y;
289   UINT32 n_z;
290   UINT16 n_stp;
291   int n_address = 0;
292
293   if( ( n_0_command & ( 1L << 25 ) ) != 0 )
294   {
295      n_stp = 0x8000;
296   }
297   else
298   {
299      n_stp = 0x0000;
300   }
301
302   p_n_cr = &m_p_n_unpacked[ 0 ];
303   p_n_cb = &m_p_n_unpacked[ DCTSIZE2 ];
304   p_n_y = &m_p_n_unpacked[ DCTSIZE2 * 2 ];
305
306   for( n_z = 0; n_z < 2; n_z++ )
307   {
308      for( n_y = 0; n_y < 4; n_y++ )
309      {
310         for( n_x = 0; n_x < 4; n_x++ )
311         {
312            n_cr = *( p_n_cr );
313            n_cb = *( p_n_cb );
314            n_r = mdec_cr_to_r( n_cr );
315            n_g = mdec_cr_to_g( n_cr ) + mdec_cb_to_g( n_cb );
316            n_b = mdec_cb_to_b( n_cb );
317
318            mdec_makergb15( ( n_address +  0 ), n_r, n_g, n_b, p_n_y, n_stp );
319            mdec_makergb15( ( n_address + 32 ), n_r, n_g, n_b, p_n_y + 8, n_stp );
320
321            n_cr = *( p_n_cr + 4 );
322            n_cb = *( p_n_cb + 4 );
323            n_r = mdec_cr_to_r( n_cr );
324            n_g = mdec_cr_to_g( n_cr ) + mdec_cb_to_g( n_cb );
325            n_b = mdec_cb_to_b( n_cb );
326
327            mdec_makergb15( ( n_address + 16 ), n_r, n_g, n_b, p_n_y + DCTSIZE2, n_stp );
328            mdec_makergb15( ( n_address + 48 ), n_r, n_g, n_b, p_n_y + DCTSIZE2 + 8, n_stp );
329
330            p_n_cr++;
331            p_n_cb++;
332            p_n_y += 2;
333            n_address += 4;
334         }
335         p_n_cr += 4;
336         p_n_cb += 4;
337         p_n_y += 8;
338         n_address += 48;
339      }
340      p_n_y += DCTSIZE2;
341   }
342   n_decoded = ( 16 * 16 ) / 2;
343}
344
345UINT16 psxmdec_device::mdec_clamp8( INT32 n_r ) const
346{
347   return p_n_clamp8[ n_r + 128 + 256 ];
348}
349
350void psxmdec_device::mdec_makergb24( UINT32 n_address, INT32 n_r, INT32 n_g, INT32 n_b, INT32 *p_n_y, UINT32 n_stp )
351{
352   p_n_output[ WORD_XOR_LE( n_address + 0 ) / 2 ] = ( mdec_clamp8( p_n_y[ 0 ] + n_g ) << 8 ) | mdec_clamp8( p_n_y[ 0 ] + n_r );
353   p_n_output[ WORD_XOR_LE( n_address + 2 ) / 2 ] = ( mdec_clamp8( p_n_y[ 1 ] + n_r ) << 8 ) | mdec_clamp8( p_n_y[ 0 ] + n_b );
354   p_n_output[ WORD_XOR_LE( n_address + 4 ) / 2 ] = ( mdec_clamp8( p_n_y[ 1 ] + n_b ) << 8 ) | mdec_clamp8( p_n_y[ 1 ] + n_g );
355}
356
357void psxmdec_device::mdec_yuv2_to_rgb24( void )
358{
359   INT32 n_r;
360   INT32 n_g;
361   INT32 n_b;
362   INT32 n_cb;
363   INT32 n_cr;
364   INT32 *p_n_cb;
365   INT32 *p_n_cr;
366   INT32 *p_n_y;
367   UINT32 n_x;
368   UINT32 n_y;
369   UINT32 n_z;
370   UINT32 n_stp;
371   int n_address = 0;
372
373   if( ( n_0_command & ( 1L << 25 ) ) != 0 )
374   {
375      n_stp = 0x80008000;
376   }
377   else
378   {
379      n_stp = 0x00000000;
380   }
381
382   p_n_cr = &m_p_n_unpacked[ 0 ];
383   p_n_cb = &m_p_n_unpacked[ DCTSIZE2 ];
384   p_n_y = &m_p_n_unpacked[ DCTSIZE2 * 2 ];
385
386   for( n_z = 0; n_z < 2; n_z++ )
387   {
388      for( n_y = 0; n_y < 4; n_y++ )
389      {
390         for( n_x = 0; n_x < 4; n_x++ )
391         {
392            n_cr = *( p_n_cr );
393            n_cb = *( p_n_cb );
394            n_r = mdec_cr_to_r( n_cr );
395            n_g = mdec_cr_to_g( n_cr ) + mdec_cb_to_g( n_cb );
396            n_b = mdec_cb_to_b( n_cb );
397
398            mdec_makergb24( ( n_address +  0 ), n_r, n_g, n_b, p_n_y, n_stp );
399            mdec_makergb24( ( n_address + 48 ), n_r, n_g, n_b, p_n_y + 8, n_stp );
400
401            n_cr = *( p_n_cr + 4 );
402            n_cb = *( p_n_cb + 4 );
403            n_r = mdec_cr_to_r( n_cr );
404            n_g = mdec_cr_to_g( n_cr ) + mdec_cb_to_g( n_cb );
405            n_b = mdec_cb_to_b( n_cb );
406
407            mdec_makergb24( ( n_address + 24 ), n_r, n_g, n_b, p_n_y + DCTSIZE2, n_stp );
408            mdec_makergb24( ( n_address + 72 ), n_r, n_g, n_b, p_n_y + DCTSIZE2 + 8, n_stp );
409
410            p_n_cr++;
411            p_n_cb++;
412            p_n_y += 2;
413            n_address += 6;
414         }
415         p_n_cr += 4;
416         p_n_cb += 4;
417         p_n_y += 8;
418         n_address += 72;
419      }
420      p_n_y += DCTSIZE2;
421   }
422   n_decoded = ( 24 * 16 ) / 2;
423}
424
425void psxmdec_device::dma_write( UINT32 n_address, INT32 n_size )
426{
427   psx_state *p_psx = machine().driver_data<psx_state>();
428   UINT32 *p_n_psxram = p_psx->m_p_n_psxram;
429   int n_index;
430
431   verboselog( machine(), 2, "mdec0_write( %08x, %08x )\n", n_address, n_size );
432
433   switch( n_0_command >> 28 )
434   {
435   case 0x3:
436      verboselog( machine(), 1, "mdec decode %08x %08x %08x\n", n_0_command, n_address, n_size );
437      n_0_address = n_address;
438      n_0_size = n_size * 4;
439      n_1_status |= ( 1L << 29 );
440      break;
441   case 0x4:
442      verboselog( machine(), 1, "mdec quantize table %08x %08x %08x\n", n_0_command, n_address, n_size );
443      n_index = 0;
444      while( n_size > 0 )
445      {
446         if( n_index < DCTSIZE2 )
447         {
448            p_n_quantize_y[ n_index + 0 ] = ( p_n_psxram[ n_address / 4 ] >> 0 ) & 0xff;
449            p_n_quantize_y[ n_index + 1 ] = ( p_n_psxram[ n_address / 4 ] >> 8 ) & 0xff;
450            p_n_quantize_y[ n_index + 2 ] = ( p_n_psxram[ n_address / 4 ] >> 16 ) & 0xff;
451            p_n_quantize_y[ n_index + 3 ] = ( p_n_psxram[ n_address / 4 ] >> 24 ) & 0xff;
452         }
453         else if( n_index < DCTSIZE2 * 2 )
454         {
455            p_n_quantize_uv[ n_index + 0 - DCTSIZE2 ] = ( p_n_psxram[ n_address / 4 ] >> 0 ) & 0xff;
456            p_n_quantize_uv[ n_index + 1 - DCTSIZE2 ] = ( p_n_psxram[ n_address / 4 ] >> 8 ) & 0xff;
457            p_n_quantize_uv[ n_index + 2 - DCTSIZE2 ] = ( p_n_psxram[ n_address / 4 ] >> 16 ) & 0xff;
458            p_n_quantize_uv[ n_index + 3 - DCTSIZE2 ] = ( p_n_psxram[ n_address / 4 ] >> 24 ) & 0xff;
459         }
460         n_index += 4;
461         n_address += 4;
462         n_size--;
463      }
464      break;
465   case 0x6:
466      verboselog( machine(), 1, "mdec cosine table %08x %08x %08x\n", n_0_command, n_address, n_size );
467      n_index = 0;
468      while( n_size > 0 )
469      {
470         p_n_cos[ n_index + 0 ] = (INT16)( ( p_n_psxram[ n_address / 4 ] >> 0 ) & 0xffff );
471         p_n_cos[ n_index + 1 ] = (INT16)( ( p_n_psxram[ n_address / 4 ] >> 16 ) & 0xffff );
472         n_index += 2;
473         n_address += 4;
474         n_size--;
475      }
476      mdec_cos_precalc();
477      break;
478   default:
479      verboselog( machine(), 0, "mdec unknown command %08x %08x %08x\n", n_0_command, n_address, n_size );
480      break;
481   }
482}
483
484void psxmdec_device::dma_read( UINT32 n_address, INT32 n_size )
485{
486   psx_state *p_psx = machine().driver_data<psx_state>();
487   UINT32 *p_n_psxram = p_psx->m_p_n_psxram;
488   UINT32 n_this;
489   UINT32 n_nextaddress;
490
491   verboselog( machine(), 2, "mdec1_read( %08x, %08x )\n", n_address, n_size );
492   if( ( n_0_command & ( 1L << 29 ) ) != 0 && n_0_size != 0 )
493   {
494      while( n_size > 0 )
495      {
496         if( n_decoded == 0 )
497         {
498            if( (int)n_0_size <= 0 )
499            {
500               mame_printf_debug( "ran out of data %08x\n", n_size );
501               n_0_size = 0;
502               break;
503            }
504
505            n_nextaddress = mdec_unpack( n_0_address );
506            n_0_size -= n_nextaddress - n_0_address;
507            n_0_address = n_nextaddress;
508
509            if( ( n_0_command & ( 1L << 27 ) ) != 0 )
510            {
511               mdec_yuv2_to_rgb15();
512            }
513            else
514            {
515               mdec_yuv2_to_rgb24();
516            }
517            n_offset = 0;
518         }
519
520         n_this = n_decoded;
521         if( n_this > n_size )
522         {
523            n_this = n_size;
524         }
525         n_decoded -= n_this;
526
527         memcpy( (UINT8 *)p_n_psxram + n_address, (UINT8 *)p_n_output + n_offset, n_this * 4 );
528         n_offset += n_this * 4;
529         n_address += n_this * 4;
530         n_size -= n_this;
531      }
532
533      if( (int)n_0_size < 0 )
534      {
535         mame_printf_debug( "ran out of data %d\n", n_0_size );
536      }
537   }
538   else
539   {
540      mame_printf_debug( "mdec1_read no conversion :%08x:%08x:\n", n_0_command, n_0_size );
541   }
542   n_1_status &= ~( 1L << 29 );
543}
544
545WRITE32_MEMBER( psxmdec_device::write )
546{
547   switch( offset )
548   {
549   case 0:
550      verboselog( machine(), 2, "mdec 0 command %08x\n", data );
551      n_0_command = data;
552      break;
553   case 1:
554      verboselog( machine(), 2, "mdec 1 command %08x\n", data );
555      n_1_command = data;
556      break;
557   }
558}
559
560READ32_MEMBER( psxmdec_device::read )
561{
562   switch( offset )
563   {
564   case 0:
565      verboselog( machine(), 2, "mdec 0 status %08x\n", 0 );
566      return 0;
567   case 1:
568      verboselog( machine(), 2, "mdec 1 status %08x\n", n_1_status );
569      return n_1_status;
570   }
571   return 0;
572}
src/emu/cpu/psx/mdec.h
r12101r12102
1/*
2 * PlayStation DMA emulator
3 *
4 * Copyright 2003-2011 smf
5 *
6 */
7 
8#pragma once
9 
10#ifndef __PSXMDEC_H__
11#define __PSXMDEC_H__
12 
13#include "emu.h"
14 
15extern const device_type PSX_MDEC;
16 
17#define   DCTSIZE ( 8 )
18#define   DCTSIZE2 ( DCTSIZE * DCTSIZE )
19 
20#define MDEC_COS_PRECALC_BITS ( 21 )
21 
22class psxmdec_device : public device_t
23{
24public:
25   psxmdec_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
26 
27   WRITE32_MEMBER( write );
28   READ32_MEMBER( read );
29 
30   void dma_write( UINT32 n_address, INT32 n_size );
31   void dma_read( UINT32 n_address, INT32 n_size );
32 
33protected:
34   virtual void device_start();
35   virtual void device_reset();
36   virtual void device_post_load();
37 
38private:
39   void mdec_cos_precalc();
40   void mdec_idct( INT32 *p_n_src, INT32 *p_n_dst );
41   UINT32 mdec_unpack( UINT32 n_address );
42   UINT16 mdec_clamp_r5( INT32 n_r ) const;
43   UINT16 mdec_clamp_g5( INT32 n_g ) const;
44   UINT16 mdec_clamp_b5( INT32 n_b ) const;
45   UINT16 mdec_clamp8( INT32 n_r ) const;
46   void mdec_yuv2_to_rgb15( void );
47   void mdec_yuv2_to_rgb24( void );
48   void mdec_makergb15( UINT32 n_address, INT32 n_r, INT32 n_g, INT32 n_b, INT32 *p_n_y, UINT16 n_stp );
49   void mdec_makergb24( UINT32 n_address, INT32 n_r, INT32 n_g, INT32 n_b, INT32 *p_n_y, UINT32 n_stp );
50 
51   UINT32 n_decoded;
52   UINT32 n_offset;
53   UINT16 p_n_output[ 24 * 16 ];
54 
55   INT32 p_n_quantize_y[ DCTSIZE2 ];
56   INT32 p_n_quantize_uv[ DCTSIZE2 ];
57   INT32 p_n_cos[ DCTSIZE2 ];
58   INT32 p_n_cos_precalc[ DCTSIZE2 * DCTSIZE2 ];
59 
60   UINT32 n_0_command;
61   UINT32 n_0_address;
62   UINT32 n_0_size;
63   UINT32 n_1_command;
64   UINT32 n_1_status;
65 
66   UINT16 p_n_clamp8[ 256 * 3 ];
67   UINT16 p_n_r5[ 256 * 3 ];
68   UINT16 p_n_g5[ 256 * 3 ];
69   UINT16 p_n_b5[ 256 * 3 ];
70 
71   INT32 m_p_n_unpacked[ DCTSIZE2 * 6 * 2 ];
72};
73 
74#endif
1/*
2 * PlayStation DMA emulator
3 *
4 * Copyright 2003-2011 smf
5 *
6 */
7
8#pragma once
9
10#ifndef __PSXMDEC_H__
11#define __PSXMDEC_H__
12
13#include "emu.h"
14
15extern const device_type PSX_MDEC;
16
17#define   DCTSIZE ( 8 )
18#define   DCTSIZE2 ( DCTSIZE * DCTSIZE )
19
20#define MDEC_COS_PRECALC_BITS ( 21 )
21
22class psxmdec_device : public device_t
23{
24public:
25   psxmdec_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
26
27   WRITE32_MEMBER( write );
28   READ32_MEMBER( read );
29
30   void dma_write( UINT32 n_address, INT32 n_size );
31   void dma_read( UINT32 n_address, INT32 n_size );
32
33protected:
34   virtual void device_start();
35   virtual void device_reset();
36   virtual void device_post_load();
37
38private:
39   void mdec_cos_precalc();
40   void mdec_idct( INT32 *p_n_src, INT32 *p_n_dst );
41   UINT32 mdec_unpack( UINT32 n_address );
42   UINT16 mdec_clamp_r5( INT32 n_r ) const;
43   UINT16 mdec_clamp_g5( INT32 n_g ) const;
44   UINT16 mdec_clamp_b5( INT32 n_b ) const;
45   UINT16 mdec_clamp8( INT32 n_r ) const;
46   void mdec_yuv2_to_rgb15( void );
47   void mdec_yuv2_to_rgb24( void );
48   void mdec_makergb15( UINT32 n_address, INT32 n_r, INT32 n_g, INT32 n_b, INT32 *p_n_y, UINT16 n_stp );
49   void mdec_makergb24( UINT32 n_address, INT32 n_r, INT32 n_g, INT32 n_b, INT32 *p_n_y, UINT32 n_stp );
50
51   UINT32 n_decoded;
52   UINT32 n_offset;
53   UINT16 p_n_output[ 24 * 16 ];
54
55   INT32 p_n_quantize_y[ DCTSIZE2 ];
56   INT32 p_n_quantize_uv[ DCTSIZE2 ];
57   INT32 p_n_cos[ DCTSIZE2 ];
58   INT32 p_n_cos_precalc[ DCTSIZE2 * DCTSIZE2 ];
59
60   UINT32 n_0_command;
61   UINT32 n_0_address;
62   UINT32 n_0_size;
63   UINT32 n_1_command;
64   UINT32 n_1_status;
65
66   UINT16 p_n_clamp8[ 256 * 3 ];
67   UINT16 p_n_r5[ 256 * 3 ];
68   UINT16 p_n_g5[ 256 * 3 ];
69   UINT16 p_n_b5[ 256 * 3 ];
70
71   INT32 m_p_n_unpacked[ DCTSIZE2 * 6 * 2 ];
72};
73
74#endif
src/emu/video/psx.h
r12101r12102
1/*
2 * PlayStation GPU emulator
3 *
4 * Copyright 2003-2011 smf
5 *
6 */
7 
8#pragma once
9 
10#ifndef __PSXGPU_H__
11#define __PSXGPU_H__
12 
13#include "emu.h"
14 
15#define MCFG_PSXGPU_ADD( cputag, tag, type, clock ) \
16   MCFG_DEVICE_ADD( tag, type, clock ) \
17   MCFG_PSX_DMA_CHANNEL_READ( cputag, 2, psx_dma_write_delegate( FUNC( psxgpu_device::dma_read ), (psxgpu_device *) device ) ) \
18   MCFG_PSX_DMA_CHANNEL_WRITE( cputag, 2, psx_dma_read_delegate( FUNC( psxgpu_device::dma_write ), (psxgpu_device *) device ) )
19 
20#define MCFG_PSXGPU_REPLACE( cputag, tag, type, clock ) \
21   MCFG_DEVICE_REPLACE( tag, type, clock ) \
22   MCFG_PSX_DMA_CHANNEL_READ( cputag, 2, psx_dma_write_delegate( FUNC( psxgpu_device::dma_read ), (psxgpu_device *) device ) ) \
23   MCFG_PSX_DMA_CHANNEL_WRITE( cputag, 2, psx_dma_read_delegate( FUNC( psxgpu_device::dma_write ), (psxgpu_device *) device ) )
24 
25extern const device_type CXD8514Q;
26extern const device_type CXD8538Q;
27extern const device_type CXD8561Q;
28extern const device_type CXD8561BQ;
29extern const device_type CXD8561CQ;
30extern const device_type CXD8654Q;
31 
32#define STOP_ON_ERROR ( 0 )
33 
34#define MAX_LEVEL ( 32 )
35#define MID_LEVEL ( ( MAX_LEVEL / 2 ) << 8 )
36#define MAX_SHADE ( 0x100 )
37#define MID_SHADE ( 0x80 )
38 
39#define DEBUG_COORDS ( 10 )
40#define DEBUG_MAX ( 512 )
41 
42typedef struct _psx_gpu_debug psx_gpu_debug;
43struct _psx_gpu_debug
44{
45   bitmap_t *mesh;
46   int b_clear;
47   int b_mesh;
48   int n_skip;
49   int b_texture;
50   int n_interleave;
51   int n_coord;
52   int n_coordx[ DEBUG_COORDS ];
53   int n_coordy[ DEBUG_COORDS ];
54};
55 
56struct FLATVERTEX
57{
58   PAIR n_coord;
59};
60 
61struct GOURAUDVERTEX
62{
63   PAIR n_bgr;
64   PAIR n_coord;
65};
66 
67struct FLATTEXTUREDVERTEX
68{
69   PAIR n_coord;
70   PAIR n_texture;
71};
72 
73struct GOURAUDTEXTUREDVERTEX
74{
75   PAIR n_bgr;
76   PAIR n_coord;
77   PAIR n_texture;
78};
79 
80union PACKET
81{
82   UINT32 n_entry[ 16 ];
83 
84   struct
85   {
86      PAIR n_cmd;
87      struct FLATVERTEX vertex[ 2 ];
88      PAIR n_size;
89   } MoveImage;
90 
91   struct
92   {
93      PAIR n_bgr;
94      PAIR n_coord;
95      PAIR n_size;
96   } FlatRectangle;
97 
98   struct
99   {
100      PAIR n_bgr;
101      PAIR n_coord;
102   } FlatRectangle8x8;
103 
104   struct
105   {
106      PAIR n_bgr;
107      PAIR n_coord;
108   } FlatRectangle16x16;
109 
110   struct
111   {
112      PAIR n_bgr;
113      PAIR n_coord;
114      PAIR n_texture;
115   } Sprite8x8;
116 
117   struct
118   {
119      PAIR n_bgr;
120      PAIR n_coord;
121      PAIR n_texture;
122   } Sprite16x16;
123 
124   struct
125   {
126      PAIR n_bgr;
127      PAIR n_coord;
128      PAIR n_texture;
129      PAIR n_size;
130   } FlatTexturedRectangle;
131 
132   struct
133   {
134      PAIR n_bgr;
135      struct FLATVERTEX vertex[ 4 ];
136   } FlatPolygon;
137 
138   struct
139   {
140      struct GOURAUDVERTEX vertex[ 4 ];
141   } GouraudPolygon;
142 
143   struct
144   {
145      PAIR n_bgr;
146      struct FLATVERTEX vertex[ 2 ];
147   } MonochromeLine;
148 
149   struct
150   {
151      struct GOURAUDVERTEX vertex[ 2 ];
152   } GouraudLine;
153 
154   struct
155   {
156      PAIR n_bgr;
157      struct FLATTEXTUREDVERTEX vertex[ 4 ];
158   } FlatTexturedPolygon;
159 
160   struct
161   {
162      struct GOURAUDTEXTUREDVERTEX vertex[ 4 ];
163   } GouraudTexturedPolygon;
164 
165   struct
166   {
167      PAIR n_bgr;
168      struct FLATVERTEX vertex;
169   } Dot;
170};
171 
172class psxgpu_device : public device_t
173{
174public:
175   // construction/destruction
176   psxgpu_device(const machine_config &mconfig, device_type type, const char *name, const char *tag, device_t *owner, UINT32 clock);
177 
178   void update_screen(bitmap_t *bitmap, const rectangle *cliprect);
179   WRITE32_MEMBER( write );
180   READ32_MEMBER( read );
181   void dma_read( UINT32 n_address, INT32 n_size );
182   void dma_write( UINT32 n_address, INT32 n_size );
183   void lightgun_set( int, int );
184   void vblank( void );
185 
186protected:
187   virtual void device_start();
188 
189   void updatevisiblearea();
190   void decode_tpage( UINT32 tpage );
191   void FlatPolygon( int n_points );
192   void FlatTexturedPolygon( int n_points );
193   void GouraudPolygon( int n_points );
194   void GouraudTexturedPolygon( int n_points );
195   void MonochromeLine( void );
196   void GouraudLine( void );
197   void FrameBufferRectangleDraw( void );
198   void FlatRectangle( void );
199   void FlatRectangle8x8( void );
200   void FlatRectangle16x16( void );
201   void FlatTexturedRectangle( void );
202   void Sprite8x8( void );
203   void Sprite16x16( void );
204   void Dot( void );
205   void MoveImage( void );
206   void psx_gpu_init( int n_gputype );
207   void gpu_reset();
208   void gpu_read( UINT32 *p_ram, INT32 n_size );
209   void gpu_write( UINT32 *p_ram, INT32 n_size );
210 
211#if defined( MAME_DEBUG )
212   void DebugMeshInit( void );
213   void DebugMesh( int n_coordx, int n_coordy );
214   void DebugMeshEnd( void );
215   void DebugCheckKeys( void );
216   int DebugMeshDisplay( bitmap_t *bitmap, const rectangle *cliprect );
217   int DebugTextureDisplay( bitmap_t *bitmap );
218#endif
219 
220   INT32 m_n_tx;
221   INT32 m_n_ty;
222   INT32 n_abr;
223   INT32 n_tp;
224   INT32 n_ix;
225   INT32 n_iy;
226   INT32 n_ti;
227 
228   UINT16 *p_vram;
229   UINT32 n_vram_size;
230   UINT32 n_vramx;
231   UINT32 n_vramy;
232   UINT32 n_twy;
233   UINT32 n_twx;
234   UINT32 n_twh;
235   UINT32 n_tww;
236   UINT32 n_drawarea_x1;
237   UINT32 n_drawarea_y1;
238   UINT32 n_drawarea_x2;
239   UINT32 n_drawarea_y2;
240   UINT32 n_horiz_disstart;
241   UINT32 n_horiz_disend;
242   UINT32 n_vert_disstart;
243   UINT32 n_vert_disend;
244   UINT32 b_reverseflag;
245   INT32 n_drawoffset_x;
246   INT32 n_drawoffset_y;
247   UINT32 m_n_displaystartx;
248   UINT32 n_displaystarty;
249   int m_n_gputype;
250   UINT32 n_gpustatus;
251   UINT32 n_gpuinfo;
252   UINT32 n_gpu_buffer_offset;
253   UINT32 n_lightgun_x;
254   UINT32 n_lightgun_y;
255   UINT32 n_screenwidth;
256   UINT32 n_screenheight;
257 
258   PACKET m_packet;
259 
260   psx_gpu_debug m_debug;
261 
262   UINT16 *p_p_vram[ 1024 ];
263 
264   UINT16 p_n_redshade[ MAX_LEVEL * MAX_SHADE ];
265   UINT16 p_n_greenshade[ MAX_LEVEL * MAX_SHADE ];
266   UINT16 p_n_blueshade[ MAX_LEVEL * MAX_SHADE ];
267   UINT16 p_n_redlevel[ 0x10000 ];
268   UINT16 p_n_greenlevel[ 0x10000 ];
269   UINT16 p_n_bluelevel[ 0x10000 ];
270 
271   UINT16 p_n_f025[ MAX_LEVEL * MAX_SHADE ];
272   UINT16 p_n_f05[ MAX_LEVEL * MAX_SHADE ];
273   UINT16 p_n_f1[ MAX_LEVEL * MAX_SHADE ];
274   UINT16 p_n_redb05[ 0x10000 ];
275   UINT16 p_n_greenb05[ 0x10000 ];
276   UINT16 p_n_blueb05[ 0x10000 ];
277   UINT16 p_n_redb1[ 0x10000 ];
278   UINT16 p_n_greenb1[ 0x10000 ];
279   UINT16 p_n_blueb1[ 0x10000 ];
280   UINT16 p_n_redaddtrans[ MAX_LEVEL * MAX_LEVEL ];
281   UINT16 p_n_greenaddtrans[ MAX_LEVEL * MAX_LEVEL ];
282   UINT16 p_n_blueaddtrans[ MAX_LEVEL * MAX_LEVEL ];
283   UINT16 p_n_redsubtrans[ MAX_LEVEL * MAX_LEVEL ];
284   UINT16 p_n_greensubtrans[ MAX_LEVEL * MAX_LEVEL ];
285   UINT16 p_n_bluesubtrans[ MAX_LEVEL * MAX_LEVEL ];
286 
287   UINT16 p_n_g0r0[ 0x10000 ];
288   UINT16 p_n_b0[ 0x10000 ];
289   UINT16 p_n_r1[ 0x10000 ];
290   UINT16 p_n_b1g1[ 0x10000 ];
291};
292 
293class cxd8514q_device : public psxgpu_device
294{
295public:
296   // construction/destruction
297   cxd8514q_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
298};
299 
300class cxd8538q_device : public psxgpu_device
301{
302public:
303   // construction/destruction
304   cxd8538q_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
305};
306 
307class cxd8561q_device : public psxgpu_device
308{
309public:
310   // construction/destruction
311   cxd8561q_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
312};
313 
314class cxd8561bq_device : public psxgpu_device
315{
316public:
317   // construction/destruction
318   cxd8561bq_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
319};
320 
321class cxd8561cq_device : public psxgpu_device
322{
323public:
324   // construction/destruction
325   cxd8561cq_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
326};
327 
328class cxd8654q_device : public psxgpu_device
329{
330public:
331   // construction/destruction
332   cxd8654q_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
333};
334 
335#endif
1/*
2 * PlayStation GPU emulator
3 *
4 * Copyright 2003-2011 smf
5 *
6 */
7
8#pragma once
9
10#ifndef __PSXGPU_H__
11#define __PSXGPU_H__
12
13#include "emu.h"
14
15#define MCFG_PSXGPU_ADD( cputag, tag, type, clock ) \
16   MCFG_DEVICE_ADD( tag, type, clock ) \
17   MCFG_PSX_DMA_CHANNEL_READ( cputag, 2, psx_dma_write_delegate( FUNC( psxgpu_device::dma_read ), (psxgpu_device *) device ) ) \
18   MCFG_PSX_DMA_CHANNEL_WRITE( cputag, 2, psx_dma_read_delegate( FUNC( psxgpu_device::dma_write ), (psxgpu_device *) device ) )
19
20#define MCFG_PSXGPU_REPLACE( cputag, tag, type, clock ) \
21   MCFG_DEVICE_REPLACE( tag, type, clock ) \
22   MCFG_PSX_DMA_CHANNEL_READ( cputag, 2, psx_dma_write_delegate( FUNC( psxgpu_device::dma_read ), (psxgpu_device *) device ) ) \
23   MCFG_PSX_DMA_CHANNEL_WRITE( cputag, 2, psx_dma_read_delegate( FUNC( psxgpu_device::dma_write ), (psxgpu_device *) device ) )
24
25extern const device_type CXD8514Q;
26extern const device_type CXD8538Q;
27extern const device_type CXD8561Q;
28extern const device_type CXD8561BQ;
29extern const device_type CXD8561CQ;
30extern const device_type CXD8654Q;
31
32#define STOP_ON_ERROR ( 0 )
33
34#define MAX_LEVEL ( 32 )
35#define MID_LEVEL ( ( MAX_LEVEL / 2 ) << 8 )
36#define MAX_SHADE ( 0x100 )
37#define MID_SHADE ( 0x80 )
38
39#define DEBUG_COORDS ( 10 )
40#define DEBUG_MAX ( 512 )
41
42typedef struct _psx_gpu_debug psx_gpu_debug;
43struct _psx_gpu_debug
44{
45   bitmap_t *mesh;
46   int b_clear;
47   int b_mesh;
48   int n_skip;
49   int b_texture;
50   int n_interleave;
51   int n_coord;
52   int n_coordx[ DEBUG_COORDS ];
53   int n_coordy[ DEBUG_COORDS ];
54};
55
56struct FLATVERTEX
57{
58   PAIR n_coord;
59};
60
61struct GOURAUDVERTEX
62{
63   PAIR n_bgr;
64   PAIR n_coord;
65};
66
67struct FLATTEXTUREDVERTEX
68{
69   PAIR n_coord;
70   PAIR n_texture;
71};
72
73struct GOURAUDTEXTUREDVERTEX
74{
75   PAIR n_bgr;
76   PAIR n_coord;
77   PAIR n_texture;
78};
79
80union PACKET
81{
82   UINT32 n_entry[ 16 ];
83
84   struct
85   {
86      PAIR n_cmd;
87      struct FLATVERTEX vertex[ 2 ];
88      PAIR n_size;
89   } MoveImage;
90
91   struct
92   {
93      PAIR n_bgr;
94      PAIR n_coord;
95      PAIR n_size;
96   } FlatRectangle;
97
98   struct
99   {
100      PAIR n_bgr;
101      PAIR n_coord;
102   } FlatRectangle8x8;
103
104   struct
105   {
106      PAIR n_bgr;
107      PAIR n_coord;
108   } FlatRectangle16x16;
109
110   struct
111   {
112      PAIR n_bgr;
113      PAIR n_coord;
114      PAIR n_texture;
115   } Sprite8x8;
116
117   struct
118   {
119      PAIR n_bgr;
120      PAIR n_coord;
121      PAIR n_texture;
122   } Sprite16x16;
123
124   struct
125   {
126      PAIR n_bgr;
127      PAIR n_coord;
128      PAIR n_texture;
129      PAIR n_size;
130   } FlatTexturedRectangle;
131
132   struct
133   {
134      PAIR n_bgr;
135      struct FLATVERTEX vertex[ 4 ];
136   } FlatPolygon;
137
138   struct
139   {
140      struct GOURAUDVERTEX vertex[ 4 ];
141   } GouraudPolygon;
142
143   struct
144   {
145      PAIR n_bgr;
146      struct FLATVERTEX vertex[ 2 ];
147   } MonochromeLine;
148
149   struct
150   {
151      struct GOURAUDVERTEX vertex[ 2 ];
152   } GouraudLine;
153
154   struct
155   {
156      PAIR n_bgr;
157      struct FLATTEXTUREDVERTEX vertex[ 4 ];
158   } FlatTexturedPolygon;
159
160   struct
161   {
162      struct GOURAUDTEXTUREDVERTEX vertex[ 4 ];
163   } GouraudTexturedPolygon;
164
165   struct
166   {
167      PAIR n_bgr;
168      struct FLATVERTEX vertex;
169   } Dot;
170};
171
172class psxgpu_device : public device_t
173{
174public:
175   // construction/destruction
176   psxgpu_device(const machine_config &mconfig, device_type type, const char *name, const char *tag, device_t *owner, UINT32 clock);
177
178   void update_screen(bitmap_t *bitmap, const rectangle *cliprect);
179   WRITE32_MEMBER( write );
180   READ32_MEMBER( read );
181   void dma_read( UINT32 n_address, INT32 n_size );
182   void dma_write( UINT32 n_address, INT32 n_size );
183   void lightgun_set( int, int );
184   void vblank( void );
185
186protected:
187   virtual void device_start();
188
189   void updatevisiblearea();
190   void decode_tpage( UINT32 tpage );
191   void FlatPolygon( int n_points );
192   void FlatTexturedPolygon( int n_points );
193   void GouraudPolygon( int n_points );
194   void GouraudTexturedPolygon( int n_points );
195   void MonochromeLine( void );
196   void GouraudLine( void );
197   void FrameBufferRectangleDraw( void );
198   void FlatRectangle( void );
199   void FlatRectangle8x8( void );
200   void FlatRectangle16x16( void );
201   void FlatTexturedRectangle( void );
202   void Sprite8x8( void );
203   void Sprite16x16( void );
204   void Dot( void );
205   void MoveImage( void );
206   void psx_gpu_init( int n_gputype );
207   void gpu_reset();
208   void gpu_read( UINT32 *p_ram, INT32 n_size );
209   void gpu_write( UINT32 *p_ram, INT32 n_size );
210
211#if defined( MAME_DEBUG )
212   void DebugMeshInit( void );
213   void DebugMesh( int n_coordx, int n_coordy );
214   void DebugMeshEnd( void );
215   void DebugCheckKeys( void );
216   int DebugMeshDisplay( bitmap_t *bitmap, const rectangle *cliprect );
217   int DebugTextureDisplay( bitmap_t *bitmap );
218#endif
219
220   INT32 m_n_tx;
221   INT32 m_n_ty;
222   INT32 n_abr;
223   INT32 n_tp;
224   INT32 n_ix;
225   INT32 n_iy;
226   INT32 n_ti;
227
228   UINT16 *p_vram;
229   UINT32 n_vram_size;
230   UINT32 n_vramx;
231   UINT32 n_vramy;
232   UINT32 n_twy;
233   UINT32 n_twx;
234   UINT32 n_twh;
235   UINT32 n_tww;
236   UINT32 n_drawarea_x1;
237   UINT32 n_drawarea_y1;
238   UINT32 n_drawarea_x2;
239   UINT32 n_drawarea_y2;
240   UINT32 n_horiz_disstart;
241   UINT32 n_horiz_disend;
242   UINT32 n_vert_disstart;
243   UINT32 n_vert_disend;
244   UINT32 b_reverseflag;
245   INT32 n_drawoffset_x;
246   INT32 n_drawoffset_y;
247   UINT32 m_n_displaystartx;
248   UINT32 n_displaystarty;
249   int m_n_gputype;
250   UINT32 n_gpustatus;
251   UINT32 n_gpuinfo;
252   UINT32 n_gpu_buffer_offset;
253   UINT32 n_lightgun_x;
254   UINT32 n_lightgun_y;
255   UINT32 n_screenwidth;
256   UINT32 n_screenheight;
257
258   PACKET m_packet;
259
260   psx_gpu_debug m_debug;
261
262   UINT16 *p_p_vram[ 1024 ];
263
264   UINT16 p_n_redshade[ MAX_LEVEL * MAX_SHADE ];
265   UINT16 p_n_greenshade[ MAX_LEVEL * MAX_SHADE ];
266   UINT16 p_n_blueshade[ MAX_LEVEL * MAX_SHADE ];
267   UINT16 p_n_redlevel[ 0x10000 ];
268   UINT16 p_n_greenlevel[ 0x10000 ];
269   UINT16 p_n_bluelevel[ 0x10000 ];
270
271   UINT16 p_n_f025[ MAX_LEVEL * MAX_SHADE ];
272   UINT16 p_n_f05[ MAX_LEVEL * MAX_SHADE ];
273   UINT16 p_n_f1[ MAX_LEVEL * MAX_SHADE ];
274   UINT16 p_n_redb05[ 0x10000 ];
275   UINT16 p_n_greenb05[ 0x10000 ];
276   UINT16 p_n_blueb05[ 0x10000 ];
277   UINT16 p_n_redb1[ 0x10000 ];
278   UINT16 p_n_greenb1[ 0x10000 ];
279   UINT16 p_n_blueb1[ 0x10000 ];
280   UINT16 p_n_redaddtrans[ MAX_LEVEL * MAX_LEVEL ];
281   UINT16 p_n_greenaddtrans[ MAX_LEVEL * MAX_LEVEL ];
282   UINT16 p_n_blueaddtrans[ MAX_LEVEL * MAX_LEVEL ];
283   UINT16 p_n_redsubtrans[ MAX_LEVEL * MAX_LEVEL ];
284   UINT16 p_n_greensubtrans[ MAX_LEVEL * MAX_LEVEL ];
285   UINT16 p_n_bluesubtrans[ MAX_LEVEL * MAX_LEVEL ];
286
287   UINT16 p_n_g0r0[ 0x10000 ];
288   UINT16 p_n_b0[ 0x10000 ];
289   UINT16 p_n_r1[ 0x10000 ];
290   UINT16 p_n_b1g1[ 0x10000 ];
291};
292
293class cxd8514q_device : public psxgpu_device
294{
295public:
296   // construction/destruction
297   cxd8514q_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
298};
299
300class cxd8538q_device : public psxgpu_device
301{
302public:
303   // construction/destruction
304   cxd8538q_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
305};
306
307class cxd8561q_device : public psxgpu_device
308{
309public:
310   // construction/destruction
311   cxd8561q_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
312};
313
314class cxd8561bq_device : public psxgpu_device
315{
316public:
317   // construction/destruction
318   cxd8561bq_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
319};
320
321class cxd8561cq_device : public psxgpu_device
322{
323public:
324   // construction/destruction
325   cxd8561cq_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
326};
327
328class cxd8654q_device : public psxgpu_device
329{
330public:
331   // construction/destruction
332   cxd8654q_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
333};
334
335#endif
src/lib/cothread/test/test_timing.cpp
r12101r12102
1#include "test.h"
2enum { Iterations = 500000000 };
3 
4namespace thread {
5  cothread_t x;
6  cothread_t y;
7  volatile int counter;
8}
9 
10void co_timingtest() {
11  for(;;) {
12    thread::counter++;
13    co_switch(thread::x);
14  }
15}
16 
17void sub_timingtest() {
18  thread::counter++;
19}
20 
21int main() {
22  printf("context-switching timing test\n\n");
23  time_t start, end;
24  int i, t1, t2;
25 
26  start = clock();
27  for(thread::counter = 0, i = 0; i < Iterations; i++) {
28    sub_timingtest();
29  }
30  end = clock();
31 
32  t1 = (int)difftime(end, start);
33  printf("%2.3f seconds per  50 million subroutine calls (%d iterations)\n", (float)t1 / CLOCKS_PER_SEC, thread::counter);
34 
35  thread::x = co_active();
36  thread::y = co_create(65536, co_timingtest);
37 
38  start = clock();
39  for(thread::counter = 0, i = 0; i < Iterations; i++) {
40    co_switch(thread::y);
41  }
42  end = clock();
43 
44  co_delete(thread::y);
45 
46  t2 = (int)difftime(end, start);
47  printf("%2.3f seconds per 100 million co_switch  calls (%d iterations)\n", (float)t2 / CLOCKS_PER_SEC, thread::counter);
48 
49  printf("co_switch skew = %fx\n\n", (double)t2 / (double)t1);
50  return 0;
51}
52 
1#include "test.h"
2enum { Iterations = 500000000 };
3
4namespace thread {
5  cothread_t x;
6  cothread_t y;
7  volatile int counter;
8}
9
10void co_timingtest() {
11  for(;;) {
12    thread::counter++;
13    co_switch(thread::x);
14  }
15}
16
17void sub_timingtest() {
18  thread::counter++;
19}
20
21int main() {
22  printf("context-switching timing test\n\n");
23  time_t start, end;
24  int i, t1, t2;
25
26  start = clock();
27  for(thread::counter = 0, i = 0; i < Iterations; i++) {
28    sub_timingtest();
29  }
30  end = clock();
31
32  t1 = (int)difftime(end, start);
33  printf("%2.3f seconds per  50 million subroutine calls (%d iterations)\n", (float)t1 / CLOCKS_PER_SEC, thread::counter);
34
35  thread::x = co_active();
36  thread::y = co_create(65536, co_timingtest);
37
38  start = clock();
39  for(thread::counter = 0, i = 0; i < Iterations; i++) {
40    co_switch(thread::y);
41  }
42  end = clock();
43
44  co_delete(thread::y);
45
46  t2 = (int)difftime(end, start);
47  printf("%2.3f seconds per 100 million co_switch  calls (%d iterations)\n", (float)t2 / CLOCKS_PER_SEC, thread::counter);
48
49  printf("co_switch skew = %fx\n\n", (double)t2 / (double)t1);
50  return 0;
51}
52
src/lib/cothread/test/cc.bat
r12101r12102
1mingw32-gcc -O3 -fomit-frame-pointer -o libco.o -c ../libco.c
2mingw32-g++ -O3 -fomit-frame-pointer -c test_timing.cpp
3mingw32-g++ -O3 -fomit-frame-pointer test_timing.o libco.o -o test_timing.exe
4@del *.o
1mingw32-gcc -O3 -fomit-frame-pointer -o libco.o -c ../libco.c
2mingw32-g++ -O3 -fomit-frame-pointer -c test_timing.cpp
3mingw32-g++ -O3 -fomit-frame-pointer test_timing.o libco.o -o test_timing.exe
4@del *.o
src/lib/cothread/test/test_args.cpp
r12101r12102
1/*****
2 * cothread parameterized function example
3 *****
4 * entry point to cothreads cannot take arguments.
5 * this is due to portability issues: each processor,
6 * operating system, programming language and compiler
7 * can use different parameter passing methods, so
8 * arguments to the cothread entry points were omitted.
9 *
10 * however, the behavior can easily be simulated by use
11 * of a specialized co_switch to set global parameters to
12 * be used as function arguments.
13 *
14 * in this way, with a bit of extra red tape, one gains
15 * even more flexibility than would be possible with a
16 * fixed argument list entry point, such as void (*)(void*),
17 * as any number of arguments can be used.
18 *
19 * this also eliminates race conditions where a pointer
20 * passed to co_create may have changed or become invalidated
21 * before call to co_switch, as said pointer would now be set
22 * when calling co_switch, instead.
23 *****/
24 
25#include "test.h"
26 
27cothread_t thread[3];
28 
29namespace co_arg {
30  int param_x;
31  int param_y;
32};
33 
34//one could also call this co_init or somesuch if they preferred ...
35void co_switch(cothread_t thread, int param_x, int param_y) {
36  co_arg::param_x = param_x;
37  co_arg::param_y = param_y;
38  co_switch(thread);
39}
40 
41void co_entrypoint() {
42int param_x = co_arg::param_x;
43int param_y = co_arg::param_y;
44  printf("co_entrypoint(%d, %d)\n", param_x, param_y);
45  co_switch(thread[0]);
46 
47//co_arg::param_x will change here (due to co_switch(cothread_t, int, int) call changing values),
48//however, param_x and param_y will persist as they are thread local
49 
50  printf("co_entrypoint(%d, %d)\n", param_x, param_y);
51  co_switch(thread[0]);
52  throw;
53}
54 
55int main() {
56  printf("cothread parameterized function example\n\n");
57 
58  thread[0] = co_active();
59  thread[1] = co_create(65536, co_entrypoint);
60  thread[2] = co_create(65536, co_entrypoint);
61 
62//use specialized co_switch(cothread_t, int, int) for initial co_switch call
63  co_switch(thread[1], 1, 2);
64  co_switch(thread[2], 4, 8);
65 
66//after first call, entry point arguments have been initialized, standard
67//co_switch(cothread_t) can be used from now on
68  co_switch(thread[2]);
69  co_switch(thread[1]);
70 
71  printf("\ndone\n");
72#if defined(_MSC_VER) || defined(__DJGPP__)
73  getch();
74#endif
75  return 0;
76}
1/*****
2 * cothread parameterized function example
3 *****
4 * entry point to cothreads cannot take arguments.
5 * this is due to portability issues: each processor,
6 * operating system, programming language and compiler
7 * can use different parameter passing methods, so
8 * arguments to the cothread entry points were omitted.
9 *
10 * however, the behavior can easily be simulated by use
11 * of a specialized co_switch to set global parameters to
12 * be used as function arguments.
13 *
14 * in this way, with a bit of extra red tape, one gains
15 * even more flexibility than would be possible with a
16 * fixed argument list entry point, such as void (*)(void*),
17 * as any number of arguments can be used.
18 *
19 * this also eliminates race conditions where a pointer
20 * passed to co_create may have changed or become invalidated
21 * before call to co_switch, as said pointer would now be set
22 * when calling co_switch, instead.
23 *****/
24
25#include "test.h"
26
27cothread_t thread[3];
28
29namespace co_arg {
30  int param_x;
31  int param_y;
32};
33
34//one could also call this co_init or somesuch if they preferred ...
35void co_switch(cothread_t thread, int param_x, int param_y) {
36  co_arg::param_x = param_x;
37  co_arg::param_y = param_y;
38  co_switch(thread);
39}
40
41void co_entrypoint() {
42int param_x = co_arg::param_x;
43int param_y = co_arg::param_y;
44  printf("co_entrypoint(%d, %d)\n", param_x, param_y);
45  co_switch(thread[0]);
46
47//co_arg::param_x will change here (due to co_switch(cothread_t, int, int) call changing values),
48//however, param_x and param_y will persist as they are thread local
49
50  printf("co_entrypoint(%d, %d)\n", param_x, param_y);
51  co_switch(thread[0]);
52  throw;
53}
54
55int main() {
56  printf("cothread parameterized function example\n\n");
57
58  thread[0] = co_active();
59  thread[1] = co_create(65536, co_entrypoint);
60  thread[2] = co_create(65536, co_entrypoint);
61
62//use specialized co_switch(cothread_t, int, int) for initial co_switch call
63  co_switch(thread[1], 1, 2);
64  co_switch(thread[2], 4, 8);
65
66//after first call, entry point arguments have been initialized, standard
67//co_switch(cothread_t) can be used from now on
68  co_switch(thread[2]);
69  co_switch(thread[1]);
70
71  printf("\ndone\n");
72#if defined(_MSC_VER) || defined(__DJGPP__)
73  getch();
74#endif
75  return 0;
76}
src/lib/cothread/test/cc.sh
r12101r12102
1clear
2g++ -O3 -fomit-frame-pointer -c test_timing.cpp
3gcc -O3 -fomit-frame-pointer -o libco.o -c ../libco.c
4g++ -O3 -fomit-frame-pointer test_timing.o libco.o -o test_timing
5rm -f *.o
1clear
2g++ -O3 -fomit-frame-pointer -c test_timing.cpp
3gcc -O3 -fomit-frame-pointer -o libco.o -c ../libco.c
4g++ -O3 -fomit-frame-pointer test_timing.o libco.o -o test_timing
5rm -f *.o
src/lib/cothread/doc/x86.asm
r12101r12102
1; fastcall ABI:
2; return = eax
3; arguments = ecx,edx
4; non-volatile registers = ebp,esi,edi,ebx
5 
6co_swap_fastcall(to = ecx, from = edx):
7  mov [edx],esp
8  mov esp,[ecx]
9  pop eax
10 
11  mov [edx+ 4],ebp
12  mov [edx+ 8],esi
13  mov [edx+12],edi
14  mov [edx+16],ebx
15 
16  mov ebp,[ecx+ 4]
17  mov esi,[ecx+ 8]
18  mov edi,[ecx+12]
19  mov ebx,[ecx+16]
20 
21  jmp eax
1; fastcall ABI:
2; return = eax
3; arguments = ecx,edx
4; non-volatile registers = ebp,esi,edi,ebx
5
6co_swap_fastcall(to = ecx, from = edx):
7  mov [edx],esp
8  mov esp,[ecx]
9  pop eax
10
11  mov [edx+ 4],ebp
12  mov [edx+ 8],esi
13  mov [edx+12],edi
14  mov [edx+16],ebx
15
16  mov ebp,[ecx+ 4]
17  mov esi,[ecx+ 8]
18  mov edi,[ecx+12]
19  mov ebx,[ecx+16]
20
21  jmp eax
src/lib/cothread/doc/style.css
r12101r12102
1body {
2  background: #333;
3  color: #fff;
4}
5 
6code {
7  background: #444;
8}
1body {
2  background: #333;
3  color: #fff;
4}
5
6code {
7  background: #444;
8}
src/lib/cothread/doc/usage.html
r12101r12102
1<html>
2<head>
3  <title></title>
4  <link href="style.css" rel="stylesheet" type="text/css">
5</head>
6<body>
7 
8<b>License:</b><br/><br/>
9libco is released to the public domain.
10<hr/>
11 
12<b>Contact:</b><br/><br/>
13At present, you may contact me at setsunakun0 at hotmail dot com.<br/>
14I am interested in knowing of any projects that make use of this library,
15though this is only a courtesy.
16<hr/>
17 
18<b>Foreword:</b><br/><br/>
19libco is a cross-platform, public domain implementation of
20cooperative-multithreading; a feature that is sorely lacking
21from the ISO C/C++ standard.<br/>
22The library is designed for maximum speed and portability, and
23not for safety or features. If safety or extra functionality is desired,
24a wrapper API can easily be written to encapsulate all library functions.<br/>
25Behavior of executing operations that are listed as not permitted
26below result in undefined behavior. They may work anyway, they
27may cause undesired / unknown behavior, or they may crash the
28program entirely.<br/>
29The goal of this library was to simplify the base API as much as possible,
30implementing only that which cannot be implemented using pure C. Additional
31functionality after this would only complicate ports of this library to new
32platforms.
33<hr/>
34 
35<b>Porting:</b><br/><br/>
36This document is included as a reference for porting libco. Please submit any
37ports you create to me, so that libco can become more useful. Please note that
38since libco is public domain, you must submit your code as a work of the
39public domain in order for it to be included in the official distribution.
40Full credit will be given in the source code of the official release. Please
41do not bother submitting code to me under any other license -- including GPL,
42LGPL, BSD or CC -- I am not interested in creating a library with multiple
43different licenses depending on which targets are used.
44<hr/>
45 
46<b>Synopsis:</b><br/><br/>
47<code>
48typedef void* cothread_t;<br/>
49<br/>
50cothread_t co_active();<br/>
51cothread_t co_create(unsigned int heapsize, void (*coentry)(void));<br/>
52void &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;co_delete(cothread_t cothread);<br/>
53void &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;co_switch(cothread_t cothread);<br/>
54</code>
55<hr/>
56 
57<b>Usage:</b>
58<hr/>
59 
60<code>typedef void* cothread_t;</code><br/><br/>
61Handle to cothread.<br/>
62Handle must be of type void*.<br/>
63A value of null (0) indicates an uninitialized or invalid
64handle, whereas a non-zero value indicates a valid handle.
65<hr/>
66 
67<code>cothread_t co_active();</code><br/><br/>
68Return handle to current cothread. Always returns a valid handle, even when
69called from the main program thread.
70<hr/>
71 
72<code>cothread_t co_create(unsigned int heapsize, void (*coentry)(void));</code><br/><br/>
73Create new cothread.<br/>
74Heapsize is the amount of memory allocated for the cothread stack, specified
75in bytes. This is unfortunately impossible to make fully portable. It is
76recommended to specify sizes using `n * sizeof(void*)'. It is better to err
77on the side of caution and allocate more memory than will be needed to ensure
78compatibility with other platforms, within reason. A typical heapsize for a
7932-bit architecture is ~1MB.<br/>
80When the new cothread is first called, program execution jumps to coentry.
81This function does not take any arguments, due to portability issues with
82passing function arguments. However, arguments can be simulated by the use
83of global variables, which can be set before the first call to each cothread.<br/>
84coentry() must not return, and should end with an appropriate co_switch()
85statement. Behavior is undefined if entry point returns normally.<br/>
86Library is responsible for allocating cothread stack memory, to free
87the user from needing to allocate special memory capable of being used
88as program stack memory on platforms where this is required.<br/>
89User is always responsible for deleting cothreads with co_delete().<br/>
90Return value of null (0) indicates cothread creation failed.
91<hr/>
92 
93<code>void co_delete(cothread_t cothread);</code><br/><br/>
94Delete specified cothread.<br/>
95Null (0) or invalid cothread handle is not allowed.<br/>
96Passing handle of active cothread to this function is not allowed.<br/>
97Passing handle of primary cothread is not allowed.
98<hr/>
99 
100<code>void co_switch(cothread_t cothread);</code><br/><br/>
101Switch to specified cothread.<br/>
102Null (0) or invalid cothread handle is not allowed.<br/>
103Passing handle of active cothread to this function is not allowed.
104<hr/>
105 
106</body>
107</html>
1<html>
2<head>
3  <title></title>
4  <link href="style.css" rel="stylesheet" type="text/css">
5</head>
6<body>
7
8<b>License:</b><br/><br/>
9libco is released to the public domain.
10<hr/>
11
12<b>Contact:</b><br/><br/>
13At present, you may contact me at setsunakun0 at hotmail dot com.<br/>
14I am interested in knowing of any projects that make use of this library,
15though this is only a courtesy.
16<hr/>
17
18<b>Foreword:</b><br/><br/>
19libco is a cross-platform, public domain implementation of
20cooperative-multithreading; a feature that is sorely lacking
21from the ISO C/C++ standard.<br/>
22The library is designed for maximum speed and portability, and
23not for safety or features. If safety or extra functionality is desired,
24a wrapper API can easily be written to encapsulate all library functions.<br/>
25Behavior of executing operations that are listed as not permitted
26below result in undefined behavior. They may work anyway, they
27may cause undesired / unknown behavior, or they may crash the
28program entirely.<br/>
29The goal of this library was to simplify the base API as much as possible,
30implementing only that which cannot be implemented using pure C. Additional
31functionality after this would only complicate ports of this library to new
32platforms.
33<hr/>
34
35<b>Porting:</b><br/><br/>
36This document is included as a reference for porting libco. Please submit any
37ports you create to me, so that libco can become more useful. Please note that
38since libco is public domain, you must submit your code as a work of the
39public domain in order for it to be included in the official distribution.
40Full credit will be given in the source code of the official release. Please
41do not bother submitting code to me under any other license -- including GPL,
42LGPL, BSD or CC -- I am not interested in creating a library with multiple
43different licenses depending on which targets are used.
44<hr/>
45
46<b>Synopsis:</b><br/><br/>
47<code>
48typedef void* cothread_t;<br/>
49<br/>
50cothread_t co_active();<br/>
51cothread_t co_create(unsigned int heapsize, void (*coentry)(void));<br/>
52void &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;co_delete(cothread_t cothread);<br/>
53void &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;co_switch(cothread_t cothread);<br/>
54</code>
55<hr/>
56
57<b>Usage:</b>
58<hr/>
59
60<code>typedef void* cothread_t;</code><br/><br/>
61Handle to cothread.<br/>
62Handle must be of type void*.<br/>
63A value of null (0) indicates an uninitialized or invalid
64handle, whereas a non-zero value indicates a valid handle.
65<hr/>
66
67<code>cothread_t co_active();</code><br/><br/>
68Return handle to current cothread. Always returns a valid handle, even when
69called from the main program thread.
70<hr/>
71
72<code>cothread_t co_create(unsigned int heapsize, void (*coentry)(void));</code><br/><br/>
73Create new cothread.<br/>
74Heapsize is the amount of memory allocated for the cothread stack, specified
75in bytes. This is unfortunately impossible to make fully portable. It is
76recommended to specify sizes using `n * sizeof(void*)'. It is better to err
77on the side of caution and allocate more memory than will be needed to ensure
78compatibility with other platforms, within reason. A typical heapsize for a
7932-bit architecture is ~1MB.<br/>
80When the new cothread is first called, program execution jumps to coentry.
81This function does not take any arguments, due to portability issues with
82passing function arguments. However, arguments can be simulated by the use
83of global variables, which can be set before the first call to each cothread.<br/>
84coentry() must not return, and should end with an appropriate co_switch()
85statement. Behavior is undefined if entry point returns normally.<br/>
86Library is responsible for allocating cothread stack memory, to free
87the user from needing to allocate special memory capable of being used
88as program stack memory on platforms where this is required.<br/>
89User is always responsible for deleting cothreads with co_delete().<br/>
90Return value of null (0) indicates cothread creation failed.
91<hr/>
92
93<code>void co_delete(cothread_t cothread);</code><br/><br/>
94Delete specified cothread.<br/>
95Null (0) or invalid cothread handle is not allowed.<br/>
96Passing handle of active cothread to this function is not allowed.<br/>
97Passing handle of primary cothread is not allowed.
98<hr/>
99
100<code>void co_switch(cothread_t cothread);</code><br/><br/>
101Switch to specified cothread.<br/>
102Null (0) or invalid cothread handle is not allowed.<br/>
103Passing handle of active cothread to this function is not allowed.
104<hr/>
105
106</body>
107</html>
src/lib/cothread/doc/amd64.asm
r12101r12102
1; SystemV ABI:
2; return = rax
3; arguments = rdi,rsi,rdx,rcx,r8,r9
4; non-volatile registers = rbp,rbx,r12-r15
5 
6co_swap_systemV(to = rdi, from = rsi):
7  mov [rsi],rsp
8  mov rsp,[rdi]
9  pop rax
10 
11  mov [rsi+ 8],rbp
12  mov [rsi+16],rbx
13  mov [rsi+24],r12
14  mov [rsi+32],r13
15  mov [rsi+40],r14
16  mov [rsi+48],r15
17 
18  mov rbp,[rdi+ 8]
19  mov rbx,[rdi+16]
20  mov r12,[rdi+24]
21  mov r13,[rdi+32]
22  mov r14,[rdi+40]
23  mov r15,[rdi+48]
24 
25  jmp rax
26 
27; Win64 ABI:
28; return = rax
29; arguments = rcx,rdx,r8,r9
30; non-volatile registers = rbp,rsi,rdi,rbx,r12-r15
31; non-volatile FPU registers = xmm6-xmm15
32 
33co_swap_win64(to = rcx, from = rdx):
34  mov [rdx],rsp
35  mov rsp,[rcx]
36  pop rax
37 
38  mov [rdx+  8],rbp
39  mov [rdx+ 16],rsi
40  mov [rdx+ 24],rdi
41  mov [rdx+ 32],rbx
42  mov [rdx+ 40],r12
43  mov [rdx+ 48],r13
44  mov [rdx+ 56],r14
45  mov [rdx+ 64],r15
46 
47  add rdx,128
48  and rdx,~15
49 
50  movaps [rdx+  0],xmm6
51  movaps [rdx+ 16],xmm7
52  movaps [rdx+ 32],xmm8
53  movaps [rdx+ 48],xmm9
54  movaps [rdx+ 64],xmm10
55  movaps [rdx+ 80],xmm11
56  movaps [rdx+ 96],xmm12
57  movaps [rdx+112],xmm13
58  movaps [rdx+128],xmm14
59  movaps [rdx+144],xmm15
60 
61  mov rbp,[rcx+  8]
62  mov rsi,[rcx+ 16]
63  mov rdi,[rcx+ 24]
64  mov rbx,[rcx+ 32]
65  mov r12,[rcx+ 40]
66  mov r13,[rcx+ 48]
67  mov r14,[rcx+ 56]
68  mov r15,[rcx+ 64]
69 
70  add rcx,128
71  and rcx,~15
72 
73  movaps [rcx+  0],xmm6
74  movaps [rcx+ 16],xmm7
75  movaps [rcx+ 32],xmm8
76  movaps [rcx+ 48],xmm9
77  movaps [rcx+ 64],xmm10
78  movaps [rcx+ 80],xmm11
79  movaps [rcx+ 96],xmm12
80  movaps [rcx+112],xmm13
81  movaps [rcx+128],xmm14
82  movaps [rcx+144],xmm15
83 
84  jmp rax
85 
1; SystemV ABI:
2; return = rax
3; arguments = rdi,rsi,rdx,rcx,r8,r9
4; non-volatile registers = rbp,rbx,r12-r15
5
6co_swap_systemV(to = rdi, from = rsi):
7  mov [rsi],rsp
8  mov rsp,[rdi]
9  pop rax
10
11  mov [rsi+ 8],rbp
12  mov [rsi+16],rbx
13  mov [rsi+24],r12
14  mov [rsi+32],r13
15  mov [rsi+40],r14
16  mov [rsi+48],r15
17
18  mov rbp,[rdi+ 8]
19  mov rbx,[rdi+16]
20  mov r12,[rdi+24]
21  mov r13,[rdi+32]
22  mov r14,[rdi+40]
23  mov r15,[rdi+48]
24
25  jmp rax
26
27; Win64 ABI:
28; return = rax
29; arguments = rcx,rdx,r8,r9
30; non-volatile registers = rbp,rsi,rdi,rbx,r12-r15
31; non-volatile FPU registers = xmm6-xmm15
32
33co_swap_win64(to = rcx, from = rdx):
34  mov [rdx],rsp
35  mov rsp,[rcx]
36  pop rax
37
38  mov [rdx+  8],rbp
39  mov [rdx+ 16],rsi
40  mov [rdx+ 24],rdi
41  mov [rdx+ 32],rbx
42  mov [rdx+ 40],r12
43  mov [rdx+ 48],r13
44  mov [rdx+ 56],r14
45  mov [rdx+ 64],r15
46
47  add rdx,128
48  and rdx,~15
49
50  movaps [rdx+  0],xmm6
51  movaps [rdx+ 16],xmm7
52  movaps [rdx+ 32],xmm8
53  movaps [rdx+ 48],xmm9
54  movaps [rdx+ 64],xmm10
55  movaps [rdx+ 80],xmm11
56  movaps [rdx+ 96],xmm12
57  movaps [rdx+112],xmm13
58  movaps [rdx+128],xmm14
59  movaps [rdx+144],xmm15
60
61  mov rbp,[rcx+  8]
62  mov rsi,[rcx+ 16]
63  mov rdi,[rcx+ 24]
64  mov rbx,[rcx+ 32]
65  mov r12,[rcx+ 40]
66  mov r13,[rcx+ 48]
67  mov r14,[rcx+ 56]
68  mov r15,[rcx+ 64]
69
70  add rcx,128
71  and rcx,~15
72
73  movaps [rcx+  0],xmm6
74  movaps [rcx+ 16],xmm7
75  movaps [rcx+ 32],xmm8
76  movaps [rcx+ 48],xmm9
77  movaps [rcx+ 64],xmm10
78  movaps [rcx+ 80],xmm11
79  movaps [rcx+ 96],xmm12
80  movaps [rcx+112],xmm13
81  movaps [rcx+128],xmm14
82  movaps [rcx+144],xmm15
83
84  jmp rax
85
src/lib/cothread/doc/targets.html
r12101r12102
1<html>
2<head>
3  <title></title>
4  <link href="style.css" rel="stylesheet" type="text/css">
5</head>
6<body>
7 
8<b>Supported targets:</b><br/><br/>
9 
10Note that supported targets are only those that have been tested and confirmed
11working. It is quite possible that libco will work on more processors, compilers
12and operating systems than those listed below.
13<hr/>
14 
15<b>libco.x86</b><br/>
16Overhead: ~5x<br/>
17Supported processor(s): 32-bit x86<br/>
18Supported compiler(s): any<br/>
19Supported operating system(s):<ul>
20<li>Windows</li>
21<li>Mac OS X</li>
22<li>Linux</li>
23<li>BSD</li>
24</ul>
25<hr/>
26 
27<b>libco.amd64</b><br/>
28Overhead: ~10x (Windows), ~6x (all other platforms)<br/>
29Supported processor(s): 64-bit amd64<br/>
30Supported compiler(s): any<br/>
31Supported operating system(s):<ul>
32<li>Windows</li>
33<li>Mac OS X</li>
34<li>Linux</li>
35<li>BSD</li>
36</ul>
37<hr/>
38 
39<b>libco.ppc</b><br/>
40Overhead: ~20x<br/>
41Supported processor(s): 32-bit PowerPC, 64-bit PowerPC<br/>
42Supported compiler(s): GNU GCC<br/>
43Supported operating system(s):<ul>
44</ul>
45<li>Mac OS X</li>
46<li>Linux</li>
47<li>BSD</li>
48<li>Playstation 3</li>
49</ul>
50<br/>
51 
52Note: this module contains compiler flags to enable/disable FPU and Altivec
53support.
54 
55<hr/>
56 
57<b>libco.fiber</b><br/>
58Overhead: ~15x<br/>
59Supported processor(s): Processor independent<br/>
60Supported compiler(s): any<br/>
61Supported operating system(s):<ul>
62<li>Windows</li>
63</ul>
64<hr/>
65 
66<b>libco.sjlj</b><br/>
67Overhead: ~30x<br/>
68Supported processor(s): Processor independent<br/>
69Supported compiler(s): any<br/>
70Supported operating system(s):<ul>
71<li>Mac OS X</li>
72<li>Linux</li>
73<li>BSD</li>
74<li>Solaris</li>
75</ul>
76<hr/>
77 
78<b>libco.ucontext</b><br/>
79Overhead: <b><font color="#ff0000">~300x</font></b><br/>
80Supported processor(s): Processor independent<br/>
81Supported compiler(s): any<br/>
82Supported operating system(s):<ul>
83<li>Linux</li>
84<li>BSD</li>
85</ul>
86<hr/>
87 
88</body>
89</html>
1<html>
2<head>
3  <title></title>
4  <link href="style.css" rel="stylesheet" type="text/css">
5</head>
6<body>
7
8<b>Supported targets:</b><br/><br/>
9
10Note that supported targets are only those that have been tested and confirmed
11working. It is quite possible that libco will work on more processors, compilers
12and operating systems than those listed below.
13<hr/>
14
15<b>libco.x86</b><br/>
16Overhead: ~5x<br/>
17Supported processor(s): 32-bit x86<br/>
18Supported compiler(s): any<br/>
19Supported operating system(s):<ul>
20<li>Windows</li>
21<li>Mac OS X</li>
22<li>Linux</li>
23<li>BSD</li>
24</ul>
25<hr/>
26
27<b>libco.amd64</b><br/>
28Overhead: ~10x (Windows), ~6x (all other platforms)<br/>
29Supported processor(s): 64-bit amd64<br/>
30Supported compiler(s): any<br/>
31Supported operating system(s):<ul>
32<li>Windows</li>
33<li>Mac OS X</li>
34<li>Linux</li>
35<li>BSD</li>
36</ul>
37<hr/>
38
39<b>libco.ppc</b><br/>
40Overhead: ~20x<br/>
41Supported processor(s): 32-bit PowerPC, 64-bit PowerPC<br/>
42Supported compiler(s): GNU GCC<br/>
43Supported operating system(s):<ul>
44</ul>
45<li>Mac OS X</li>
46<li>Linux</li>
47<li>BSD</li>
48<li>Playstation 3</li>
49</ul>
50<br/>
51
52Note: this module contains compiler flags to enable/disable FPU and Altivec
53support.
54
55<hr/>
56
57<b>libco.fiber</b><br/>
58Overhead: ~15x<br/>
59Supported processor(s): Processor independent<br/>
60Supported compiler(s): any<br/>
61Supported operating system(s):<ul>
62<li>Windows</li>
63</ul>
64<hr/>
65
66<b>libco.sjlj</b><br/>
67Overhead: ~30x<br/>
68Supported processor(s): Processor independent<br/>
69Supported compiler(s): any<br/>
70Supported operating system(s):<ul>
71<li>Mac OS X</li>
72<li>Linux</li>
73<li>BSD</li>
74<li>Solaris</li>
75</ul>
76<hr/>
77
78<b>libco.ucontext</b><br/>
79Overhead: <b><font color="#ff0000">~300x</font></b><br/>
80Supported processor(s): Processor independent<br/>
81Supported compiler(s): any<br/>
82Supported operating system(s):<ul>
83<li>Linux</li>
84<li>BSD</li>
85</ul>
86<hr/>
87
88</body>
89</html>
src/mess/machine/pce220_ser.c
r12101r12102
1/****************************************************************************
2 
3    pce220_ser.c
4 
5    Sharp PC-E220/PC-G850V Serial I/O
6 
7****************************************************************************/
8 
9#include "emu.h"
10#include "pce220_ser.h"
11 
12// SIO configuration
13#define SIO_BAUD_RATE      1200
14#define SIO_EOT_BYTE      0x1a
15#define SIO_DATA_BIT      8      // number of data bit
16#define SIO_PARITY         0      // 0=none 1=odd 2=even
17#define SIO_STOP_BIT      1      // number of stop bit
18 
19//SIO states
20enum
21{
22   SIO_WAIT = 0,
23   SIO_SEND_START,
24   SIO_SEND_BIT0,
25   SIO_SEND_BIT1,
26   SIO_SEND_BIT2,
27   SIO_SEND_BIT3,
28   SIO_SEND_BIT4,
29   SIO_SEND_BIT5,
30   SIO_SEND_BIT6,
31   SIO_SEND_BIT7,
32   SIO_SEND_PARITY,
33   SIO_SEND_STOP1,
34   SIO_SEND_STOP2
35};
36 
37// device type definition
38const device_type PCE220SERIAL = &device_creator<pce220_serial_device>;
39 
40//-------------------------------------------------
41//  pce220_serial_device - constructor
42//-------------------------------------------------
43 
44pce220_serial_device::pce220_serial_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
45    : device_t(mconfig, PCE220SERIAL, "Sharp PC-E220 serial", tag, owner, clock),
46     device_image_interface(mconfig, *this)
47{
48}
49 
50//-------------------------------------------------
51//  pce220_serial_device - destructor
52//-------------------------------------------------
53 
54pce220_serial_device::~pce220_serial_device()
55{
56}
57 
58 
59//-------------------------------------------------
60//  device_start - device-specific startup
61//-------------------------------------------------
62 
63void pce220_serial_device::device_start()
64{
65   m_send_timer = timer_alloc(TIMER_SEND);
66   m_receive_timer = timer_alloc(TIMER_RECEIVE);
67   m_send_timer->reset();
68   m_receive_timer->reset();
69}
70 
71 
72//-------------------------------------------------
73//  device_reset - reset up the device
74//-------------------------------------------------
75 
76void pce220_serial_device::device_reset()
77{
78   m_state = SIO_WAIT;
79   m_bytes_count = 0;
80   m_dout = m_busy = m_xout = 0;
81   m_din = m_xin = m_ack = 0;
82}
83 
84 
85//-------------------------------------------------
86//  device_config_complete - perform any
87//  operations now that the configuration is
88//  complete
89//-------------------------------------------------
90 
91void pce220_serial_device::device_config_complete()
92{
93   // set brief and instance name
94   update_names();
95}
96 
97 
98//-------------------------------------------------
99//  device_timer - handler timer events
100//-------------------------------------------------
101 
102void pce220_serial_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
103{
104   if (id == TIMER_SEND && m_enabled)
105   {
106      //send data
107      if (m_bytes_count <= length())
108      {
109         switch(m_state)
110         {
111         case SIO_WAIT:
112            m_ack = 1;   //data send request
113            //waits until is ready to receive
114            if(!m_busy)
115               return;
116            break;
117         case SIO_SEND_START:
118            //send start bit
119            m_xin = 1;
120            break;
121         case SIO_SEND_BIT0:
122         case SIO_SEND_BIT1:
123         case SIO_SEND_BIT2:
124         case SIO_SEND_BIT3:
125         case SIO_SEND_BIT4:
126         case SIO_SEND_BIT5:
127         case SIO_SEND_BIT6:
128         case SIO_SEND_BIT7:
129            m_xin = BIT(~m_current_byte, m_state - SIO_SEND_BIT0);
130            break;
131         case SIO_SEND_PARITY:
132            m_xin = calc_parity(m_current_byte);
133            break;
134         case SIO_SEND_STOP1:
135         case SIO_SEND_STOP2:
136            //stop bit
137            m_xin = m_ack = 0;
138            break;
139         }
140 
141         if (m_state == (SIO_SEND_PARITY + SIO_STOP_BIT))
142         {
143            //next byte
144            m_bytes_count++;
145            popmessage("Send %d/%d bytes\n", m_bytes_count , (UINT32)length());
146            m_state = SIO_WAIT;
147            if (m_bytes_count < length())
148               fread(&m_current_byte, 1);
149            else
150               m_current_byte = SIO_EOT_BYTE;
151         }
152         else
153         {
154            m_state = get_next_state();
155         }
156 
157      }
158      else
159      {
160         m_din = m_xin = m_ack = 0;
161      }
162   }
163   else if (id == TIMER_RECEIVE && m_enabled)
164   {
165      //receive data
166      switch(m_state)
167      {
168      case SIO_WAIT:
169         //wait send request
170         if(m_busy)
171            return;
172         m_ack = 1; //acknowledge
173         break;
174      case SIO_SEND_START:
175         //wait for the start bit
176         if (!m_xout)
177            return;
178         break;
179      case SIO_SEND_BIT0:
180      case SIO_SEND_BIT1:
181      case SIO_SEND_BIT2:
182      case SIO_SEND_BIT3:
183      case SIO_SEND_BIT4:
184      case SIO_SEND_BIT5:
185      case SIO_SEND_BIT6:
186      case SIO_SEND_BIT7:
187         m_current_byte |= ((~m_xout)&1) << (m_state - SIO_SEND_BIT0);
188         break;
189      case SIO_SEND_PARITY:
190         if (m_xout != calc_parity(m_current_byte))
191            logerror("SIO %s: byte %d has wrong parity!\n", tag(), m_bytes_count);
192         break;
193      case SIO_SEND_STOP1:
194      case SIO_SEND_STOP2:
195         //receive stop bit
196         m_ack = 0;
197         break;
198      }
199 
200      if (m_state == (SIO_SEND_PARITY + SIO_STOP_BIT))
201      {
202         //next byte
203         m_bytes_count++;
204         popmessage("Received %d bytes\n", m_bytes_count);
205         m_state = SIO_WAIT;
206         if (m_current_byte != SIO_EOT_BYTE)
207            fwrite(&m_current_byte, 1);
208         m_current_byte = 0;
209      }
210      else
211      {
212         m_state = get_next_state();
213      }
214   }
215   else
216   {
217      m_din = m_xin = m_ack = 0;
218   }
219}
220 
221/*-------------------------------------------------
222    DEVICE_IMAGE_LOAD( pce220_serial )
223-------------------------------------------------*/
224 
225bool pce220_serial_device::call_load()
226{
227   m_state = SIO_WAIT;
228   m_bytes_count = 0;
229   m_send_timer->adjust(attotime::from_hz(SIO_BAUD_RATE), 0, attotime::from_hz(SIO_BAUD_RATE));
230 
231   //read the first byte
232   fread(&m_current_byte, 1);
233 
234   return IMAGE_INIT_PASS;
235}
236 
237/*-------------------------------------------------
238    DEVICE_IMAGE_CREATE( pce220_serial )
239-------------------------------------------------*/
240 
241bool pce220_serial_device::call_create(int format_type, option_resolution *format_options)
242{
243   m_state = SIO_WAIT;
244   m_bytes_count = 0;
245   m_current_byte = 0;
246   m_receive_timer->adjust(attotime::from_hz(SIO_BAUD_RATE), 0, attotime::from_hz(SIO_BAUD_RATE));
247 
248   return IMAGE_INIT_PASS;
249}
250 
251 
252/*-------------------------------------------------
253    DEVICE_IMAGE_UNLOAD( pce220_serial )
254-------------------------------------------------*/
255 
256void pce220_serial_device::call_unload()
257{
258   m_send_timer->reset();
259   m_receive_timer->reset();
260   m_state = SIO_WAIT;
261   m_bytes_count = 0;
262   m_current_byte = 0;
263}
264 
265 
266/*-------------------------------------------------
267    calculate the byte parity
268-------------------------------------------------*/
269 
270int pce220_serial_device::calc_parity(UINT8 data)
271{
272   UINT8 count = 0;
273 
274   while(data != 0)
275   {
276      count++;
277      data &= (data-1);
278   }
279 
280   return (count ^ BIT(SIO_PARITY, 1)) & 1;
281}
282 
283 
284/*-------------------------------------------------
285    returns the next SIO state basing on the
286    configuration
287-------------------------------------------------*/
288 
289int pce220_serial_device::get_next_state()
290{
291   switch(m_state)
292   {
293   case SIO_SEND_BIT6:
294      if (SIO_DATA_BIT == 8)
295         return SIO_SEND_BIT7;
296   //fall through
297   case SIO_SEND_BIT7:
298      return (SIO_PARITY ? SIO_SEND_PARITY : SIO_SEND_STOP1);
299   case SIO_SEND_STOP1:
300   case SIO_SEND_STOP2:
301      return (SIO_STOP_BIT == 2 ? SIO_SEND_STOP2 : SIO_SEND_STOP1);
302   default:
303      return m_state + 1;
304   }
305}
306 
1/****************************************************************************
2
3    pce220_ser.c
4
5    Sharp PC-E220/PC-G850V Serial I/O
6
7****************************************************************************/
8
9#include "emu.h"
10#include "pce220_ser.h"
11
12// SIO configuration
13#define SIO_BAUD_RATE      1200
14#define SIO_EOT_BYTE      0x1a
15#define SIO_DATA_BIT      8      // number of data bit
16#define SIO_PARITY         0      // 0=none 1=odd 2=even
17#define SIO_STOP_BIT      1      // number of stop bit
18
19//SIO states
20enum
21{
22   SIO_WAIT = 0,
23   SIO_SEND_START,
24   SIO_SEND_BIT0,
25   SIO_SEND_BIT1,
26   SIO_SEND_BIT2,
27   SIO_SEND_BIT3,
28   SIO_SEND_BIT4,
29   SIO_SEND_BIT5,
30   SIO_SEND_BIT6,
31   SIO_SEND_BIT7,
32   SIO_SEND_PARITY,
33   SIO_SEND_STOP1,
34   SIO_SEND_STOP2
35};
36
37// device type definition
38const device_type PCE220SERIAL = &device_creator<pce220_serial_device>;
39
40//-------------------------------------------------
41//  pce220_serial_device - constructor
42//-------------------------------------------------
43
44pce220_serial_device::pce220_serial_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
45    : device_t(mconfig, PCE220SERIAL, "Sharp PC-E220 serial", tag, owner, clock),
46     device_image_interface(mconfig, *this)
47{
48}
49
50//-------------------------------------------------
51//  pce220_serial_device - destructor
52//-------------------------------------------------
53
54pce220_serial_device::~pce220_serial_device()
55{
56}
57
58
59//-------------------------------------------------
60//  device_start - device-specific startup
61//-------------------------------------------------
62
63void pce220_serial_device::device_start()
64{
65   m_send_timer = timer_alloc(TIMER_SEND);
66   m_receive_timer = timer_alloc(TIMER_RECEIVE);
67   m_send_timer->reset();
68   m_receive_timer->reset();
69}
70
71
72//-------------------------------------------------
73//  device_reset - reset up the device
74//-------------------------------------------------
75
76void pce220_serial_device::device_reset()
77{
78   m_state = SIO_WAIT;
79   m_bytes_count = 0;
80   m_dout = m_busy = m_xout = 0;
81   m_din = m_xin = m_ack = 0;
82}
83
84
85//-------------------------------------------------
86//  device_config_complete - perform any
87//  operations now that the configuration is
88//  complete
89//-------------------------------------------------
90
91void pce220_serial_device::device_config_complete()
92{
93   // set brief and instance name
94   update_names();
95}
96
97
98//-------------------------------------------------
99//  device_timer - handler timer events
100//-------------------------------------------------
101
102void pce220_serial_device::device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr)
103{
104   if (id == TIMER_SEND && m_enabled)
105   {
106      //send data
107      if (m_bytes_count <= length())
108      {
109         switch(m_state)
110         {
111         case SIO_WAIT:
112            m_ack = 1;   //data send request
113            //waits until is ready to receive
114            if(!m_busy)
115               return;
116            break;
117         case SIO_SEND_START:
118            //send start bit
119            m_xin = 1;
120            break;
121         case SIO_SEND_BIT0:
122         case SIO_SEND_BIT1:
123         case SIO_SEND_BIT2:
124         case SIO_SEND_BIT3:
125         case SIO_SEND_BIT4:
126         case SIO_SEND_BIT5:
127         case SIO_SEND_BIT6:
128         case SIO_SEND_BIT7:
129            m_xin = BIT(~m_current_byte, m_state - SIO_SEND_BIT0);
130            break;
131         case SIO_SEND_PARITY:
132            m_xin = calc_parity(m_current_byte);
133            break;
134         case SIO_SEND_STOP1:
135         case SIO_SEND_STOP2:
136            //stop bit
137            m_xin = m_ack = 0;
138            break;
139         }
140
141         if (m_state == (SIO_SEND_PARITY + SIO_STOP_BIT))
142         {
143            //next byte
144            m_bytes_count++;
145            popmessage("Send %d/%d bytes\n", m_bytes_count , (UINT32)length());
146            m_state = SIO_WAIT;
147            if (m_bytes_count < length())
148               fread(&m_current_byte, 1);
149            else
150               m_current_byte = SIO_EOT_BYTE;
151         }
152         else
153         {
154            m_state = get_next_state();
155         }
156
157      }
158      else
159      {
160         m_din = m_xin = m_ack = 0;
161      }
162   }
163   else if (id == TIMER_RECEIVE && m_enabled)
164   {
165      //receive data
166      switch(m_state)
167      {
168      case SIO_WAIT:
169         //wait send request
170         if(m_busy)
171            return;
172         m_ack = 1; //acknowledge
173         break;
174      case SIO_SEND_START:
175         //wait for the start bit
176         if (!m_xout)
177            return;
178         break;
179      case SIO_SEND_BIT0:
180      case SIO_SEND_BIT1:
181      case SIO_SEND_BIT2:
182      case SIO_SEND_BIT3:
183      case SIO_SEND_BIT4:
184      case SIO_SEND_BIT5:
185      case SIO_SEND_BIT6:
186      case SIO_SEND_BIT7:
187         m_current_byte |= ((~m_xout)&1) << (m_state - SIO_SEND_BIT0);
188         break;
189      case SIO_SEND_PARITY:
190         if (m_xout != calc_parity(m_current_byte))
191            logerror("SIO %s: byte %d has wrong parity!\n", tag(), m_bytes_count);
192         break;
193      case SIO_SEND_STOP1:
194      case SIO_SEND_STOP2:
195         //receive stop bit
196         m_ack = 0;
197         break;
198      }
199
200      if (m_state == (SIO_SEND_PARITY + SIO_STOP_BIT))
201      {
202         //next byte
203         m_bytes_count++;
204         popmessage("Received %d bytes\n", m_bytes_count);
205         m_state = SIO_WAIT;
206         if (m_current_byte != SIO_EOT_BYTE)
207            fwrite(&m_current_byte, 1);
208         m_current_byte = 0;
209      }
210      else
211      {
212         m_state = get_next_state();
213      }
214   }
215   else
216   {
217      m_din = m_xin = m_ack = 0;
218   }
219}
220
221/*-------------------------------------------------
222    DEVICE_IMAGE_LOAD( pce220_serial )
223-------------------------------------------------*/
224
225bool pce220_serial_device::call_load()
226{
227   m_state = SIO_WAIT;
228   m_bytes_count = 0;
229   m_send_timer->adjust(attotime::from_hz(SIO_BAUD_RATE), 0, attotime::from_hz(SIO_BAUD_RATE));
230
231   //read the first byte
232   fread(&m_current_byte, 1);
233
234   return IMAGE_INIT_PASS;
235}
236
237/*-------------------------------------------------
238    DEVICE_IMAGE_CREATE( pce220_serial )
239-------------------------------------------------*/
240
241bool pce220_serial_device::call_create(int format_type, option_resolution *format_options)
242{
243   m_state = SIO_WAIT;
244   m_bytes_count = 0;
245   m_current_byte = 0;
246   m_receive_timer->adjust(attotime::from_hz(SIO_BAUD_RATE), 0, attotime::from_hz(SIO_BAUD_RATE));
247
248   return IMAGE_INIT_PASS;
249}
250
251
252/*-------------------------------------------------
253    DEVICE_IMAGE_UNLOAD( pce220_serial )
254-------------------------------------------------*/
255
256void pce220_serial_device::call_unload()
257{
258   m_send_timer->reset();
259   m_receive_timer->reset();
260   m_state = SIO_WAIT;
261   m_bytes_count = 0;
262   m_current_byte = 0;
263}
264
265
266/*-------------------------------------------------
267    calculate the byte parity
268-------------------------------------------------*/
269
270int pce220_serial_device::calc_parity(UINT8 data)
271{
272   UINT8 count = 0;
273
274   while(data != 0)
275   {
276      count++;
277      data &= (data-1);
278   }
279
280   return (count ^ BIT(SIO_PARITY, 1)) & 1;
281}
282
283
284/*-------------------------------------------------
285    returns the next SIO state basing on the
286    configuration
287-------------------------------------------------*/
288
289int pce220_serial_device::get_next_state()
290{
291   switch(m_state)
292   {
293   case SIO_SEND_BIT6:
294      if (SIO_DATA_BIT == 8)
295         return SIO_SEND_BIT7;
296   //fall through
297   case SIO_SEND_BIT7:
298      return (SIO_PARITY ? SIO_SEND_PARITY : SIO_SEND_STOP1);
299   case SIO_SEND_STOP1:
300   case SIO_SEND_STOP2:
301      return (SIO_STOP_BIT == 2 ? SIO_SEND_STOP2 : SIO_SEND_STOP1);
302   default:
303      return m_state + 1;
304   }
305}
306
src/mess/machine/pce220_ser.h
r12101r12102
1/****************************************************************************
2 
3    pce220_ser.h
4 
5    Sharp PC-E220/PC-G850V Serial I/O
6 
7****************************************************************************/
8 
9#ifndef __PCE220_SER_H__
10#define __PCE220_SER_H__
11 
12 
13/***************************************************************************
14    TYPE DEFINITIONS
15***************************************************************************/
16 
17// ======================> pce220_serial_device
18 
19class pce220_serial_device :   public device_t,
20                        public device_image_interface
21{
22public:
23   // construction/destruction
24   pce220_serial_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
25   virtual ~pce220_serial_device();
26 
27   // image-level overrides
28   virtual bool call_load();
29   virtual void call_unload();
30   virtual bool call_create(int format_type, option_resolution *format_options);
31 
32   virtual iodevice_t image_type() const { return IO_SERIAL; }
33 
34   virtual bool is_readable()  const { return 1; }
35   virtual bool is_writeable() const { return 1; }
36   virtual bool is_creatable() const { return 1; }
37   virtual bool must_be_loaded() const { return 0; }
38   virtual bool is_reset_on_load() const { return 0; }
39   virtual const char *image_interface() const { return NULL; }
40   virtual const char *file_extensions() const { return "txt,ihx"; }
41   virtual const option_guide *create_option_guide() const { return NULL; }
42 
43   // specific implementation
44   UINT8 in_xin(void) { return m_xin & 0x01; }
45   UINT8 in_din(void) { return m_din & 0x01; }
46   UINT8 in_ack(void) { return m_ack & 0x01; }
47   void out_busy(UINT8 state)   { m_busy = state & 0x01; }
48   void out_dout(UINT8 state)   { m_dout = state & 0x01; }
49   void out_xout(UINT8 state)   { m_xout = state & 0x01; }
50   void enable_interface(UINT8 state)   { m_enabled = state & 0x01; }
51 
52protected:
53   // internal helpers
54   int calc_parity(UINT8 data);
55   int get_next_state();
56 
57   // device-level overrides
58   virtual void device_start();
59   virtual void device_reset();
60   virtual void device_config_complete();
61   virtual void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr);
62 
63private:
64    // internal device state
65   static const device_timer_id TIMER_SEND    = 0;
66   static const device_timer_id TIMER_RECEIVE = 1;
67 
68   emu_timer*   m_send_timer;      // timer for send data
69   emu_timer*   m_receive_timer;   // timer for receive data
70   UINT8      m_state;         // transfer status
71   UINT32      m_bytes_count;      // number of bytes transferred
72   UINT8      m_current_byte;      // byte in transfer
73   UINT8      m_enabled;         // enable/disable
74 
75   UINT8      m_busy;            // CTS
76   UINT8      m_dout;            // DTR
77   UINT8      m_xout;            // TXD
78   UINT8      m_xin;            // RXD
79   UINT8      m_din;            // DSR
80   UINT8      m_ack;            // RTS
81};
82 
83// device type definition
84extern const device_type PCE220SERIAL;
85 
86/***************************************************************************
87    DEVICE CONFIGURATION MACROS
88***************************************************************************/
89#define PCE220SERIAL_TAG      "serial"
90 
91#define MCFG_PCE220_SERIAL_ADD(_tag) \
92   MCFG_DEVICE_ADD(_tag, PCE220SERIAL, 0) \
93 
94#endif /* __PCE220_SER_H__ */
1/****************************************************************************
2
3    pce220_ser.h
4
5    Sharp PC-E220/PC-G850V Serial I/O
6
7****************************************************************************/
8
9#ifndef __PCE220_SER_H__
10#define __PCE220_SER_H__
11
12
13/***************************************************************************
14    TYPE DEFINITIONS
15***************************************************************************/
16
17// ======================> pce220_serial_device
18
19class pce220_serial_device :   public device_t,
20                        public device_image_interface
21{
22public:
23   // construction/destruction
24   pce220_serial_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
25   virtual ~pce220_serial_device();
26
27   // image-level overrides
28   virtual bool call_load();
29   virtual void call_unload();
30   virtual bool call_create(int format_type, option_resolution *format_options);
31
32   virtual iodevice_t image_type() const { return IO_SERIAL; }
33
34   virtual bool is_readable()  const { return 1; }
35   virtual bool is_writeable() const { return 1; }
36   virtual bool is_creatable() const { return 1; }
37   virtual bool must_be_loaded() const { return 0; }
38   virtual bool is_reset_on_load() const { return 0; }
39   virtual const char *image_interface() const { return NULL; }
40   virtual const char *file_extensions() const { return "txt,ihx"; }
41   virtual const option_guide *create_option_guide() const { return NULL; }
42
43   // specific implementation
44   UINT8 in_xin(void) { return m_xin & 0x01; }
45   UINT8 in_din(void) { return m_din & 0x01; }
46   UINT8 in_ack(void) { return m_ack & 0x01; }
47   void out_busy(UINT8 state)   { m_busy = state & 0x01; }
48   void out_dout(UINT8 state)   { m_dout = state & 0x01; }
49   void out_xout(UINT8 state)   { m_xout = state & 0x01; }
50   void enable_interface(UINT8 state)   { m_enabled = state & 0x01; }
51
52protected:
53   // internal helpers
54   int calc_parity(UINT8 data);
55   int get_next_state();
56
57   // device-level overrides
58   virtual void device_start();
59   virtual void device_reset();
60   virtual void device_config_complete();
61   virtual void device_timer(emu_timer &timer, device_timer_id id, int param, void *ptr);
62
63private:
64    // internal device state
65   static const device_timer_id TIMER_SEND    = 0;
66   static const device_timer_id TIMER_RECEIVE = 1;
67
68   emu_timer*   m_send_timer;      // timer for send data
69   emu_timer*   m_receive_timer;   // timer for receive data
70   UINT8      m_state;         // transfer status
71   UINT32      m_bytes_count;      // number of bytes transferred
72   UINT8      m_current_byte;      // byte in transfer
73   UINT8      m_enabled;         // enable/disable
74
75   UINT8      m_busy;            // CTS
76   UINT8      m_dout;            // DTR
77   UINT8      m_xout;            // TXD
78   UINT8      m_xin;            // RXD
79   UINT8      m_din;            // DSR
80   UINT8      m_ack;            // RTS
81};
82
83// device type definition
84extern const device_type PCE220SERIAL;
85
86/***************************************************************************
87    DEVICE CONFIGURATION MACROS
88***************************************************************************/
89#define PCE220SERIAL_TAG      "serial"
90
91#define MCFG_PCE220_SERIAL_ADD(_tag) \
92   MCFG_DEVICE_ADD(_tag, PCE220SERIAL, 0) \
93
94#endif /* __PCE220_SER_H__ */
src/mess/machine/psion_pack.c
r12101r12102
1/****************************************************************************
2 
3    psion_pack.c
4 
5    Psion Organiser II Datapack emulation
6 
7****************************************************************************/
8 
9#include "emu.h"
10#include "psion_pack.h"
11 
12// Datapack control lines
13#define DP_LINE_CLOCK         0x01
14#define DP_LINE_RESET         0x02
15#define DP_LINE_PROGRAM         0x04
16#define DP_LINE_OUTPUT_ENABLE   0x08
17#define DP_LINE_SLOT_SELECT      0x10
18 
19// Datapack ID
20#define DP_ID_EPROM            0x02
21#define DP_ID_PAGED            0x04
22#define DP_ID_WRITE            0x08
23#define DP_ID_BOOT            0x10
24#define DP_ID_COPY            0x20
25 
26#define OPK_HEAD_SIZE         6
27 
28 
29static OPTION_GUIDE_START( datapack_option_guide )
30   OPTION_INT('S', "size", "Datapack size" )
31   OPTION_INT('R', "ram", "RAM/EPROM" )
32   OPTION_INT('P', "paged", "Paged" )
33   OPTION_INT('W', "protect", "Write-protected" )
34   OPTION_INT('B', "boot", "Bootable" )
35   OPTION_INT('C', "copy", "Copyable" )
36OPTION_GUIDE_END
37 
38static const char *datapack_option_spec =
39   "S1/2/4/[8]/16;R0/[1];P[0]/1;W[0]/1;B[0]/1;C0/[1]";
40 
41 
42// device type definition
43const device_type PSION_DATAPACK = &device_creator<datapack_device>;
44 
45//-------------------------------------------------
46//  datapack_device - constructor
47//-------------------------------------------------
48 
49datapack_device::datapack_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
50    : device_t(mconfig, PSION_DATAPACK, "Psion Datapack", tag, owner, clock),
51     device_image_interface(mconfig, *this)
52{
53}
54 
55 
56//-------------------------------------------------
57//  datapack_device - destructor
58//-------------------------------------------------
59 
60datapack_device::~datapack_device()
61{
62}
63 
64 
65//-------------------------------------------------
66//  device_start - device-specific startup
67//-------------------------------------------------
68 
69void datapack_device::device_start()
70{
71   call_unload();
72}
73 
74 
75//-------------------------------------------------
76//  device_config_complete - perform any
77//  operations now that the configuration is
78//  complete
79//-------------------------------------------------
80 
81void datapack_device::device_config_complete()
82{
83   image_device_format *format = global_alloc_clear(image_device_format);
84 
85   format->m_index      = 0;
86   format->m_name        = "opk";
87   format->m_description = "Psion Datapack image";
88   format->m_extensions  = "opk";
89   format->m_optspec     = datapack_option_spec;
90   format->m_next        = NULL;
91 
92   m_formatlist = format;
93 
94   // set brief and instance name
95   update_names();
96}
97 
98 
99//-------------------------------------------------
100//  option_guide for create new image
101//-------------------------------------------------
102 
103const option_guide *datapack_device::create_option_guide() const
104{
105   return datapack_option_guide;
106}
107 
108 
109/*-------------------------------------------------
110    update the internal state
111-------------------------------------------------*/
112 
113void datapack_device::update()
114{
115   UINT32 pack_addr = m_counter + ((m_id & DP_ID_PAGED) ? (m_page << 8) : 0);
116 
117   // if the datapack is 128k or more is treated as segmented
118   if (m_size >= 0x10)
119      pack_addr += (m_segment << 14);
120 
121   if (pack_addr < (m_size << 13))
122   {
123      if ((m_control & DP_LINE_OUTPUT_ENABLE) && !(m_control & DP_LINE_RESET))
124      {
125         // write data
126         if (software_entry() == NULL && (m_id & DP_ID_WRITE))
127         {
128            fseek(pack_addr + OPK_HEAD_SIZE, SEEK_SET);
129            fwrite(&m_data, 1);
130         }
131      }
132      else if ((m_control & DP_LINE_OUTPUT_ENABLE) && (m_control & DP_LINE_RESET))
133      {
134         // write datapack segment
135         if (m_size <= 0x10)
136            m_segment = m_data & 0x07;
137         else if (m_size <= 0x20)
138            m_segment = m_data & 0x0f;
139         else if (m_size <= 0x40)
140            m_segment = m_data & 0x1f;
141         else if (m_size <= 0x80)
142            m_segment = m_data & 0x3f;
143         else
144            m_segment = m_data;
145      }
146      else if (!(m_control & DP_LINE_OUTPUT_ENABLE) && !(m_control & DP_LINE_RESET))
147      {
148         // read data
149         if ((pack_addr + OPK_HEAD_SIZE) < length())
150         {
151            fseek(pack_addr + OPK_HEAD_SIZE, SEEK_SET);
152            fread(&m_data, 1);
153         }
154         else
155         {
156            m_data = 0xff;
157         }
158      }
159      else if (!(m_control & DP_LINE_OUTPUT_ENABLE) && (m_control & DP_LINE_RESET))
160      {
161         // read datapack ID
162         if (m_id & DP_ID_EPROM)
163            m_data = m_id;
164         else
165            m_data = 0x01;      // for identify RAM pack
166      }
167   }
168}
169 
170 
171/*-------------------------------------------------
172    read datapack data lines
173-------------------------------------------------*/
174 
175UINT8 datapack_device::data_r()
176{
177   return (!(m_control & DP_LINE_SLOT_SELECT)) ? m_data : 0;
178}
179 
180 
181/*-------------------------------------------------
182    write datapack data lines
183-------------------------------------------------*/
184 
185void  datapack_device::data_w(UINT8 data)
186{
187   if (is_loaded())
188   {
189      m_data = data;
190 
191      // updates the internal state
192      if (!(data & DP_LINE_SLOT_SELECT))
193         update();
194   }
195}
196 
197 
198/*-------------------------------------------------
199    read datapack control lines
200-------------------------------------------------*/
201 
202UINT8 datapack_device::control_r()
203{
204   return m_control;
205}
206 
207 
208/*-------------------------------------------------
209    write datapack control lines
210-------------------------------------------------*/
211 
212void datapack_device::control_w(UINT8 data)
213{
214   if (is_loaded())
215   {
216      if ((m_control & DP_LINE_CLOCK) != (data & DP_LINE_CLOCK))
217      {
218         // increments the counter
219         m_counter++;
220 
221         if (m_counter >= ((m_id & DP_ID_PAGED) ? 0x100 : (m_size << 13)))
222            m_counter = 0;
223      }
224 
225      if ((m_control & DP_LINE_PROGRAM) && !(data & DP_LINE_PROGRAM))
226      {
227         // increments the page
228         m_page++;
229 
230         if (m_page >= (m_size << 5))
231            m_page = 0;
232      }
233 
234      if (data & DP_LINE_RESET)
235      {
236         // reset counter and page
237         m_counter = 0;
238         m_page = 0;
239      }
240 
241      m_control = data;
242 
243      // updates the internal state
244      if (!(data & DP_LINE_SLOT_SELECT))
245         update();
246   }
247}
248 
249 
250/*-------------------------------------------------
251    DEVICE_IMAGE_LOAD( datapack )
252-------------------------------------------------*/
253 
254bool datapack_device::call_load()
255{
256   UINT8 data[0x10];
257 
258   fread(data, 0x10);
259 
260   // check the OPK head
261   if(strncmp((const char*)data, "OPK", 3))
262      return IMAGE_INIT_FAIL;
263 
264   // get datapack ID and size
265   m_id   = data[OPK_HEAD_SIZE + 0];
266   m_size = data[OPK_HEAD_SIZE + 1];
267 
268   return IMAGE_INIT_PASS;
269}
270 
271 
272/*-------------------------------------------------
273    DEVICE_IMAGE_CREATE( datapack )
274-------------------------------------------------*/
275 
276bool datapack_device::call_create(int format_type, option_resolution *create_args)
277{
278   static const UINT8 opk_head[6] = {'O', 'P', 'K', 0x00, 0x00, 0x00};
279 
280   if (create_args != NULL)
281   {
282      m_id = 0x40;
283      m_id |= (option_resolution_lookup_int(create_args, 'R')) ? 0x00 : 0x02;
284      m_id |= (option_resolution_lookup_int(create_args, 'P')) ? 0x04 : 0x00;
285      m_id |= (option_resolution_lookup_int(create_args, 'W')) ? 0x00 : 0x08;
286      m_id |= (option_resolution_lookup_int(create_args, 'B')) ? 0x00 : 0x10;
287      m_id |= (option_resolution_lookup_int(create_args, 'C')) ? 0x20 : 0x00;
288      m_size = option_resolution_lookup_int(create_args, 'S');
289   }
290   else
291   {
292      // 64k RAM datapack by default
293      m_id = 0x7c;
294      m_size = 0x08;
295   }
296 
297   fwrite(opk_head, 6);
298   fwrite(&m_id, 1);
299   fwrite(&m_size, 1);
300 
301   return IMAGE_INIT_PASS;
302}
303 
304 
305/*-------------------------------------------------
306    DEVICE_IMAGE_UNLOAD( datapack )
307-------------------------------------------------*/
308 
309void datapack_device::call_unload()
310{
311   m_id      = 0;
312   m_size    = 0;
313   m_counter = 0;
314   m_page    = 0;
315   m_segment = 0;
316   m_control = 0;
317   m_data    = 0;
318}
1/****************************************************************************
2
3    psion_pack.c
4
5    Psion Organiser II Datapack emulation
6
7****************************************************************************/
8
9#include "emu.h"
10#include "psion_pack.h"
11
12// Datapack control lines
13#define DP_LINE_CLOCK         0x01
14#define DP_LINE_RESET         0x02
15#define DP_LINE_PROGRAM         0x04
16#define DP_LINE_OUTPUT_ENABLE   0x08
17#define DP_LINE_SLOT_SELECT      0x10
18
19// Datapack ID
20#define DP_ID_EPROM            0x02
21#define DP_ID_PAGED            0x04
22#define DP_ID_WRITE            0x08
23#define DP_ID_BOOT            0x10
24#define DP_ID_COPY            0x20
25
26#define OPK_HEAD_SIZE         6
27
28
29static OPTION_GUIDE_START( datapack_option_guide )
30   OPTION_INT('S', "size", "Datapack size" )
31   OPTION_INT('R', "ram", "RAM/EPROM" )
32   OPTION_INT('P', "paged", "Paged" )
33   OPTION_INT('W', "protect", "Write-protected" )
34   OPTION_INT('B', "boot", "Bootable" )
35   OPTION_INT('C', "copy", "Copyable" )
36OPTION_GUIDE_END
37
38static const char *datapack_option_spec =
39   "S1/2/4/[8]/16;R0/[1];P[0]/1;W[0]/1;B[0]/1;C0/[1]";
40
41
42// device type definition
43const device_type PSION_DATAPACK = &device_creator<datapack_device>;
44
45//-------------------------------------------------
46//  datapack_device - constructor
47//-------------------------------------------------
48
49datapack_device::datapack_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock)
50    : device_t(mconfig, PSION_DATAPACK, "Psion Datapack", tag, owner, clock),
51     device_image_interface(mconfig, *this)
52{
53}
54
55
56//-------------------------------------------------
57//  datapack_device - destructor
58//-------------------------------------------------
59
60datapack_device::~datapack_device()
61{
62}
63
64
65//-------------------------------------------------
66//  device_start - device-specific startup
67//-------------------------------------------------
68
69void datapack_device::device_start()
70{
71   call_unload();
72}
73
74
75//-------------------------------------------------
76//  device_config_complete - perform any
77//  operations now that the configuration is
78//  complete
79//-------------------------------------------------
80
81void datapack_device::device_config_complete()
82{
83   image_device_format *format = global_alloc_clear(image_device_format);
84
85   format->m_index      = 0;
86   format->m_name        = "opk";
87   format->m_description = "Psion Datapack image";
88   format->m_extensions  = "opk";
89   format->m_optspec     = datapack_option_spec;
90   format->m_next        = NULL;
91
92   m_formatlist = format;
93
94   // set brief and instance name
95   update_names();
96}
97
98
99//-------------------------------------------------
100//  option_guide for create new image
101//-------------------------------------------------
102
103const option_guide *datapack_device::create_option_guide() const
104{
105   return datapack_option_guide;
106}
107
108
109/*-------------------------------------------------
110    update the internal state
111-------------------------------------------------*/
112
113void datapack_device::update()
114{
115   UINT32 pack_addr = m_counter + ((m_id & DP_ID_PAGED) ? (m_page << 8) : 0);
116
117   // if the datapack is 128k or more is treated as segmented
118   if (m_size >= 0x10)
119      pack_addr += (m_segment << 14);
120
121   if (pack_addr < (m_size << 13))
122   {
123      if ((m_control & DP_LINE_OUTPUT_ENABLE) && !(m_control & DP_LINE_RESET))
124      {
125         // write data
126         if (software_entry() == NULL && (m_id & DP_ID_WRITE))
127         {
128            fseek(pack_addr + OPK_HEAD_SIZE, SEEK_SET);
129            fwrite(&m_data, 1);
130         }
131      }
132      else if ((m_control & DP_LINE_OUTPUT_ENABLE) && (m_control & DP_LINE_RESET))
133      {
134         // write datapack segment
135         if (m_size <= 0x10)
136            m_segment = m_data & 0x07;
137         else if (m_size <= 0x20)
138            m_segment = m_data & 0x0f;
139         else if (m_size <= 0x40)
140            m_segment = m_data & 0x1f;
141         else if (m_size <= 0x80)
142            m_segment = m_data & 0x3f;
143         else
144            m_segment = m_data;
145      }
146      else if (!(m_control & DP_LINE_OUTPUT_ENABLE) && !(m_control & DP_LINE_RESET))
147      {
148         // read data
149         if ((pack_addr + OPK_HEAD_SIZE) < length())
150         {
151            fseek(pack_addr + OPK_HEAD_SIZE, SEEK_SET);
152            fread(&m_data, 1);
153         }
154         else
155         {
156            m_data = 0xff;
157         }
158      }
159      else if (!(m_control & DP_LINE_OUTPUT_ENABLE) && (m_control & DP_LINE_RESET))
160      {
161         // read datapack ID
162         if (m_id & DP_ID_EPROM)
163            m_data = m_id;
164         else
165            m_data = 0x01;      // for identify RAM pack
166      }
167   }
168}
169
170
171/*-------------------------------------------------
172    read datapack data lines
173-------------------------------------------------*/
174
175UINT8 datapack_device::data_r()
176{
177   return (!(m_control & DP_LINE_SLOT_SELECT)) ? m_data : 0;
178}
179
180
181/*-------------------------------------------------
182    write datapack data lines
183-------------------------------------------------*/
184
185void  datapack_device::data_w(UINT8 data)
186{
187   if (is_loaded())
188   {
189      m_data = data;
190
191      // updates the internal state
192      if (!(data & DP_LINE_SLOT_SELECT))
193         update();
194   }
195}
196
197
198/*-------------------------------------------------
199    read datapack control lines
200-------------------------------------------------*/
201
202UINT8 datapack_device::control_r()
203{
204   return m_control;
205}
206
207
208/*-------------------------------------------------
209    write datapack control lines
210-------------------------------------------------*/
211
212void datapack_device::control_w(UINT8 data)
213{
214   if (is_loaded())
215   {
216      if ((m_control & DP_LINE_CLOCK) != (data & DP_LINE_CLOCK))
217      {
218         // increments the counter
219         m_counter++;
220
221         if (m_counter >= ((m_id & DP_ID_PAGED) ? 0x100 : (m_size << 13)))
222            m_counter = 0;
223      }
224
225      if ((m_control & DP_LINE_PROGRAM) && !(data & DP_LINE_PROGRAM))
226      {
227         // increments the page
228         m_page++;
229
230         if (m_page >= (m_size << 5))
231            m_page = 0;
232      }
233
234      if (data & DP_LINE_RESET)
235      {
236         // reset counter and page
237         m_counter = 0;
238         m_page = 0;
239      }
240
241      m_control = data;
242
243      // updates the internal state
244      if (!(data & DP_LINE_SLOT_SELECT))
245         update();
246   }
247}
248
249
250/*-------------------------------------------------
251    DEVICE_IMAGE_LOAD( datapack )
252-------------------------------------------------*/
253
254bool datapack_device::call_load()
255{
256   UINT8 data[0x10];
257
258   fread(data, 0x10);
259
260   // check the OPK head
261   if(strncmp((const char*)data, "OPK", 3))
262      return IMAGE_INIT_FAIL;
263
264   // get datapack ID and size
265   m_id   = data[OPK_HEAD_SIZE + 0];
266   m_size = data[OPK_HEAD_SIZE + 1];
267
268   return IMAGE_INIT_PASS;
269}
270
271
272/*-------------------------------------------------
273    DEVICE_IMAGE_CREATE( datapack )
274-------------------------------------------------*/
275
276bool datapack_device::call_create(int format_type, option_resolution *create_args)
277{
278   static const UINT8 opk_head[6] = {'O', 'P', 'K', 0x00, 0x00, 0x00};
279
280   if (create_args != NULL)
281   {
282      m_id = 0x40;
283      m_id |= (option_resolution_lookup_int(create_args, 'R')) ? 0x00 : 0x02;
284      m_id |= (option_resolution_lookup_int(create_args, 'P')) ? 0x04 : 0x00;
285      m_id |= (option_resolution_lookup_int(create_args, 'W')) ? 0x00 : 0x08;
286      m_id |= (option_resolution_lookup_int(create_args, 'B')) ? 0x00 : 0x10;
287      m_id |= (option_resolution_lookup_int(create_args, 'C')) ? 0x20 : 0x00;
288      m_size = option_resolution_lookup_int(create_args, 'S');
289   }
290   else
291   {
292      // 64k RAM datapack by default
293      m_id = 0x7c;
294      m_size = 0x08;
295   }
296
297   fwrite(opk_head, 6);
298   fwrite(&m_id, 1);
299   fwrite(&m_size, 1);
300
301   return IMAGE_INIT_PASS;
302}
303
304
305/*-------------------------------------------------
306    DEVICE_IMAGE_UNLOAD( datapack )
307-------------------------------------------------*/
308
309void datapack_device::call_unload()
310{
311   m_id      = 0;
312   m_size    = 0;
313   m_counter = 0;
314   m_page    = 0;
315   m_segment = 0;
316   m_control = 0;
317   m_data    = 0;
318}
src/mess/machine/psion_pack.h
r12101r12102
1/****************************************************************************
2 
3    psion_pack.h
4 
5****************************************************************************/
6 
7#ifndef __PSION_PACK_H__
8#define __PSION_PACK_H__
9 
10 
11/***************************************************************************
12    TYPE DEFINITIONS
13***************************************************************************/
14 
15// ======================> datapack_device
16 
17class datapack_device :   public device_t,
18                  public device_image_interface
19{
20public:
21   // construction/destruction
22   datapack_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
23   virtual ~datapack_device();
24 
25   // image-level overrides
26   virtual bool call_load();
27   virtual void call_unload();
28   virtual bool call_create(int format_type, option_resolution *create_args);
29   virtual bool call_softlist_load(char *swlist, char *swname, rom_entry *start_entry) { return load_software(swlist, swname, start_entry); }
30 
31   virtual iodevice_t image_type() const { return IO_CARTSLOT; }
32   virtual bool is_readable()  const { return 1; }
33   virtual bool is_writeable() const { return 1; }
34   virtual bool is_creatable() const { return 1; }
35   virtual bool must_be_loaded() const { return 0; }
36   virtual bool is_reset_on_load() const { return 0; }
37   virtual const char *image_interface() const { return "psion_pack"; }
38   virtual const char *file_extensions() const { return "opk"; }
39   virtual const option_guide *create_option_guide() const;
40 
41   // specific implementation
42   UINT8 data_r();
43   void  data_w(UINT8 data);
44   UINT8 control_r();
45   void control_w(UINT8 data);
46 
47protected:
48   // internal helper
49   void update();
50 
51   // device-level overrides
52   virtual void device_start();
53   virtual void device_config_complete();
54 
55private:
56   // internal device state
57   UINT8  m_id;            //datapack ID
58   UINT8  m_size;            //size in 8k blocks
59   UINT8  m_data;            //data lines
60   UINT8  m_control;         //control lines
61   UINT16 m_counter;         //address counter
62   UINT8  m_page;            //active page (only for paged Datapack)
63   UINT8  m_segment;         //active segment (only for segmented Datapack)
64};
65 
66 
67// device type definition
68extern const device_type PSION_DATAPACK;
69 
70 
71/***************************************************************************
72    DEVICE CONFIGURATION MACROS
73***************************************************************************/
74 
75#define MCFG_PSION_DATAPACK_ADD(_tag) \
76   MCFG_DEVICE_ADD(_tag, PSION_DATAPACK, 0) \
77 
78#endif /* __PSION_PACK_H__ */
1/****************************************************************************
2
3    psion_pack.h
4
5****************************************************************************/
6
7#ifndef __PSION_PACK_H__
8#define __PSION_PACK_H__
9
10
11/***************************************************************************
12    TYPE DEFINITIONS
13***************************************************************************/
14
15// ======================> datapack_device
16
17class datapack_device :   public device_t,
18                  public device_image_interface
19{
20public:
21   // construction/destruction
22   datapack_device(const machine_config &mconfig, const char *tag, device_t *owner, UINT32 clock);
23   virtual ~datapack_device();
24
25   // image-level overrides
26   virtual bool call_load();
27   virtual void call_unload();
28   virtual bool call_create(int format_type, option_resolution *create_args);
29   virtual bool call_softlist_load(char *swlist, char *swname, rom_entry *start_entry) { return load_software(swlist, swname, start_entry); }
30
31   virtual iodevice_t image_type() const { return IO_CARTSLOT; }
32   virtual bool is_readable()  const { return 1; }
33   virtual bool is_writeable() const { return 1; }
34   virtual bool is_creatable() const { return 1; }
35   virtual bool must_be_loaded() const { return 0; }
36   virtual bool is_reset_on_load() const { return 0; }
37   virtual const char *image_interface() const { return "psion_pack"; }
38   virtual const char *file_extensions() const { return "opk"; }
39   virtual const option_guide *create_option_guide() const;
40
41   // specific implementation
42   UINT8 data_r();
43   void  data_w(UINT8 data);
44   UINT8 control_r();
45   void control_w(UINT8 data);
46
47protected:
48   // internal helper
49   void update();
50
51   // device-level overrides
52   virtual void device_start();
53   virtual void device_config_complete();
54
55private:
56   // internal device state
57   UINT8  m_id;            //datapack ID
58   UINT8  m_size;            //size in 8k blocks
59   UINT8  m_data;            //data lines
60   UINT8  m_control;         //control lines
61   UINT16 m_counter;         //address counter
62   UINT8  m_page;            //active page (only for paged Datapack)
63   UINT8  m_segment;         //active segment (only for segmented Datapack)
64};
65
66
67// device type definition
68extern const device_type PSION_DATAPACK;
69
70
71/***************************************************************************
72    DEVICE CONFIGURATION MACROS
73***************************************************************************/
74
75#define MCFG_PSION_DATAPACK_ADD(_tag) \
76   MCFG_DEVICE_ADD(_tag, PSION_DATAPACK, 0) \
77
78#endif /* __PSION_PACK_H__ */
src/mess/machine/segasms.c
r12101r12102
1#include "emu.h"
2#include "crsshair.h"
3#include "image.h"
4#include "includes/segasms.h"
5#include "video/smsvdp.h"
6#include "sound/2413intf.h"
7#include "imagedev/cartslot.h"
8#include "hashfile.h"
9 
10#define VERBOSE 0
11#define LOG(x) do { if (VERBOSE) logerror x; } while (0)
12 
13#define CF_CODEMASTERS_MAPPER    0x01
14#define CF_KOREAN_MAPPER         0x02
15#define CF_KOREAN_ZEMINA_MAPPER  0x04
16#define CF_KOREAN_NOBANK_MAPPER  0x08
17#define CF_93C46_EEPROM          0x10
18#define CF_ONCART_RAM            0x20
19#define CF_GG_SMS_MODE           0x40
20 
21#define LGUN_RADIUS           6
22#define LGUN_X_INTERVAL       4
23 
24 
25static void setup_rom(address_space *space);
26 
27 
28static TIMER_CALLBACK( rapid_fire_callback )
29{
30   sms_state *state = machine.driver_data<sms_state>();
31   state->m_rapid_fire_state_1 ^= 0xff;
32   state->m_rapid_fire_state_2 ^= 0xff;
33}
34 
35 
36static WRITE8_HANDLER( sms_input_write )
37{
38   sms_state *state = space->machine().driver_data<sms_state>();
39 
40   switch (offset)
41   {
42   case 0:
43      switch (input_port_read_safe(space->machine(), "CTRLSEL", 0x00) & 0x0f)
44      {
45      case 0x03:   /* Sports Pad */
46         if (data != state->m_sports_pad_last_data_1)
47         {
48            UINT32 cpu_cycles = downcast<cpu_device *>(&space->device())->total_cycles();
49 
50            state->m_sports_pad_last_data_1 = data;
51            if (cpu_cycles - state->m_last_sports_pad_time_1 > 512)
52            {
53               state->m_sports_pad_state_1 = 3;
54               state->m_sports_pad_1_x = input_port_read(space->machine(), "SPORT0");
55               state->m_sports_pad_1_y = input_port_read(space->machine(), "SPORT1");
56            }
57            state->m_last_sports_pad_time_1 = cpu_cycles;
58            state->m_sports_pad_state_1 = (state->m_sports_pad_state_1 + 1) & 3;
59         }
60         break;
61      }
62      break;
63 
64   case 1:
65      switch (input_port_read_safe(space->machine(), "CTRLSEL", 0x00) >> 4)
66      {
67      case 0x03:   /* Sports Pad */
68         if (data != state->m_sports_pad_last_data_2)
69         {
70            UINT32 cpu_cycles = downcast<cpu_device *>(&space->device())->total_cycles();
71 
72            state->m_sports_pad_last_data_2 = data;
73            if (cpu_cycles - state->m_last_sports_pad_time_2 > 2048)
74            {
75               state->m_sports_pad_state_2 = 3;
76               state->m_sports_pad_2_x = input_port_read(space->machine(), "SPORT2");
77               state->m_sports_pad_2_y = input_port_read(space->machine(), "SPORT3");
78            }
79            state->m_last_sports_pad_time_2 = cpu_cycles;
80            state->m_sports_pad_state_2 = (state->m_sports_pad_state_2 + 1) & 3;
81         }
82         break;
83      }
84      break;
85   }
86}
87 
88/* FIXME: this function is a hack for Light Phaser emulation. Theoretically
89   sms_vdp_hcount_latch() should be used instead, but it returns incorrect
90   position for unknown reason (timing?) */
91static void sms_vdp_hcount_lphaser( running_machine &machine, int hpos )
92{
93   sms_state *state = machine.driver_data<sms_state>();
94   int hpos_tmp = hpos + state->m_lphaser_x_offs;
95   UINT8 tmp = ((hpos_tmp - 46) >> 1) & 0xff;
96 
97   //printf ("sms_vdp_hcount_lphaser: hpos %3d hpos_tmp %3d => hcount %2X\n", hpos, hpos_tmp, tmp);
98   sms_vdp_hcount_latch_w(state->m_vdp, 0, tmp);
99}
100 
101 
102/*
103    Light Phaser (light gun) emulation notes:
104    - The sensor is activated based on color brightness of some individual
105      pixels being drawn by the beam, at circular area where the gun is aiming.
106    - Currently, brightness is calculated based only on single pixels.
107    - In general, after the trigger is pressed, games draw the next frame using
108      a light color pattern, to make sure sensor will be activated. If emulation
109      skips that frame, sensor may stay deactivated. Frameskip set to 0 (no skip) is
110      recommended to avoid problems.
111    - When sensor switches from off to on, a value is latched for HCount register
112      and a flag is set. The emulation uses the flag to avoid sequential latches and
113      to signal that TH line is activated when the status of the input port is read.
114      When the status is read, the flag is cleared, or else it is cleared later when
115      the Pause status is read (end of a frame). This is necessary because the
116      "Color & Switch Test" ROM only reads the TH bit after the VINT line.
117    - The gun test of "Color & Switch Test" is an example that requires checks
118      of sensor status independent of other events, like trigger press or TH bit
119      reads. Another example is the title screen of "Hang-On & Safari Hunt", where
120      the game only reads HCount register in a loop, expecting a latch by the gun.
121    - The whole procedure is managed by a timer callback, that always reschedule
122      itself to run in some intervals when the beam is at the circular area.
123*/
124static int lgun_bright_aim_area( running_machine &machine, emu_timer *timer, int lgun_x, int lgun_y )
125{
126   sms_state *state = machine.driver_data<sms_state>();
127   const int r_x_r = LGUN_RADIUS * LGUN_RADIUS;
128   screen_device *screen = machine.first_screen();
129   const rectangle &visarea = screen->visible_area();
130   int beam_x = screen->hpos();
131   int beam_y = screen->vpos();
132   int dx, dy;
133   int result = 0;
134   int pos_changed = 0;
135   double dx_circ;
136 
137    while (1)
138   {
139      dy = abs(beam_y - lgun_y);
140 
141      if (dy > LGUN_RADIUS || beam_y < visarea.min_y || beam_y > visarea.max_y)
142      {
143         beam_y = lgun_y - LGUN_RADIUS;
144         if (beam_y < visarea.min_y)
145            beam_y = visarea.min_y;
146         dy = abs(beam_y - lgun_y);
147         pos_changed = 1;
148      }
149      /* step 1: r^2 = dx^2 + dy^2 */
150      /* step 2: dx^2 = r^2 - dy^2 */
151      /* step 3: dx = sqrt(r^2 - dy^2) */
152      dx_circ = ceil((float) sqrt((float) (r_x_r - (dy * dy))));
153      dx = abs(beam_x - lgun_x);
154 
155      if (dx > dx_circ || beam_x < visarea.min_x || beam_x > visarea.max_x)
156      {
157         if (beam_x > lgun_x)
158         {
159            beam_x = 0;
160            beam_y++;
161            continue;
162         }
163         beam_x = lgun_x - dx_circ;
164         if (beam_x < visarea.min_x)
165            beam_x = visarea.min_x;
166         pos_changed = 1;
167      }
168 
169      if (!pos_changed)
170      {
171         result = sms_vdp_check_brightness(state->m_vdp, beam_x, beam_y);
172         /* next check at same line */
173         beam_x += LGUN_X_INTERVAL;
174         pos_changed = 1;
175      }
176      else
177         break;
178   }
179   timer->adjust(machine.first_screen()->time_until_pos(beam_y, beam_x));
180 
181   return result;
182}
183 
184static UINT8 sms_vdp_hcount( running_machine &machine )
185{
186   UINT8 tmp;
187   screen_device *screen = machine.first_screen();
188   int hpos = screen->hpos();
189 
190   /* alternative method: pass HCounter test, but some others fail */
191   //int hpos_tmp = hpos;
192   //if ((hpos + 2) % 6 == 0) hpos_tmp--;
193   //tmp = ((hpos_tmp - 46) >> 1) & 0xff;
194 
195   UINT64 calc_cycles;
196   attotime time_end;
197   int vpos = screen->vpos();
198   int max_hpos = screen->width() - 1;
199 
200   if (hpos == max_hpos)
201      time_end = attotime::zero;
202   else
203      time_end = screen->time_until_pos(vpos, max_hpos);
204   calc_cycles = machine.device<cpu_device>("maincpu")->attotime_to_clocks(time_end);
205 
206   /* equation got from SMSPower forum, posted by Flubba. */
207   tmp = ((590 - (calc_cycles * 3)) / 4) & 0xff;
208 
209   //printf ("sms_vdp_hcount: hpos %3d => hcount %2X\n", hpos, tmp);
210   return tmp;
211}
212 
213 
214static void sms_vdp_hcount_latch( running_machine &machine )
215{
216   sms_state *state = machine.driver_data<sms_state>();
217   UINT8 value = sms_vdp_hcount(machine);
218 
219   sms_vdp_hcount_latch_w(state->m_vdp, 0, value);
220}
221 
222 
223static UINT16 screen_hpos_nonscaled( screen_device *screen, int scaled_hpos )
224{
225   const rectangle &visarea = screen->visible_area();
226   int offset_x = (scaled_hpos * (visarea.max_x - visarea.min_x)) / 255;
227   return visarea.min_x + offset_x;
228}
229 
230 
231static UINT16 screen_vpos_nonscaled( screen_device *screen, int scaled_vpos )
232{
233   const rectangle &visarea = screen->visible_area();
234   int offset_y = (scaled_vpos * (visarea.max_y - visarea.min_y)) / 255;
235   return visarea.min_y + offset_y;
236}
237 
238 
239static void lphaser1_sensor_check( running_machine &machine )
240{
241   sms_state *state = machine.driver_data<sms_state>();
242   const int x = screen_hpos_nonscaled(machine.first_screen(), input_port_read(machine, "LPHASER0"));
243   const int y = screen_vpos_nonscaled(machine.first_screen(), input_port_read(machine, "LPHASER1"));
244 
245   if (lgun_bright_aim_area(machine, state->m_lphaser_1_timer, x, y))
246   {
247      if (state->m_lphaser_1_latch == 0)
248      {
249         state->m_lphaser_1_latch = 1;
250         sms_vdp_hcount_lphaser(machine, x);
251      }
252   }
253}
254 
255static void lphaser2_sensor_check( running_machine &machine )
256{
257   sms_state *state = machine.driver_data<sms_state>();
258   const int x = screen_hpos_nonscaled(machine.first_screen(), input_port_read(machine, "LPHASER2"));
259   const int y = screen_vpos_nonscaled(machine.first_screen(), input_port_read(machine, "LPHASER3"));
260 
261   if (lgun_bright_aim_area(machine, state->m_lphaser_2_timer, x, y))
262   {
263      if (state->m_lphaser_2_latch == 0)
264      {
265         state->m_lphaser_2_latch = 1;
266         sms_vdp_hcount_lphaser(machine, x);
267      }
268   }
269}
270 
271 
272// at each input port read we check if lightguns are enabled in one of the ports:
273// if so, we turn on crosshair and the lightgun timer
274static TIMER_CALLBACK( lightgun_tick )
275{
276   sms_state *state = machine.driver_data<sms_state>();
277 
278   if ((input_port_read_safe(machine, "CTRLSEL", 0x00) & 0x0f) == 0x01)
279   {
280      /* enable crosshair */
281      crosshair_set_screen(machine, 0, CROSSHAIR_SCREEN_ALL);
282      if (!state->m_lphaser_1_timer->enabled())
283         lphaser1_sensor_check(machine);
284   }
285   else
286   {
287      /* disable crosshair */
288      crosshair_set_screen(machine, 0, CROSSHAIR_SCREEN_NONE);
289      state->m_lphaser_1_timer->enable(0);
290   }
291 
292   if ((input_port_read_safe(machine, "CTRLSEL", 0x00) & 0xf0) == 0x10)
293   {
294      /* enable crosshair */
295      crosshair_set_screen(machine, 1, CROSSHAIR_SCREEN_ALL);
296      if (!state->m_lphaser_2_timer->enabled())
297         lphaser2_sensor_check(machine);
298   }
299   else
300   {
301      /* disable crosshair */
302      crosshair_set_screen(machine, 1, CROSSHAIR_SCREEN_NONE);
303      state->m_lphaser_2_timer->enable(0);
304   }
305}
306 
307 
308static TIMER_CALLBACK( lphaser_1_callback )
309{
310   lphaser1_sensor_check(machine);
311}
312 
313 
314static TIMER_CALLBACK( lphaser_2_callback )
315{
316   lphaser2_sensor_check(machine);
317}
318 
319 
320INPUT_CHANGED( lgun1_changed )
321{
322   sms_state *state = field.machine().driver_data<sms_state>();
323   if (!state->m_lphaser_1_timer ||
324      (input_port_read_safe(field.machine(), "CTRLSEL", 0x00) & 0x0f) != 0x01)
325      return;
326 
327   if (newval != oldval)
328      lphaser1_sensor_check(field.machine());
329}
330 
331INPUT_CHANGED( lgun2_changed )
332{
333   sms_state *state = field.machine().driver_data<sms_state>();
334   if (!state->m_lphaser_2_timer ||
335      (input_port_read_safe(field.machine(), "CTRLSEL", 0x00) & 0xf0) != 0x10)
336      return;
337 
338   if (newval != oldval)
339      lphaser2_sensor_check(field.machine());
340}
341 
342 
343static void sms_get_inputs( address_space *space )
344{
345   sms_state *state = space->machine().driver_data<sms_state>();
346   UINT8 data = 0x00;
347   UINT32 cpu_cycles = downcast<cpu_device *>(&space->device())->total_cycles();
348   running_machine &machine = space->machine();
349 
350   state->m_input_port0 = 0xff;
351   state->m_input_port1 = 0xff;
352 
353   if (cpu_cycles - state->m_last_paddle_read_time > 256)
354   {
355      state->m_paddle_read_state ^= 0xff;
356      state->m_last_paddle_read_time = cpu_cycles;
357   }
358 
359   /* Check if lightgun has been chosen as input: if so, enable crosshair */
360   machine.scheduler().timer_set(attotime::zero, FUNC(lightgun_tick));
361 
362   /* Player 1 */
363   switch (input_port_read_safe(machine, "CTRLSEL", 0x00) & 0x0f)
364   {
365   case 0x00:  /* Joystick */
366      data = input_port_read(machine, "PORT_DC");
367      /* Check Rapid Fire setting for Button A */
368      if (!(data & 0x10) && (input_port_read(machine, "RFU") & 0x01))
369         data |= state->m_rapid_fire_state_1 & 0x10;
370 
371      /* Check Rapid Fire setting for Button B */
372      if (!(data & 0x20) && (input_port_read(machine, "RFU") & 0x02))
373         data |= state->m_rapid_fire_state_1 & 0x20;
374 
375      state->m_input_port0 = (state->m_input_port0 & 0xc0) | (data & 0x3f);
376      break;
377 
378   case 0x01:  /* Light Phaser */
379      data = (input_port_read(machine, "CTRLIPT") & 0x01) << 4;
380      if (!(data & 0x10))
381      {
382         if (input_port_read(machine, "RFU") & 0x01)
383            data |= state->m_rapid_fire_state_1 & 0x10;
384      }
385      /* just consider the button (trigger) bit */
386      data |= ~0x10;
387      state->m_input_port0 = (state->m_input_port0 & 0xc0) | (data & 0x3f);
388      break;
389 
390   case 0x02:  /* Paddle Control */
391      /* Get button A state */
392      data = input_port_read(machine, "PADDLE0");
393 
394      if (state->m_paddle_read_state)
395         data = data >> 4;
396 
397      state->m_input_port0 = (state->m_input_port0 & 0xc0) | (data & 0x0f) | (state->m_paddle_read_state & 0x20)
398                      | ((input_port_read(machine, "CTRLIPT") & 0x02) << 3);
399      break;
400 
401   case 0x03:   /* Sega Sports Pad */
402      switch (state->m_sports_pad_state_1)
403      {
404      case 0:
405         data = (state->m_sports_pad_1_x >> 4) & 0x0f;
406         break;
407      case 1:
408         data = state->m_sports_pad_1_x & 0x0f;
409         break;
410      case 2:
411         data = (state->m_sports_pad_1_y >> 4) & 0x0f;
412         break;
413      case 3:
414         data = state->m_sports_pad_1_y & 0x0f;
415         break;
416      }
417      state->m_input_port0 = (state->m_input_port0 & 0xc0) | data | ((input_port_read(machine, "CTRLIPT") & 0x0c) << 2);
418      break;
419   }
420 
421   /* Player 2 */
422   switch (input_port_read_safe(machine, "CTRLSEL", 0x00)  & 0xf0)
423   {
424   case 0x00:   /* Joystick */
425      data = input_port_read(machine, "PORT_DC");
426      state->m_input_port0 = (state->m_input_port0 & 0x3f) | (data & 0xc0);
427 
428      data = input_port_read(machine, "PORT_DD");
429      /* Check Rapid Fire setting for Button A */
430      if (!(data & 0x04) && (input_port_read(machine, "RFU") & 0x04))
431         data |= state->m_rapid_fire_state_2 & 0x04;
432 
433      /* Check Rapid Fire setting for Button B */
434      if (!(data & 0x08) && (input_port_read(machine, "RFU") & 0x08))
435         data |= state->m_rapid_fire_state_2 & 0x08;
436 
437      state->m_input_port1 = (state->m_input_port1 & 0xf0) | (data & 0x0f);
438      break;
439 
440   case 0x10:   /* Light Phaser */
441      data = (input_port_read(machine, "CTRLIPT") & 0x10) >> 2;
442      if (!(data & 0x04))
443      {
444         if (input_port_read(machine, "RFU") & 0x04)
445            data |= state->m_rapid_fire_state_2 & 0x04;
446      }
447      /* just consider the button (trigger) bit */
448      data |= ~0x04;
449      state->m_input_port1 = (state->m_input_port1 & 0xf0) | (data & 0x0f);
450      break;
451 
452   case 0x20:   /* Paddle Control */
453      /* Get button A state */
454      data = input_port_read(machine, "PADDLE1");
455      if (state->m_paddle_read_state)
456         data = data >> 4;
457 
458      state->m_input_port0 = (state->m_input_port0 & 0x3f) | ((data & 0x03) << 6);
459      state->m_input_port1 = (state->m_input_port1 & 0xf0) | ((data & 0x0c) >> 2) | (state->m_paddle_read_state & 0x08)
460                      | ((input_port_read(machine, "CTRLIPT") & 0x20) >> 3);
461      break;
462 
463   case 0x30:   /* Sega Sports Pad */
464      switch (state->m_sports_pad_state_2)
465      {
466      case 0:
467         data = state->m_sports_pad_2_x & 0x0f;
468         break;
469      case 1:
470         data = (state->m_sports_pad_2_x >> 4) & 0x0f;
471         break;
472      case 2:
473         data = state->m_sports_pad_2_y & 0x0f;
474         break;
475      case 3:
476         data = (state->m_sports_pad_2_y >> 4) & 0x0f;
477         break;
478      }
479      state->m_input_port0 = (state->m_input_port0 & 0x3f) | ((data & 0x03) << 6);
480      state->m_input_port1 = (state->m_input_port1 & 0xf0) | (data >> 2) | ((input_port_read(machine, "CTRLIPT") & 0xc0) >> 4);
481      break;
482   }
483}
484 
485 
486WRITE8_HANDLER( sms_fm_detect_w )
487{
488   sms_state *state = space->machine().driver_data<sms_state>();
489 
490   if (state->m_has_fm)
491      state->m_fm_detect = (data & 0x01);
492}
493 
494 
495READ8_HANDLER( sms_fm_detect_r )
496{
497   sms_state *state = space->machine().driver_data<sms_state>();
498 
499   if (state->m_has_fm)
500   {
501      return state->m_fm_detect;
502   }
503   else
504   {
505      if (state->m_bios_port & IO_CHIP)
506      {
507         return 0xff;
508      }
509      else
510      {
511         sms_get_inputs(space);
512         return state->m_input_port0;
513      }
514   }
515}
516 
517WRITE8_HANDLER( sms_io_control_w )
518{
519   sms_state *state = space->machine().driver_data<sms_state>();
520 
521   if (data & 0x08)
522   {
523      /* check if TH pin level is high (1) and was low last time */
524      if (data & 0x80 && !(state->m_ctrl_reg & 0x80))
525      {
526         sms_vdp_hcount_latch(space->machine());
527      }
528      sms_input_write(space, 0, (data & 0x20) >> 5);
529   }
530 
531   if (data & 0x02)
532   {
533      if (data & 0x20 && !(state->m_ctrl_reg & 0x20))
534      {
535         sms_vdp_hcount_latch(space->machine());
536      }
537      sms_input_write(space, 1, (data & 0x80) >> 7);
538   }
539 
540   state->m_ctrl_reg = data;
541}
542 
543 
544READ8_HANDLER( sms_count_r )
545{
546   sms_state *state = space->machine().driver_data<sms_state>();
547 
548   if (offset & 0x01)
549      return sms_vdp_hcount_latch_r(state->m_vdp, offset);
550   else
551      return sms_vdp_vcount_r(state->m_vdp, offset);
552}
553 
554 
555/*
556 Check if the pause button is pressed.
557 If the gamegear is in sms mode, check if the start button is pressed.
558 */
559void sms_pause_callback( running_machine &machine )
560{
561   sms_state *state = machine.driver_data<sms_state>();
562 
563   if (state->m_is_gamegear && !(state->m_cartridge[state->m_current_cartridge].features & CF_GG_SMS_MODE))
564      return;
565 
566   if (!(input_port_read(machine, state->m_is_gamegear ? "START" : "PAUSE") & 0x80))
567   {
568      if (!state->m_paused)
569      {
570         cputag_set_input_line(machine, "maincpu", INPUT_LINE_NMI, PULSE_LINE);
571      }
572      state->m_paused = 1;
573   }
574   else
575   {
576      state->m_paused = 0;
577   }
578 
579   /* clear Light Phaser latch flags for next frame */
580   state->m_lphaser_1_latch = 0;
581   state->m_lphaser_2_latch = 0;
582}
583 
584READ8_HANDLER( sms_input_port_0_r )
585{
586   sms_state *state = space->machine().driver_data<sms_state>();
587 
588   if (state->m_bios_port & IO_CHIP)
589   {
590      return 0xff;
591   }
592   else
593   {
594      sms_get_inputs(space);
595      return state->m_input_port0;
596   }
597}
598 
599 
600READ8_HANDLER( sms_input_port_1_r )
601{
602   sms_state *state = space->machine().driver_data<sms_state>();
603 
604   if (state->m_bios_port & IO_CHIP)
605      return 0xff;
606 
607   sms_get_inputs(space);
608 
609   /* Reset Button */
610   state->m_input_port1 = (state->m_input_port1 & 0xef) | (input_port_read_safe(space->machine(), "RESET", 0x01) & 0x01) << 4;
611 
612   /* Do region detection if TH of ports A and B are set to output (0) */
613   if (!(state->m_ctrl_reg & 0x0a))
614   {
615      /* Move bits 7,5 of IO control port into bits 7, 6 */
616      state->m_input_port1 = (state->m_input_port1 & 0x3f) | (state->m_ctrl_reg & 0x80) | (state->m_ctrl_reg & 0x20) << 1;
617 
618      /* Inverse region detect value for Japanese machines */
619      if (state->m_is_region_japan)
620         state->m_input_port1 ^= 0xc0;
621   }
622   else
623   {
624      if (state->m_ctrl_reg & 0x02 && state->m_lphaser_1_latch)
625      {
626         state->m_input_port1 &= ~0x40;
627         state->m_lphaser_1_latch = 0;
628      }
629 
630      if (state->m_ctrl_reg & 0x08 && state->m_lphaser_2_latch)
631      {
632         state->m_input_port1 &= ~0x80;
633         state->m_lphaser_2_latch = 0;
634      }
635   }
636 
637   return state->m_input_port1;
638}
639 
640 
641 
642WRITE8_HANDLER( sms_ym2413_register_port_0_w )
643{
644   sms_state *state = space->machine().driver_data<sms_state>();
645 
646   if (state->m_has_fm)
647      ym2413_w(state->m_ym, 0, (data & 0x3f));
648}
649 
650 
651WRITE8_HANDLER( sms_ym2413_data_port_0_w )
652{
653   sms_state *state = space->machine().driver_data<sms_state>();
654 
655   if (state->m_has_fm)
656   {
657      logerror("data_port_0_w %x %x\n", offset, data);
658      ym2413_w(state->m_ym, 1, data);
659   }
660}
661 
662 
663READ8_HANDLER( gg_input_port_2_r )
664{
665   sms_state *state = space->machine().driver_data<sms_state>();
666 
667   //logerror("joy 2 read, val: %02x, pc: %04x\n", ((state->m_is_region_japan ? 0x00 : 0x40) | (input_port_read(machine, "START") & 0x80)), activecpu_get_pc());
668   return ((state->m_is_region_japan ? 0x00 : 0x40) | (input_port_read(space->machine(), "START") & 0x80));
669}
670 
671 
672READ8_HANDLER( sms_sscope_r )
673{
674   sms_state *state = space->machine().driver_data<sms_state>();
675   return state->m_sscope_state;
676}
677 
678 
679WRITE8_HANDLER( sms_sscope_w )
680{
681   sms_state *state = space->machine().driver_data<sms_state>();
682   state->m_sscope_state = data;
683}
684 
685 
686READ8_HANDLER( sms_mapper_r )
687{
688   sms_state *state = space->machine().driver_data<sms_state>();
689   return state->m_mapper[offset];
690}
691 
692/* Terebi Oekaki */
693/* The following code comes from sg1000.c. We should eventually merge these TV Draw implementations */
694static WRITE8_HANDLER( sms_tvdraw_axis_w )
695{
696   sms_state *state = space->machine().driver_data<sms_state>();
697   UINT8 tvboard_on = input_port_read_safe(space->machine(), "TVDRAW", 0x00);
698 
699   if (data & 0x01)
700   {
701      state->m_tvdraw_data = tvboard_on ? input_port_read(space->machine(), "TVDRAW_X") : 0x80;
702 
703      if (state->m_tvdraw_data < 4) state->m_tvdraw_data = 4;
704      if (state->m_tvdraw_data > 251) state->m_tvdraw_data = 251;
705   }
706   else
707   {
708      state->m_tvdraw_data = tvboard_on ? input_port_read(space->machine(), "TVDRAW_Y") + 0x20 : 0x80;
709   }
710}
711 
712static READ8_HANDLER( sms_tvdraw_status_r )
713{
714   UINT8 tvboard_on = input_port_read_safe(space->machine(), "TVDRAW", 0x00);
715   return tvboard_on ? input_port_read(space->machine(), "TVDRAW_PEN") : 0x01;
716}
717 
718static READ8_HANDLER( sms_tvdraw_data_r )
719{
720   sms_state *state = space->machine().driver_data<sms_state>();
721   return state->m_tvdraw_data;
722}
723 
724 
725WRITE8_HANDLER( sms_mapper_w )
726{
727   sms_state *state = space->machine().driver_data<sms_state>();
728   int page;
729   UINT8 *SOURCE_BIOS;
730   UINT8 *SOURCE_CART;
731   UINT8 *SOURCE;
732   UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
733 
734   offset &= 3;
735 
736   state->m_mapper[offset] = data;
737   state->m_mapper_ram[offset] = data;
738 
739   if (state->m_cartridge[state->m_current_cartridge].ROM)
740   {
741      SOURCE_CART = state->m_cartridge[state->m_current_cartridge].ROM + (( rom_page_count > 0) ? data % rom_page_count : 0) * 0x4000;
742   }
743   else
744   {
745      SOURCE_CART = state->m_banking_none[1];
746   }
747 
748   if (state->m_BIOS)
749   {
750      SOURCE_BIOS = state->m_BIOS + ((state->m_bios_page_count > 0) ? data % state->m_bios_page_count : 0) * 0x4000;
751   }
752   else
753   {
754      SOURCE_BIOS = state->m_banking_none[1];
755   }
756 
757   if (state->m_bios_port & IO_BIOS_ROM || (state->m_is_gamegear && state->m_BIOS == NULL))
758   {
759      if (!(state->m_bios_port & IO_CARTRIDGE) || (state->m_is_gamegear && state->m_BIOS == NULL))
760      {
761         page = (rom_page_count > 0) ? data % rom_page_count : 0;
762         if (!state->m_cartridge[state->m_current_cartridge].ROM)
763            return;
764         SOURCE = SOURCE_CART;
765      }
766      else
767      {
768         /* nothing to page in */
769         return;
770      }
771   }
772   else
773   {
774      page = (state->m_bios_page_count > 0) ? data % state->m_bios_page_count : 0;
775      if (!state->m_BIOS)
776         return;
777      SOURCE = SOURCE_BIOS;
778   }
779 
780   switch (offset)
781   {
782   case 0: /* Control */
783      /* Is it ram or rom? */
784      if (data & 0x08) /* it's ram */
785      {
786         state->m_cartridge[state->m_current_cartridge].sram_save = 1;         /* SRAM should be saved on exit. */
787         if (data & 0x04)
788         {
789            LOG(("ram 1 paged.\n"));
790            SOURCE = state->m_cartridge[state->m_current_cartridge].cartSRAM + 0x4000;
791         }
792         else
793         {
794            LOG(("ram 0 paged.\n"));
795            SOURCE = state->m_cartridge[state->m_current_cartridge].cartSRAM;
796         }
797         memory_set_bankptr(space->machine(),  "bank5", SOURCE);
798         memory_set_bankptr(space->machine(),  "bank6", SOURCE + 0x2000);
799      }
800      else /* it's rom */
801      {
802         if (state->m_bios_port & IO_BIOS_ROM || ! state->m_has_bios)
803         {
804            page = (rom_page_count > 0) ? state->m_mapper[3] % rom_page_count : 0;
805            SOURCE = state->m_banking_cart[5];
806         }
807         else
808         {
809            page = (state->m_bios_page_count > 0) ? state->m_mapper[3] % state->m_bios_page_count : 0;
810            SOURCE = state->m_banking_bios[5];
811         }
812         LOG(("rom 2 paged in %x.\n", page));
813         memory_set_bankptr(space->machine(), "bank5", SOURCE);
814         memory_set_bankptr(space->machine(), "bank6", SOURCE + 0x2000);
815      }
816      break;
817 
818   case 1: /* Select 16k ROM bank for 0400-3FFF */
819      LOG(("rom 0 paged in %x.\n", page));
820      state->m_banking_bios[2] = SOURCE_BIOS + 0x0400;
821      state->m_banking_cart[2] = SOURCE_CART + 0x0400;
822      if (state->m_is_gamegear)
823         SOURCE = SOURCE_CART;
824 
825      memory_set_bankptr(space->machine(), "bank2", SOURCE + 0x0400);
826      break;
827 
828   case 2: /* Select 16k ROM bank for 4000-7FFF */
829      LOG(("rom 1 paged in %x.\n", page));
830      state->m_banking_bios[3] = SOURCE_BIOS;
831      state->m_banking_cart[3] = SOURCE_CART;
832      if (state->m_is_gamegear)
833         SOURCE = SOURCE_CART;
834 
835      memory_set_bankptr(space->machine(), "bank3", SOURCE);
836      memory_set_bankptr(space->machine(), "bank4", SOURCE + 0x2000);
837      break;
838 
839   case 3: /* Select 16k ROM bank for 8000-BFFF */
840      state->m_banking_bios[5] = SOURCE_BIOS;
841      if (state->m_is_gamegear)
842         SOURCE = SOURCE_CART;
843 
844      if ( state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
845      {
846         if (SOURCE == SOURCE_CART)
847         {
848            SOURCE = state->m_banking_cart[5];
849         }
850      }
851      else
852      {
853         state->m_banking_cart[5] = SOURCE_CART;
854      }
855 
856      if (!(state->m_mapper[0] & 0x08)) /* is RAM disabled? */
857      {
858         LOG(("rom 2 paged in %x.\n", page));
859         memory_set_bankptr(space->machine(), "bank5", SOURCE);
860         memory_set_bankptr(space->machine(), "bank6", SOURCE + 0x2000);
861      }
862      break;
863   }
864}
865 
866#ifdef MESS
867static WRITE8_HANDLER( sms_korean_zemina_banksw_w )
868{
869   sms_state *state = space->machine().driver_data<sms_state>();
870 
871   if (state->m_cartridge[state->m_current_cartridge].features & CF_KOREAN_ZEMINA_MAPPER)
872   {
873      UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x2000;
874      int page = (rom_page_count > 0) ? data % rom_page_count : 0;
875 
876      if (!state->m_cartridge[state->m_current_cartridge].ROM)
877         return;
878 
879      switch (offset & 3)
880      {
881         case 0:
882            state->m_banking_cart[5] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x2000;
883            memory_set_bankptr(space->machine(), "bank5", state->m_banking_cart[5]);
884            break;
885         case 1:
886            state->m_banking_cart[6] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x2000;
887            memory_set_bankptr(space->machine(), "bank6", state->m_banking_cart[6]);
888            break;
889         case 2:
890            state->m_banking_cart[3] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x2000;
891            memory_set_bankptr(space->machine(), "bank3", state->m_banking_cart[3]);
892            break;
893         case 3:
894            state->m_banking_cart[4] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x2000;
895            memory_set_bankptr(space->machine(), "bank4", state->m_banking_cart[4]);
896            break;
897      }
898      LOG(("Zemina mapper write: offset %x data %x.\n", offset, page));
899   }
900}
901 
902static WRITE8_HANDLER( sms_codemasters_page0_w )
903{
904   sms_state *state = space->machine().driver_data<sms_state>();
905 
906   if (state->m_cartridge[state->m_current_cartridge].ROM && state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
907   {
908      UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
909      state->m_banking_cart[1] = state->m_cartridge[state->m_current_cartridge].ROM + ((rom_page_count > 0) ? data % rom_page_count : 0) * 0x4000;
910      state->m_banking_cart[2] = state->m_banking_cart[1] + 0x0400;
911      memory_set_bankptr(space->machine(), "bank1", state->m_banking_cart[1]);
912      memory_set_bankptr(space->machine(), "bank2", state->m_banking_cart[2]);
913   }
914}
915 
916 
917static WRITE8_HANDLER( sms_codemasters_page1_w )
918{
919   sms_state *state = space->machine().driver_data<sms_state>();
920 
921   if (state->m_cartridge[state->m_current_cartridge].ROM && state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
922   {
923      /* Check if we need to switch in some RAM */
924      if (data & 0x80)
925      {
926         state->m_cartridge[state->m_current_cartridge].ram_page = data & 0x07;
927         memory_set_bankptr(space->machine(), "bank6", state->m_cartridge[state->m_current_cartridge].cartRAM + state->m_cartridge[state->m_current_cartridge].ram_page * 0x2000);
928      }
929      else
930      {
931         UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
932         state->m_banking_cart[3] = state->m_cartridge[state->m_current_cartridge].ROM + ((rom_page_count > 0) ? data % rom_page_count : 0) * 0x4000;
933         memory_set_bankptr(space->machine(), "bank3", state->m_banking_cart[3]);
934         memory_set_bankptr(space->machine(), "bank4", state->m_banking_cart[3] + 0x2000);
935         memory_set_bankptr(space->machine(), "bank6", state->m_banking_cart[5] + 0x2000);
936      }
937   }
938}
939#endif
940 
941static READ8_HANDLER( sms_kor_nobank_r )
942{
943//  printf("read %x\n", offset);
944   return space->read_byte(0xdffc + offset);
945}
946 
947static WRITE8_HANDLER( sms_kor_nobank_w )
948{
949   sms_state *state = space->machine().driver_data<sms_state>();
950   UINT8 *SOURCE_BIOS;
951   UINT8 *SOURCE = NULL;
952 
953   space->write_byte(0xdffc + offset, data);
954   state->m_mapper[offset] = data;
955 
956   // FIXME: the code below (which only handles the BIOS bankswitch at start) should be cleaned up
957   // currently, it's just a stripped down version of sms_mapper_w
958   if (state->m_BIOS)
959      SOURCE_BIOS = state->m_BIOS + ((state->m_bios_page_count > 0) ? data % state->m_bios_page_count : 0) * 0x4000;
960   else
961      SOURCE_BIOS = state->m_banking_none[1];
962 
963   if (state->m_bios_port & IO_BIOS_ROM || (state->m_is_gamegear && state->m_BIOS == NULL))
964      return;
965   else
966   {
967      if (!state->m_BIOS)
968         return;
969      SOURCE = SOURCE_BIOS;
970   }
971 
972//  printf("write %x, %x\n", offset, data);
973 
974   switch (offset)
975   {
976      case 0: /* Control */
977         if (!(data & 0x08)) /* it's not ram */
978         {
979            if (!(state->m_bios_port & IO_BIOS_ROM) && state->m_has_bios)
980            {
981               SOURCE = state->m_banking_bios[5];
982               memory_set_bankptr(space->machine(), "bank5", SOURCE);
983               memory_set_bankptr(space->machine(), "bank6", SOURCE + 0x2000);
984            }
985         }
986         break;
987 
988      case 1: /* Select 16k ROM bank for 0400-3FFF */
989         state->m_banking_bios[2] = SOURCE_BIOS + 0x0400;
990         memory_set_bankptr(space->machine(), "bank2", SOURCE + 0x0400);
991         break;
992 
993      case 2: /* Select 16k ROM bank for 4000-7FFF */
994         state->m_banking_bios[3] = SOURCE_BIOS;
995         memory_set_bankptr(space->machine(), "bank3", SOURCE);
996         memory_set_bankptr(space->machine(), "bank4", SOURCE + 0x2000);
997         break;
998 
999      case 3: /* Select 16k ROM bank for 8000-BFFF */
1000         state->m_banking_bios[5] = SOURCE_BIOS;
1001         if (!(state->m_mapper[0] & 0x08)) /* is RAM disabled? */
1002         {
1003            memory_set_bankptr(space->machine(), "bank5", SOURCE);
1004            memory_set_bankptr(space->machine(), "bank6", SOURCE + 0x2000);
1005         }
1006         break;
1007   }
1008}
1009 
1010WRITE8_HANDLER( sms_bios_w )
1011{
1012   sms_state *state = space->machine().driver_data<sms_state>();
1013   state->m_bios_port = data;
1014 
1015   logerror("bios write %02x, pc: %04x\n", data, cpu_get_pc(&space->device()));
1016 
1017   setup_rom(space);
1018}
1019 
1020 
1021WRITE8_HANDLER( sms_cartram2_w )
1022{
1023   sms_state *state = space->machine().driver_data<sms_state>();
1024 
1025   if (state->m_mapper[0] & 0x08)
1026   {
1027      logerror("write %02X to cartram at offset #%04X\n", data, offset + 0x2000);
1028      if (state->m_mapper[0] & 0x04)
1029      {
1030         state->m_cartridge[state->m_current_cartridge].cartSRAM[offset + 0x6000] = data;
1031      }
1032      else
1033      {
1034         state->m_cartridge[state->m_current_cartridge].cartSRAM[offset + 0x2000] = data;
1035      }
1036   }
1037 
1038   if (state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
1039   {
1040      state->m_cartridge[state->m_current_cartridge].cartRAM[state->m_cartridge[state->m_current_cartridge].ram_page * 0x2000 + offset] = data;
1041   }
1042 
1043   if (state->m_cartridge[state->m_current_cartridge].features & CF_KOREAN_MAPPER && offset == 0) /* Dodgeball King mapper */
1044   {
1045      UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
1046      int page = (rom_page_count > 0) ? data % rom_page_count : 0;
1047 
1048      if (!state->m_cartridge[state->m_current_cartridge].ROM)
1049         return;
1050 
1051      state->m_banking_cart[5] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x4000;
1052      memory_set_bankptr(space->machine(), "bank5", state->m_banking_cart[5]);
1053      memory_set_bankptr(space->machine(), "bank6", state->m_banking_cart[5] + 0x2000);
1054      LOG(("rom 2 paged in %x (Korean mapper).\n", page));
1055   }
1056}
1057 
1058 
1059WRITE8_HANDLER( sms_cartram_w )
1060{
1061   sms_state *state = space->machine().driver_data<sms_state>();
1062   int page;
1063 
1064   if (state->m_mapper[0] & 0x08)
1065   {
1066      logerror("write %02X to cartram at offset #%04X\n", data, offset);
1067      if (state->m_mapper[0] & 0x04)
1068      {
1069         state->m_cartridge[state->m_current_cartridge].cartSRAM[offset + 0x4000] = data;
1070      }
1071      else
1072      {
1073         state->m_cartridge[state->m_current_cartridge].cartSRAM[offset] = data;
1074      }
1075   }
1076   else
1077   {
1078      if (state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER && offset == 0) /* Codemasters mapper */
1079      {
1080         UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
1081         page = (rom_page_count > 0) ? data % rom_page_count : 0;
1082         if (!state->m_cartridge[state->m_current_cartridge].ROM)
1083            return;
1084         state->m_banking_cart[5] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x4000;
1085         memory_set_bankptr(space->machine(), "bank5", state->m_banking_cart[5]);
1086         memory_set_bankptr(space->machine(), "bank6", state->m_banking_cart[5] + 0x2000);
1087         LOG(("rom 2 paged in %x (Codemasters mapper).\n", page));
1088      }
1089      else if (state->m_cartridge[state->m_current_cartridge].features & CF_ONCART_RAM)
1090      {
1091         state->m_cartridge[state->m_current_cartridge].cartRAM[offset & (state->m_cartridge[state->m_current_cartridge].ram_size - 1)] = data;
1092      }
1093      else
1094      {
1095         logerror("INVALID write %02X to cartram at offset #%04X\n", data, offset);
1096      }
1097   }
1098}
1099 
1100 
1101WRITE8_HANDLER( gg_sio_w )
1102{
1103   sms_state *state = space->machine().driver_data<sms_state>();
1104   logerror("*** write %02X to SIO register #%d\n", data, offset);
1105 
1106   state->m_gg_sio[offset & 0x07] = data;
1107   switch (offset & 7)
1108   {
1109      case 0x00: /* Parallel Data */
1110         break;
1111 
1112      case 0x01: /* Data Direction/ NMI Enable */
1113         break;
1114 
1115      case 0x02: /* Serial Output */
1116         break;
1117 
1118      case 0x03: /* Serial Input */
1119         break;
1120 
1121      case 0x04: /* Serial Control / Status */
1122         break;
1123   }
1124}
1125 
1126 
1127READ8_HANDLER( gg_sio_r )
1128{
1129   sms_state *state = space->machine().driver_data<sms_state>();
1130   logerror("*** read SIO register #%d\n", offset);
1131 
1132   switch (offset & 7)
1133   {
1134      case 0x00: /* Parallel Data */
1135         break;
1136 
1137      case 0x01: /* Data Direction/ NMI Enable */
1138         break;
1139 
1140      case 0x02: /* Serial Output */
1141         break;
1142 
1143      case 0x03: /* Serial Input */
1144         break;
1145 
1146      case 0x04: /* Serial Control / Status */
1147         break;
1148   }
1149 
1150   return state->m_gg_sio[offset];
1151}
1152 
1153static void sms_machine_stop( running_machine &machine )
1154{
1155   sms_state *state = machine.driver_data<sms_state>();
1156 
1157   /* Does the cartridge have SRAM that should be saved? */
1158   if (state->m_cartridge[state->m_current_cartridge].sram_save) {
1159      device_image_interface *image = dynamic_cast<device_image_interface *>(machine.device("cart1"));
1160      image->battery_save(state->m_cartridge[state->m_current_cartridge].cartSRAM, sizeof(UINT8) * NVRAM_SIZE );
1161   }
1162}
1163 
1164 
1165static void setup_rom( address_space *space )
1166{
1167   sms_state *state = space->machine().driver_data<sms_state>();
1168   running_machine &machine = space->machine();
1169 
1170   /* 1. set up bank pointers to point to nothing */
1171   memory_set_bankptr(machine, "bank1", state->m_banking_none[1]);
1172   memory_set_bankptr(machine, "bank2", state->m_banking_none[2]);
1173   memory_set_bankptr(machine, "bank3", state->m_banking_none[3]);
1174   memory_set_bankptr(machine, "bank4", state->m_banking_none[3] + 0x2000);
1175   memory_set_bankptr(machine, "bank5", state->m_banking_none[5]);
1176   memory_set_bankptr(machine, "bank6", state->m_banking_none[5] + 0x2000);
1177 
1178   /* 2. check and set up expansion port */
1179   if (!(state->m_bios_port & IO_EXPANSION) && (state->m_bios_port & IO_CARTRIDGE) && (state->m_bios_port & IO_CARD))
1180   {
1181      /* TODO: Implement me */
1182      logerror("Switching to unsupported expansion port.\n");
1183   }
1184 
1185   /* 3. check and set up card rom */
1186   if (!(state->m_bios_port & IO_CARD) && (state->m_bios_port & IO_CARTRIDGE) && (state->m_bios_port & IO_EXPANSION))
1187   {
1188      /* TODO: Implement me */
1189      logerror("Switching to unsupported card rom port.\n");
1190   }
1191 
1192   /* 4. check and set up cartridge rom */
1193   /* if ((!(bios_port & IO_CARTRIDGE) && (bios_port & IO_EXPANSION) && (bios_port & IO_CARD)) || state->m_is_gamegear) { */
1194   /* Out Run Europa initially writes a value to port 3E where IO_CARTRIDGE, IO_EXPANSION and IO_CARD are reset */
1195   if ((!(state->m_bios_port & IO_CARTRIDGE)) || state->m_is_gamegear)
1196   {
1197      memory_set_bankptr(machine, "bank1", state->m_banking_cart[1]);
1198      memory_set_bankptr(machine, "bank2", state->m_banking_cart[2]);
1199      memory_set_bankptr(machine, "bank3", state->m_banking_cart[3]);
1200      memory_set_bankptr(machine, "bank4", state->m_banking_cart[3] + 0x2000);
1201      memory_set_bankptr(machine, "bank5", state->m_banking_cart[5]);
1202      memory_set_bankptr(machine, "bank6", state->m_banking_cart[5] + 0x2000);
1203      logerror("Switched in cartridge rom.\n");
1204   }
1205 
1206   /* 5. check and set up bios rom */
1207   if (!(state->m_bios_port & IO_BIOS_ROM))
1208   {
1209      /* 0x0400 bioses */
1210      if (state->m_has_bios_0400)
1211      {
1212         memory_set_bankptr(machine, "bank1", state->m_banking_bios[1]);
1213         logerror("Switched in 0x0400 bios.\n");
1214      }
1215      /* 0x2000 bioses */
1216      if (state->m_has_bios_2000)
1217      {
1218         memory_set_bankptr(machine, "bank1", state->m_banking_bios[1]);
1219         memory_set_bankptr(machine, "bank2", state->m_banking_bios[2]);
1220         logerror("Switched in 0x2000 bios.\n");
1221      }
1222      if (state->m_has_bios_full)
1223      {
1224         memory_set_bankptr(machine, "bank1", state->m_banking_bios[1]);
1225         memory_set_bankptr(machine, "bank2", state->m_banking_bios[2]);
1226         memory_set_bankptr(machine, "bank3", state->m_banking_bios[3]);
1227         memory_set_bankptr(machine, "bank4", state->m_banking_bios[3] + 0x2000);
1228         memory_set_bankptr(machine, "bank5", state->m_banking_bios[5]);
1229         memory_set_bankptr(machine, "bank6", state->m_banking_bios[5] + 0x2000);
1230         logerror("Switched in full bios.\n");
1231      }
1232   }
1233 
1234   if (state->m_cartridge[state->m_current_cartridge].features & CF_ONCART_RAM)
1235   {
1236      memory_set_bankptr(machine, "bank5", state->m_cartridge[state->m_current_cartridge].cartRAM);
1237      memory_set_bankptr(machine, "bank6", state->m_cartridge[state->m_current_cartridge].cartRAM);
1238   }
1239}
1240 
1241 
1242static int sms_verify_cart( UINT8 *magic, int size )
1243{
1244   int retval;
1245 
1246   retval = IMAGE_VERIFY_FAIL;
1247 
1248   /* Verify the file is a valid image - check $7ff0 for "TMR SEGA" */
1249   if (size >= 0x8000)
1250   {
1251      if (!strncmp((char*)&magic[0x7ff0], "TMR SEGA", 8))
1252      {
1253#if 0
1254         /* Technically, it should be this, but remove for now until verified: */
1255         if (!strcmp(sysname, "gamegear"))
1256         {
1257            if ((unsigned char)magic[0x7ffd] < 0x50)
1258               retval = IMAGE_VERIFY_PASS;
1259         }
1260         if (!strcmp(sysname, "sms"))
1261         {
1262            if ((unsigned char)magic[0x7ffd] >= 0x50)
1263               retval = IMAGE_VERIFY_PASS;
1264         }
1265#endif
1266         retval = IMAGE_VERIFY_PASS;
1267      }
1268 
1269#if 0
1270      /* Check at $81f0 also */
1271      if (!retval)
1272      {
1273         if (!strncmp(&magic[0x81f0], "TMR SEGA", 8))
1274         {
1275#if 0
1276            /* Technically, it should be this, but remove for now until verified: */
1277            if (!strcmp(sysname, "gamegear"))
1278            {
1279               if ((unsigned char)magic[0x81fd] < 0x50)
1280                  retval = IMAGE_VERIFY_PASS;
1281            }
1282            if (!strcmp(sysname, "sms"))
1283            {
1284               if ((unsigned char)magic[0x81fd] >= 0x50)
1285                  retval = IMAGE_VERIFY_PASS;
1286            }
1287#endif
1288            retval = IMAGE_VERIFY_PASS;
1289         }
1290      }
1291#endif
1292 
1293   }
1294 
1295   return retval;
1296}
1297 
1298#ifdef UNUSED_FUNCTION
1299// For the moment we switch to a different detection routine which allows to detect
1300// in a single run Codemasters mapper, Korean mapper (including Jang Pung 3 which
1301// uses a diff signature then the one below here) and Zemina mapper (used by Wonsiin, etc.).
1302// I leave these here to document alt. detection routines and in the case these functions
1303// can be updated
1304 
1305/* Check for Codemasters mapper
1306  0x7FE3 - 93 - sms Cosmis Spacehead
1307              - sms Dinobasher
1308              - sms The Excellent Dizzy Collection
1309              - sms Fantastic Dizzy
1310              - sms Micro Machines
1311              - gamegear Cosmic Spacehead
1312              - gamegear Micro Machines
1313         - 94 - gamegear Dropzone
1314              - gamegear Ernie Els Golf (also has 64KB additional RAM on the cartridge)
1315              - gamegear Pete Sampras Tennis
1316              - gamegear S.S. Lucifer
1317         - 95 - gamegear Micro Machines 2 - Turbo Tournament
1318 
1319The Korean game Jang Pung II also seems to use a codemasters style mapper.
1320 */
1321static int detect_codemasters_mapper( UINT8 *rom )
1322{
1323   static const UINT8 jang_pung2[16] = { 0x00, 0xba, 0x38, 0x0d, 0x00, 0xb8, 0x38, 0x0c, 0x00, 0xb6, 0x38, 0x0b, 0x00, 0xb4, 0x38, 0x0a };
1324 
1325   if (((rom[0x7fe0] & 0x0f ) <= 9) && (rom[0x7fe3] == 0x93 || rom[0x7fe3] == 0x94 || rom[0x7fe3] == 0x95) &&  rom[0x7fef] == 0x00)
1326      return 1;
1327 
1328   if (!memcmp(&rom[0x7ff0], jang_pung2, 16))
1329      return 1;
1330 
1331   return 0;
1332}
1333 
1334 
1335static int detect_korean_mapper( UINT8 *rom )
1336{
1337   static const UINT8 signatures[2][16] =
1338   {
1339      { 0x3e, 0x11, 0x32, 0x00, 0xa0, 0x78, 0xcd, 0x84, 0x85, 0x3e, 0x02, 0x32, 0x00, 0xa0, 0xc9, 0xff }, /* Dodgeball King */
1340      { 0x41, 0x48, 0x37, 0x37, 0x44, 0x37, 0x4e, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20 },   /* Sangokushi 3 */
1341   };
1342   int i;
1343 
1344   for (i = 0; i < 2; i++)
1345   {
1346      if (!memcmp(&rom[0x7ff0], signatures[i], 16))
1347      {
1348         return 1;
1349      }
1350   }
1351   return 0;
1352}
1353#endif
1354 
1355 
1356static int detect_tvdraw( UINT8 *rom )
1357{
1358   static const UINT8 terebi_oekaki[7] = { 0x61, 0x6e, 0x6e, 0x61, 0x6b, 0x6d, 0x6e };   // "annakmn"
1359 
1360   if (!memcmp(&rom[0x13b3], terebi_oekaki, 7))
1361      return 1;
1362 
1363   return 0;
1364}
1365 
1366 
1367static int detect_lphaser_xoffset( running_machine &machine, UINT8 *rom )
1368{
1369   sms_state *state = machine.driver_data<sms_state>();
1370 
1371   static const UINT8 signatures[6][16] =
1372   {
1373      /* Spacegun */
1374      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0xff, 0xff, 0x9d, 0x99, 0x10, 0x90, 0x00, 0x40 },
1375      /* Gangster Town */
1376      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0x19, 0x87, 0x1b, 0xc9, 0x74, 0x50, 0x00, 0x4f },
1377      /* Shooting Gallery */
1378      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0x20, 0x20, 0x8a, 0x3a, 0x72, 0x50, 0x00, 0x4f },
1379      /* Rescue Mission */
1380      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0x20, 0x20, 0xfb, 0xd3, 0x06, 0x51, 0x00, 0x4f },
1381      /* Laser Ghost */
1382      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0x00, 0x00, 0xb7, 0x55, 0x74, 0x70, 0x00, 0x40 },
1383      /* Assault City */
1384      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0xff, 0xff, 0x9f, 0x74, 0x34, 0x70, 0x00, 0x40 },
1385   };
1386 
1387   if (!(state->m_bios_port & IO_CARTRIDGE) && state->m_cartridge[state->m_current_cartridge].size >= 0x8000)
1388   {
1389      if (!memcmp(&rom[0x7ff0], signatures[0], 16) || !memcmp(&rom[0x7ff0], signatures[1], 16))
1390         return 40;
1391 
1392      if (!memcmp(&rom[0x7ff0], signatures[2], 16))
1393         return 49;
1394 
1395      if (!memcmp(&rom[0x7ff0], signatures[3], 16))
1396         return 47;
1397 
1398      if (!memcmp(&rom[0x7ff0], signatures[4], 16))
1399         return 44;
1400 
1401      if (!memcmp(&rom[0x7ff0], signatures[5], 16))
1402         return 53;
1403 
1404   }
1405   return 50;
1406}
1407 
1408 
1409DEVICE_START( sms_cart )
1410{
1411   sms_state *state = device->machine().driver_data<sms_state>();
1412   int i;
1413 
1414   for (i = 0; i < MAX_CARTRIDGES; i++)
1415   {
1416      state->m_cartridge[i].ROM = NULL;
1417      state->m_cartridge[i].size = 0;
1418      state->m_cartridge[i].features = 0;
1419      state->m_cartridge[i].cartSRAM = NULL;
1420      state->m_cartridge[i].sram_save = 0;
1421      state->m_cartridge[i].cartRAM = NULL;
1422      state->m_cartridge[i].ram_size = 0;
1423      state->m_cartridge[i].ram_page = 0;
1424   }
1425   state->m_current_cartridge = 0;
1426 
1427   state->m_bios_port = (IO_EXPANSION | IO_CARTRIDGE | IO_CARD);
1428   if (!state->m_is_gamegear && !state->m_has_bios)
1429   {
1430      state->m_bios_port &= ~(IO_CARTRIDGE);
1431      state->m_bios_port |= IO_BIOS_ROM;
1432   }
1433}
1434 
1435 
1436DEVICE_IMAGE_LOAD( sms_cart )
1437{
1438   running_machine &machine = image.device().machine();
1439   sms_state *state = machine.driver_data<sms_state>();
1440   int size, index = 0, offset = 0;
1441   const char *extrainfo = NULL;
1442 
1443   if (strcmp(image.device().tag(), "cart1") == 0)
1444      index = 0;
1445   if (strcmp(image.device().tag(), "cart2") == 0)
1446      index = 1;
1447   if (strcmp(image.device().tag(), "cart3") == 0)
1448      index = 2;
1449   if (strcmp(image.device().tag(), "cart4") == 0)
1450      index = 3;
1451   if (strcmp(image.device().tag(), "cart5") == 0)
1452      index = 4;
1453   if (strcmp(image.device().tag(), "cart6") == 0)
1454      index = 5;
1455   if (strcmp(image.device().tag(), "cart7") == 0)
1456      index = 6;
1457   if (strcmp(image.device().tag(), "cart8") == 0)
1458      index = 7;
1459   if (strcmp(image.device().tag(), "cart9") == 0)
1460      index = 8;
1461   if (strcmp(image.device().tag(), "cart10") == 0)
1462      index = 9;
1463   if (strcmp(image.device().tag(), "cart11") == 0)
1464      index = 10;
1465   if (strcmp(image.device().tag(), "cart12") == 0)
1466      index = 11;
1467   if (strcmp(image.device().tag(), "cart13") == 0)
1468      index = 12;
1469   if (strcmp(image.device().tag(), "cart14") == 0)
1470      index = 13;
1471   if (strcmp(image.device().tag(), "cart15") == 0)
1472      index = 14;
1473   if (strcmp(image.device().tag(), "cart16") == 0)
1474      index = 15;
1475 
1476   state->m_cartridge[index].features = 0;
1477 
1478   if (image.software_entry() == NULL)
1479   {
1480      size = image.length();
1481      extrainfo = hashfile_extrainfo(image);
1482   }
1483   else
1484      size = image.get_software_region_length("rom");
1485 
1486   /* Check for 512-byte header */
1487   if ((size / 512) & 1)
1488   {
1489      offset = 512;
1490      size -= 512;
1491   }
1492 
1493   if (!size)
1494   {
1495      image.seterror(IMAGE_ERROR_UNSPECIFIED, "Invalid ROM image: ROM image is too small");
1496      return IMAGE_INIT_FAIL;
1497   }
1498 
1499   /* Create a new memory region to hold the ROM. */
1500   /* Make sure the region holds only complete (0x4000) rom banks */
1501   state->m_cartridge[index].size = (size & 0x3fff) ? (((size >> 14) + 1) << 14) : size;
1502   state->m_cartridge[index].ROM = auto_alloc_array(machine, UINT8, state->m_cartridge[index].size);
1503   state->m_cartridge[index].cartSRAM = auto_alloc_array(machine, UINT8, NVRAM_SIZE);
1504 
1505   /* Load ROM banks */
1506   if (image.software_entry() == NULL)
1507   {
1508      image.fseek(offset, SEEK_SET);
1509 
1510      if (image.fread( state->m_cartridge[index].ROM, size) != size)
1511         return IMAGE_INIT_FAIL;
1512   }
1513   else
1514   {
1515      memcpy(state->m_cartridge[index].ROM, image.get_software_region("rom") + offset, size);
1516 
1517      // check for mapper feature (currently only KOREAN_NOBANK, but eventually we will add the other mappers as well)
1518      if (image.get_feature("pcb") != NULL)
1519      {
1520         const char *mapper = image.get_feature("pcb");
1521         if (!strcmp(mapper, "korean_nobank"))
1522            state->m_cartridge[index].features |= CF_KOREAN_NOBANK_MAPPER;
1523      }
1524   }
1525 
1526   /* check the image */
1527   if (!state->m_has_bios)
1528   {
1529      if (sms_verify_cart(state->m_cartridge[index].ROM, size) == IMAGE_VERIFY_FAIL)
1530      {
1531         logerror("Warning loading image: sms_verify_cart failed\n");
1532      }
1533   }
1534 
1535   /* Detect special features from the extrainfo field */
1536   if (extrainfo)
1537   {
1538      /* Check for codemasters mapper */
1539      if (strstr(extrainfo, "CODEMASTERS"))
1540         state->m_cartridge[index].features |= CF_CODEMASTERS_MAPPER;
1541 
1542      /* Check for korean mapper */
1543      if (strstr(extrainfo, "KOREAN"))
1544         state->m_cartridge[index].features |= CF_KOREAN_MAPPER;
1545 
1546      /* Check for special SMS Compatibility mode gamegear cartridges */
1547      if (state->m_is_gamegear && strstr(extrainfo, "GGSMS"))
1548         state->m_cartridge[index].features |= CF_GG_SMS_MODE;
1549 
1550      /* Check for 93C46 eeprom */
1551      if (strstr(extrainfo, "93C46"))
1552         state->m_cartridge[index].features |= CF_93C46_EEPROM;
1553 
1554      /* Check for 8KB on-cart RAM */
1555      if (strstr(extrainfo, "8KB_CART_RAM"))
1556      {
1557         state->m_cartridge[index].features |= CF_ONCART_RAM;
1558         state->m_cartridge[index].ram_size = 0x2000;
1559         state->m_cartridge[index].cartRAM = auto_alloc_array(machine, UINT8, state->m_cartridge[index].ram_size);
1560      }
1561   }
1562   else
1563   {
1564      /* If no extrainfo information is available try to find special information out on our own */
1565      /* Check for special cartridge features (new routine, courtesy of Omar Cornut, from MEKA)  */
1566      if (size >= 0x8000)
1567      {
1568         int c0002 = 0, c8000 = 0, cA000 = 0, cFFFF = 0, i;
1569         for (i = 0; i < 0x8000; i++)
1570         {
1571            if (state->m_cartridge[index].ROM[i] == 0x32) // Z80 opcode for: LD (xxxx), A
1572            {
1573               UINT16 addr = (state->m_cartridge[index].ROM[i + 2] << 8) | state->m_cartridge[index].ROM[i + 1];
1574               if (addr == 0xFFFF)
1575               { i += 2; cFFFF++; continue; }
1576               if (addr == 0x0002 || addr == 0x0003 || addr == 0x0004)
1577               { i += 2; c0002++; continue; }
1578               if (addr == 0x8000)
1579               { i += 2; c8000++; continue; }
1580               if (addr == 0xA000)
1581               { i += 2; cA000++; continue; }
1582            }
1583         }
1584 
1585         LOG(("Mapper test: c002 = %d, c8000 = %d, cA000 = %d, cFFFF = %d\n", c0002, c8000, cA000, cFFFF));
1586 
1587         // 2 is a security measure, although tests on existing ROM showed it was not needed
1588         if (c0002 > cFFFF + 2 || (c0002 > 0 && cFFFF == 0))
1589            state->m_cartridge[index].features |= CF_KOREAN_ZEMINA_MAPPER;
1590         else if (c8000 > cFFFF + 2 || (c8000 > 0 && cFFFF == 0))
1591            state->m_cartridge[index].features |= CF_CODEMASTERS_MAPPER;
1592         else if (cA000 > cFFFF + 2 || (cA000 > 0 && cFFFF == 0))
1593            state->m_cartridge[index].features |= CF_KOREAN_MAPPER;
1594 
1595      }
1596 
1597      /* Check for special SMS Compatibility mode gamegear cartridges */
1598      if (state->m_is_gamegear && image.software_entry() == NULL)   // not sure about how to handle this with softlists
1599      {
1600         /* Just in case someone passes us an sms file */
1601         if (!mame_stricmp (image.filetype(), "sms"))
1602            state->m_cartridge[index].features |= CF_GG_SMS_MODE;
1603      }
1604   }
1605 
1606   if (state->m_cartridge[index].features & CF_CODEMASTERS_MAPPER)
1607   {
1608      state->m_cartridge[index].ram_size = 0x10000;
1609      state->m_cartridge[index].cartRAM = auto_alloc_array(machine, UINT8, state->m_cartridge[index].ram_size);
1610      state->m_cartridge[index].ram_page = 0;
1611   }
1612 
1613   /* For Light Phaser games, we have to detect the x offset */
1614   state->m_lphaser_x_offs = detect_lphaser_xoffset(machine, state->m_cartridge[index].ROM);
1615 
1616   /* Terebi Oekaki (TV Draw) is a SG1000 game with special input device which is compatible with SG1000 Mark III */
1617   if ((detect_tvdraw(state->m_cartridge[index].ROM)) && state->m_is_region_japan)
1618   {
1619      address_space *program = machine.device("maincpu")->memory().space(AS_PROGRAM);
1620      program->install_legacy_write_handler(0x6000, 0x6000, FUNC(sms_tvdraw_axis_w));
1621      program->install_legacy_read_handler(0x8000, 0x8000, FUNC(sms_tvdraw_status_r));
1622      program->install_legacy_read_handler(0xa000, 0xa000, FUNC(sms_tvdraw_data_r));
1623      program->nop_write(0xa000, 0xa000);
1624   }
1625 
1626   if (state->m_cartridge[index].features & CF_KOREAN_NOBANK_MAPPER)
1627   {
1628      // FIXME: we should have by default FFFD-FFFF to be only a mirror for DFFD-DFFF (with no bankswitch logic)
1629      // and then the handlers should be installed here for all but the KOREAN_NOBANK carts
1630      // However, to avoid memory map breakage, we currently go the other way around
1631      address_space *program = machine.device("maincpu")->memory().space(AS_PROGRAM);
1632      program->install_legacy_readwrite_handler(0xfffc, 0xffff, FUNC(sms_kor_nobank_r), FUNC(sms_kor_nobank_w));
1633   }
1634 
1635   LOG(("Cart Features: %x\n", state->m_cartridge[index].features));
1636 
1637   /* Load battery backed RAM, if available */
1638   image.battery_load(state->m_cartridge[index].cartSRAM, sizeof(UINT8) * NVRAM_SIZE, 0x00);
1639 
1640   return IMAGE_INIT_PASS;
1641}
1642 
1643 
1644static void setup_cart_banks( running_machine &machine )
1645{
1646   sms_state *state = machine.driver_data<sms_state>();
1647   if (state->m_cartridge[state->m_current_cartridge].ROM)
1648   {
1649      UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
1650      state->m_banking_cart[1] = state->m_cartridge[state->m_current_cartridge].ROM;
1651      state->m_banking_cart[2] = state->m_cartridge[state->m_current_cartridge].ROM + 0x0400;
1652      state->m_banking_cart[3] = state->m_cartridge[state->m_current_cartridge].ROM + ((1 < rom_page_count) ? 0x4000 : 0);
1653      state->m_banking_cart[5] = state->m_cartridge[state->m_current_cartridge].ROM + ((2 < rom_page_count) ? 0x8000 : 0);
1654      /* Codemasters mapper points to bank 0 for page 2 */
1655      if (state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
1656      {
1657         state->m_banking_cart[5] = state->m_cartridge[state->m_current_cartridge].ROM;
1658      }
1659   }
1660   else
1661   {
1662      state->m_banking_cart[1] = state->m_banking_none[1];
1663      state->m_banking_cart[2] = state->m_banking_none[2];
1664      state->m_banking_cart[3] = state->m_banking_none[3];
1665      state->m_banking_cart[5] = state->m_banking_none[5];
1666   }
1667}
1668 
1669#ifdef MESS
1670static void setup_banks( running_machine &machine )
1671{
1672   sms_state *state = machine.driver_data<sms_state>();
1673   UINT8 *mem = machine.region("maincpu")->base();
1674   state->m_banking_bios[1] = state->m_banking_cart[1] = state->m_banking_none[1] = mem;
1675   state->m_banking_bios[2] = state->m_banking_cart[2] = state->m_banking_none[2] = mem;
1676   state->m_banking_bios[3] = state->m_banking_cart[3] = state->m_banking_none[3] = mem;
1677   state->m_banking_bios[4] = state->m_banking_cart[4] = state->m_banking_none[4] = mem;
1678   state->m_banking_bios[5] = state->m_banking_cart[5] = state->m_banking_none[5] = mem;
1679   state->m_banking_bios[6] = state->m_banking_cart[6] = state->m_banking_none[6] = mem;
1680 
1681   state->m_BIOS = machine.region("user1")->base();
1682 
1683   state->m_bios_page_count = (state->m_BIOS ? machine.region("user1")->bytes() / 0x4000 : 0);
1684 
1685   setup_cart_banks(machine);
1686 
1687   if (state->m_BIOS == NULL || state->m_BIOS[0] == 0x00)
1688   {
1689      state->m_BIOS = NULL;
1690      state->m_bios_port |= IO_BIOS_ROM;
1691   }
1692 
1693   if (state->m_BIOS)
1694   {
1695      state->m_banking_bios[1] = state->m_BIOS;
1696      state->m_banking_bios[2] = state->m_BIOS + 0x0400;
1697      state->m_banking_bios[3] = state->m_BIOS + ((1 < state->m_bios_page_count) ? 0x4000 : 0);
1698      state->m_banking_bios[5] = state->m_BIOS + ((2 < state->m_bios_page_count) ? 0x8000 : 0);
1699   }
1700}
1701#endif
1702 
1703MACHINE_START( sms )
1704{
1705   sms_state *state = machine.driver_data<sms_state>();
1706 
1707   machine.add_notifier(MACHINE_NOTIFY_EXIT, machine_notify_delegate(FUNC(sms_machine_stop),&machine));
1708   state->m_rapid_fire_timer = machine.scheduler().timer_alloc(FUNC(rapid_fire_callback));
1709   state->m_rapid_fire_timer->adjust(attotime::from_hz(10), 0, attotime::from_hz(10));
1710 
1711   state->m_lphaser_1_timer = machine.scheduler().timer_alloc(FUNC(lphaser_1_callback));
1712   state->m_lphaser_2_timer = machine.scheduler().timer_alloc(FUNC(lphaser_2_callback));
1713 
1714   state->m_main_cpu = machine.device("maincpu");
1715   state->m_control_cpu = machine.device("control");
1716   state->m_vdp = machine.device("sms_vdp");
1717   state->m_ym = machine.device("ym2413");
1718   state->m_main_scr = machine.device("screen");
1719   state->m_left_lcd = machine.device("left_lcd");
1720   state->m_right_lcd = machine.device("right_lcd");
1721 
1722   /* Check if lightgun has been chosen as input: if so, enable crosshair */
1723   machine.scheduler().timer_set(attotime::zero, FUNC(lightgun_tick));
1724}
1725 
1726#ifdef MESS
1727MACHINE_RESET( sms )
1728{
1729   address_space *space = machine.device("maincpu")->memory().space(AS_PROGRAM);
1730   sms_state *state = machine.driver_data<sms_state>();
1731 
1732   state->m_ctrl_reg = 0xff;
1733   if (state->m_has_fm)
1734      state->m_fm_detect = 0x01;
1735 
1736   state->m_mapper_ram = (UINT8*)space->get_write_ptr(0xdffc);
1737 
1738   state->m_bios_port = 0;
1739 
1740   if (state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
1741   {
1742      /* Install special memory handlers */
1743      space->install_legacy_write_handler(0x0000, 0x0000, FUNC(sms_codemasters_page0_w));
1744      space->install_legacy_write_handler(0x4000, 0x4000, FUNC(sms_codemasters_page1_w));
1745   }
1746 
1747   if (state->m_cartridge[state->m_current_cartridge].features & CF_KOREAN_ZEMINA_MAPPER)
1748      space->install_legacy_write_handler(0x0000, 0x0003, FUNC(sms_korean_zemina_banksw_w));
1749 
1750   if (state->m_cartridge[state->m_current_cartridge].features & CF_GG_SMS_MODE)
1751      sms_vdp_set_ggsmsmode(state->m_vdp, 1);
1752 
1753   /* Initialize SIO stuff for GG */
1754   state->m_gg_sio[0] = 0x7f;
1755   state->m_gg_sio[1] = 0xff;
1756   state->m_gg_sio[2] = 0x00;
1757   state->m_gg_sio[3] = 0xff;
1758   state->m_gg_sio[4] = 0x00;
1759 
1760   state->m_store_control = 0;
1761 
1762   setup_banks(machine);
1763 
1764   setup_rom(space);
1765 
1766   state->m_rapid_fire_state_1 = 0;
1767   state->m_rapid_fire_state_2 = 0;
1768 
1769   state->m_last_paddle_read_time = 0;
1770   state->m_paddle_read_state = 0;
1771 
1772   state->m_last_sports_pad_time_1 = 0;
1773   state->m_last_sports_pad_time_2 = 0;
1774   state->m_sports_pad_state_1 = 0;
1775   state->m_sports_pad_state_2 = 0;
1776   state->m_sports_pad_last_data_1 = 0;
1777   state->m_sports_pad_last_data_2 = 0;
1778   state->m_sports_pad_1_x = 0;
1779   state->m_sports_pad_1_y = 0;
1780   state->m_sports_pad_2_x = 0;
1781   state->m_sports_pad_2_y = 0;
1782 
1783   state->m_lphaser_1_latch = 0;
1784   state->m_lphaser_2_latch = 0;
1785 
1786   state->m_sscope_state = 0;
1787 
1788   state->m_tvdraw_data = 0;
1789}
1790#endif
1791 
1792READ8_HANDLER( sms_store_cart_select_r )
1793{
1794   return 0xff;
1795}
1796 
1797 
1798WRITE8_HANDLER( sms_store_cart_select_w )
1799{
1800   sms_state *state = space->machine().driver_data<sms_state>();
1801   UINT8 slot = data >> 4;
1802   UINT8 slottype = data & 0x08;
1803 
1804   logerror("switching in part of %s slot #%d\n", slottype ? "card" : "cartridge", slot );
1805   /* cartridge? slot #0 */
1806   if (slottype == 0)
1807      state->m_current_cartridge = slot;
1808 
1809   setup_cart_banks(space->machine());
1810   memory_set_bankptr(space->machine(), "bank10", state->m_banking_cart[3] + 0x2000);
1811   setup_rom(space);
1812}
1813 
1814 
1815READ8_HANDLER( sms_store_select1 )
1816{
1817   return 0xff;
1818}
1819 
1820 
1821READ8_HANDLER( sms_store_select2 )
1822{
1823   return 0xff;
1824}
1825 
1826 
1827READ8_HANDLER( sms_store_control_r )
1828{
1829   sms_state *state = space->machine().driver_data<sms_state>();
1830   return state->m_store_control;
1831}
1832 
1833 
1834WRITE8_HANDLER( sms_store_control_w )
1835{
1836   sms_state *state = space->machine().driver_data<sms_state>();
1837   logerror("0x%04X: sms_store_control write 0x%02X\n", cpu_get_pc(&space->device()), data);
1838   if (data & 0x02)
1839   {
1840      space->machine().device<cpu_device>("maincpu")->resume(SUSPEND_REASON_HALT);
1841   }
1842   else
1843   {
1844      /* Pull reset line of CPU #0 low */
1845      space->machine().device("maincpu")->reset();
1846      space->machine().device<cpu_device>("maincpu")->suspend(SUSPEND_REASON_HALT, 1);
1847   }
1848   state->m_store_control = data;
1849}
1850 
1851void sms_store_int_callback( running_machine &machine, int state )
1852{
1853   sms_state *driver_state = machine.driver_data<sms_state>();
1854   device_set_input_line(driver_state->m_store_control & 0x01 ? driver_state->m_control_cpu : driver_state->m_main_cpu, 0, state);
1855}
1856 
1857 
1858static void sms_set_zero_flag( running_machine &machine )
1859{
1860   sms_state *state = machine.driver_data<sms_state>();
1861   state->m_is_gamegear = 0;
1862   state->m_is_region_japan = 0;
1863   state->m_has_bios_0400 = 0;
1864   state->m_has_bios_2000 = 0;
1865   state->m_has_bios_full = 0;
1866   state->m_has_bios = 0;
1867   state->m_has_fm = 0;
1868}
1869 
1870DRIVER_INIT( sg1000m3 )
1871{
1872   sms_state *state = machine.driver_data<sms_state>();
1873   sms_set_zero_flag(machine);
1874   state->m_is_region_japan = 1;
1875   state->m_has_fm = 1;
1876}
1877 
1878 
1879DRIVER_INIT( sms1 )
1880{
1881   sms_state *state = machine.driver_data<sms_state>();
1882   sms_set_zero_flag(machine);
1883   state->m_has_bios_full = 1;
1884}
1885 
1886 
1887DRIVER_INIT( smsj )
1888{
1889   sms_state *state = machine.driver_data<sms_state>();
1890   sms_set_zero_flag(machine);
1891   state->m_is_region_japan = 1;
1892   state->m_has_bios_2000 = 1;
1893   state->m_has_fm = 1;
1894}
1895 
1896 
1897DRIVER_INIT( sms2kr )
1898{
1899   sms_state *state = machine.driver_data<sms_state>();
1900   sms_set_zero_flag(machine);
1901   state->m_is_region_japan = 1;
1902   state->m_has_bios_full = 1;
1903   state->m_has_fm = 1;
1904}
1905 
1906 
1907DRIVER_INIT( smssdisp )
1908{
1909   sms_set_zero_flag(machine);
1910}
1911 
1912 
1913DRIVER_INIT( gamegear )
1914{
1915   sms_state *state = machine.driver_data<sms_state>();
1916   sms_set_zero_flag(machine);
1917   state->m_is_gamegear = 1;
1918}
1919 
1920 
1921DRIVER_INIT( gamegeaj )
1922{
1923   sms_state *state = machine.driver_data<sms_state>();
1924   sms_set_zero_flag(machine);
1925   state->m_is_region_japan = 1;
1926   state->m_is_gamegear = 1;
1927   state->m_has_bios_0400 = 1;
1928}
1929 
1930 
1931static void sms_black_bitmap( const screen_device *screen, bitmap_t *bitmap )
1932{
1933   const int width = screen->width();
1934   const int height = screen->height();
1935   int x, y;
1936 
1937   for (y = 0; y < height; y++)
1938      for (x = 0; x < width; x++)
1939         *BITMAP_ADDR32(bitmap, y, x) = MAKE_RGB(0,0,0);
1940}
1941 
1942VIDEO_START( sms1 )
1943{
1944   sms_state *state = machine.driver_data<sms_state>();
1945   screen_device *screen = machine.first_screen();
1946   int width = screen->width();
1947   int height = screen->height();
1948 
1949   state->m_prevleft_bitmap = auto_bitmap_alloc(machine, width, height, BITMAP_FORMAT_INDEXED32);
1950   state->m_prevright_bitmap = auto_bitmap_alloc(machine, width, height, BITMAP_FORMAT_INDEXED32);
1951   state->save_item(NAME(*state->m_prevleft_bitmap));
1952   state->save_item(NAME(*state->m_prevright_bitmap));
1953}
1954 
1955SCREEN_UPDATE( sms1 )
1956{
1957   sms_state *state = screen->machine().driver_data<sms_state>();
1958   UINT8 sscope = input_port_read_safe(screen->machine(), "SEGASCOPE", 0x00);
1959   UINT8 sscope_binocular_hack = input_port_read_safe(screen->machine(), "SSCOPE_BINOCULAR", 0x00);
1960   UINT8 occluded_view = 0;
1961 
1962   // without SegaScope, both LCDs for glasses go black
1963   if ((screen != state->m_main_scr) && !sscope)
1964      occluded_view = 1;
1965 
1966   // with SegaScope, sscope_state 0 = left screen OFF, right screen ON
1967   if (!(state->m_sscope_state & 0x01) && (screen == state->m_left_lcd))
1968      occluded_view = 1;
1969 
1970   // with SegaScope, sscope_state 1 = left screen ON, right screen OFF
1971   if ((state->m_sscope_state & 0x01) && (screen == state->m_right_lcd))
1972      occluded_view = 1;
1973 
1974   if (!occluded_view)
1975   {
1976      sms_vdp_update(state->m_vdp, bitmap, cliprect);
1977 
1978      // HACK: fake 3D->2D handling (if enabled, it repeats each frame twice on the selected lens)
1979      // save a copy of current bitmap for the binocular hack
1980      if (sscope && (screen == state->m_left_lcd) && (sscope_binocular_hack & 0x01))
1981         copybitmap(state->m_prevleft_bitmap, bitmap, 0, 0, 0, 0, cliprect);
1982      if (sscope && (screen == state->m_right_lcd) && (sscope_binocular_hack & 0x02))
1983         copybitmap(state->m_prevright_bitmap, bitmap, 0, 0, 0, 0, cliprect);
1984   }
1985   else
1986   {
1987      sms_black_bitmap(screen, bitmap);
1988 
1989      // HACK: fake 3D->2D handling (if enabled, it repeats each frame twice on the selected lens)
1990      // use the copied bitmap for the binocular hack
1991      if (sscope && (screen == state->m_left_lcd) && (sscope_binocular_hack & 0x01))
1992         copybitmap(bitmap, state->m_prevleft_bitmap, 0, 0, 0, 0, cliprect);
1993      if (sscope && (screen == state->m_right_lcd) && (sscope_binocular_hack & 0x02))
1994         copybitmap(bitmap, state->m_prevright_bitmap, 0, 0, 0, 0, cliprect);
1995   }
1996 
1997   return 0;
1998}
1999 
2000SCREEN_UPDATE( sms )
2001{
2002   sms_state *state = screen->machine().driver_data<sms_state>();
2003   sms_vdp_update(state->m_vdp, bitmap, cliprect);
2004   return 0;
2005}
2006 
2007VIDEO_START( gamegear )
2008{
2009   sms_state *state = machine.driver_data<sms_state>();
2010   screen_device *screen = machine.first_screen();
2011   int width = screen->width();
2012   int height = screen->height();
2013 
2014   state->m_prev_bitmap = auto_bitmap_alloc(machine, width, height, BITMAP_FORMAT_INDEXED32);
2015   state->m_tmp_bitmap = auto_bitmap_alloc(machine, width, height, BITMAP_FORMAT_INDEXED32);
2016   state->save_item(NAME(*state->m_prev_bitmap));
2017}
2018 
2019SCREEN_UPDATE( gamegear )
2020{
2021   sms_state *state = screen->machine().driver_data<sms_state>();
2022   int width = screen->width();
2023   int height = screen->height();
2024   int x, y;
2025 
2026   sms_vdp_update(state->m_vdp, state->m_tmp_bitmap, cliprect);
2027 
2028   // HACK: fake LCD persistence effect
2029   // (it would be better to generalize this in the core, to be used for all LCD systems)
2030   for (y = 0; y < height; y++)
2031   {
2032      UINT32 *line0 = BITMAP_ADDR32(state->m_tmp_bitmap, y, 0);
2033      UINT32 *line1 = BITMAP_ADDR32(state->m_prev_bitmap, y, 0);
2034      for (x = 0; x < width; x++)
2035      {
2036         UINT32 color0 = line0[x];
2037         UINT32 color1 = line1[x];
2038         UINT16 r0 = (color0 >> 16) & 0x000000ff;
2039         UINT16 g0 = (color0 >>  8) & 0x000000ff;
2040         UINT16 b0 = (color0 >>  0) & 0x000000ff;
2041         UINT16 r1 = (color1 >> 16) & 0x000000ff;
2042         UINT16 g1 = (color1 >>  8) & 0x000000ff;
2043         UINT16 b1 = (color1 >>  0) & 0x000000ff;
2044         UINT8 r = (UINT8)((r0 + r1) >> 1);
2045         UINT8 g = (UINT8)((g0 + g1) >> 1);
2046         UINT8 b = (UINT8)((b0 + b1) >> 1);
2047         *BITMAP_ADDR32(bitmap, y, x) = (r << 16) | (g << 8) | b;
2048      }
2049   }
2050   copybitmap(state->m_prev_bitmap, state->m_tmp_bitmap, 0, 0, 0, 0, cliprect);
2051 
2052   return 0;
2053}
1#include "emu.h"
2#include "crsshair.h"
3#include "image.h"
4#include "includes/segasms.h"
5#include "video/smsvdp.h"
6#include "sound/2413intf.h"
7#include "imagedev/cartslot.h"
8#include "hashfile.h"
9
10#define VERBOSE 0
11#define LOG(x) do { if (VERBOSE) logerror x; } while (0)
12
13#define CF_CODEMASTERS_MAPPER    0x01
14#define CF_KOREAN_MAPPER         0x02
15#define CF_KOREAN_ZEMINA_MAPPER  0x04
16#define CF_KOREAN_NOBANK_MAPPER  0x08
17#define CF_93C46_EEPROM          0x10
18#define CF_ONCART_RAM            0x20
19#define CF_GG_SMS_MODE           0x40
20
21#define LGUN_RADIUS           6
22#define LGUN_X_INTERVAL       4
23
24
25static void setup_rom(address_space *space);
26
27
28static TIMER_CALLBACK( rapid_fire_callback )
29{
30   sms_state *state = machine.driver_data<sms_state>();
31   state->m_rapid_fire_state_1 ^= 0xff;
32   state->m_rapid_fire_state_2 ^= 0xff;
33}
34
35
36static WRITE8_HANDLER( sms_input_write )
37{
38   sms_state *state = space->machine().driver_data<sms_state>();
39
40   switch (offset)
41   {
42   case 0:
43      switch (input_port_read_safe(space->machine(), "CTRLSEL", 0x00) & 0x0f)
44      {
45      case 0x03:   /* Sports Pad */
46         if (data != state->m_sports_pad_last_data_1)
47         {
48            UINT32 cpu_cycles = downcast<cpu_device *>(&space->device())->total_cycles();
49
50            state->m_sports_pad_last_data_1 = data;
51            if (cpu_cycles - state->m_last_sports_pad_time_1 > 512)
52            {
53               state->m_sports_pad_state_1 = 3;
54               state->m_sports_pad_1_x = input_port_read(space->machine(), "SPORT0");
55               state->m_sports_pad_1_y = input_port_read(space->machine(), "SPORT1");
56            }
57            state->m_last_sports_pad_time_1 = cpu_cycles;
58            state->m_sports_pad_state_1 = (state->m_sports_pad_state_1 + 1) & 3;
59         }
60         break;
61      }
62      break;
63
64   case 1:
65      switch (input_port_read_safe(space->machine(), "CTRLSEL", 0x00) >> 4)
66      {
67      case 0x03:   /* Sports Pad */
68         if (data != state->m_sports_pad_last_data_2)
69         {
70            UINT32 cpu_cycles = downcast<cpu_device *>(&space->device())->total_cycles();
71
72            state->m_sports_pad_last_data_2 = data;
73            if (cpu_cycles - state->m_last_sports_pad_time_2 > 2048)
74            {
75               state->m_sports_pad_state_2 = 3;
76               state->m_sports_pad_2_x = input_port_read(space->machine(), "SPORT2");
77               state->m_sports_pad_2_y = input_port_read(space->machine(), "SPORT3");
78            }
79            state->m_last_sports_pad_time_2 = cpu_cycles;
80            state->m_sports_pad_state_2 = (state->m_sports_pad_state_2 + 1) & 3;
81         }
82         break;
83      }
84      break;
85   }
86}
87
88/* FIXME: this function is a hack for Light Phaser emulation. Theoretically
89   sms_vdp_hcount_latch() should be used instead, but it returns incorrect
90   position for unknown reason (timing?) */
91static void sms_vdp_hcount_lphaser( running_machine &machine, int hpos )
92{
93   sms_state *state = machine.driver_data<sms_state>();
94   int hpos_tmp = hpos + state->m_lphaser_x_offs;
95   UINT8 tmp = ((hpos_tmp - 46) >> 1) & 0xff;
96
97   //printf ("sms_vdp_hcount_lphaser: hpos %3d hpos_tmp %3d => hcount %2X\n", hpos, hpos_tmp, tmp);
98   sms_vdp_hcount_latch_w(state->m_vdp, 0, tmp);
99}
100
101
102/*
103    Light Phaser (light gun) emulation notes:
104    - The sensor is activated based on color brightness of some individual
105      pixels being drawn by the beam, at circular area where the gun is aiming.
106    - Currently, brightness is calculated based only on single pixels.
107    - In general, after the trigger is pressed, games draw the next frame using
108      a light color pattern, to make sure sensor will be activated. If emulation
109      skips that frame, sensor may stay deactivated. Frameskip set to 0 (no skip) is
110      recommended to avoid problems.
111    - When sensor switches from off to on, a value is latched for HCount register
112      and a flag is set. The emulation uses the flag to avoid sequential latches and
113      to signal that TH line is activated when the status of the input port is read.
114      When the status is read, the flag is cleared, or else it is cleared later when
115      the Pause status is read (end of a frame). This is necessary because the
116      "Color & Switch Test" ROM only reads the TH bit after the VINT line.
117    - The gun test of "Color & Switch Test" is an example that requires checks
118      of sensor status independent of other events, like trigger press or TH bit
119      reads. Another example is the title screen of "Hang-On & Safari Hunt", where
120      the game only reads HCount register in a loop, expecting a latch by the gun.
121    - The whole procedure is managed by a timer callback, that always reschedule
122      itself to run in some intervals when the beam is at the circular area.
123*/
124static int lgun_bright_aim_area( running_machine &machine, emu_timer *timer, int lgun_x, int lgun_y )
125{
126   sms_state *state = machine.driver_data<sms_state>();
127   const int r_x_r = LGUN_RADIUS * LGUN_RADIUS;
128   screen_device *screen = machine.first_screen();
129   const rectangle &visarea = screen->visible_area();
130   int beam_x = screen->hpos();
131   int beam_y = screen->vpos();
132   int dx, dy;
133   int result = 0;
134   int pos_changed = 0;
135   double dx_circ;
136
137    while (1)
138   {
139      dy = abs(beam_y - lgun_y);
140
141      if (dy > LGUN_RADIUS || beam_y < visarea.min_y || beam_y > visarea.max_y)
142      {
143         beam_y = lgun_y - LGUN_RADIUS;
144         if (beam_y < visarea.min_y)
145            beam_y = visarea.min_y;
146         dy = abs(beam_y - lgun_y);
147         pos_changed = 1;
148      }
149      /* step 1: r^2 = dx^2 + dy^2 */
150      /* step 2: dx^2 = r^2 - dy^2 */
151      /* step 3: dx = sqrt(r^2 - dy^2) */
152      dx_circ = ceil((float) sqrt((float) (r_x_r - (dy * dy))));
153      dx = abs(beam_x - lgun_x);
154
155      if (dx > dx_circ || beam_x < visarea.min_x || beam_x > visarea.max_x)
156      {
157         if (beam_x > lgun_x)
158         {
159            beam_x = 0;
160            beam_y++;
161            continue;
162         }
163         beam_x = lgun_x - dx_circ;
164         if (beam_x < visarea.min_x)
165            beam_x = visarea.min_x;
166         pos_changed = 1;
167      }
168
169      if (!pos_changed)
170      {
171         result = sms_vdp_check_brightness(state->m_vdp, beam_x, beam_y);
172         /* next check at same line */
173         beam_x += LGUN_X_INTERVAL;
174         pos_changed = 1;
175      }
176      else
177         break;
178   }
179   timer->adjust(machine.first_screen()->time_until_pos(beam_y, beam_x));
180
181   return result;
182}
183
184static UINT8 sms_vdp_hcount( running_machine &machine )
185{
186   UINT8 tmp;
187   screen_device *screen = machine.first_screen();
188   int hpos = screen->hpos();
189
190   /* alternative method: pass HCounter test, but some others fail */
191   //int hpos_tmp = hpos;
192   //if ((hpos + 2) % 6 == 0) hpos_tmp--;
193   //tmp = ((hpos_tmp - 46) >> 1) & 0xff;
194
195   UINT64 calc_cycles;
196   attotime time_end;
197   int vpos = screen->vpos();
198   int max_hpos = screen->width() - 1;
199
200   if (hpos == max_hpos)
201      time_end = attotime::zero;
202   else
203      time_end = screen->time_until_pos(vpos, max_hpos);
204   calc_cycles = machine.device<cpu_device>("maincpu")->attotime_to_clocks(time_end);
205
206   /* equation got from SMSPower forum, posted by Flubba. */
207   tmp = ((590 - (calc_cycles * 3)) / 4) & 0xff;
208
209   //printf ("sms_vdp_hcount: hpos %3d => hcount %2X\n", hpos, tmp);
210   return tmp;
211}
212
213
214static void sms_vdp_hcount_latch( running_machine &machine )
215{
216   sms_state *state = machine.driver_data<sms_state>();
217   UINT8 value = sms_vdp_hcount(machine);
218
219   sms_vdp_hcount_latch_w(state->m_vdp, 0, value);
220}
221
222
223static UINT16 screen_hpos_nonscaled( screen_device *screen, int scaled_hpos )
224{
225   const rectangle &visarea = screen->visible_area();
226   int offset_x = (scaled_hpos * (visarea.max_x - visarea.min_x)) / 255;
227   return visarea.min_x + offset_x;
228}
229
230
231static UINT16 screen_vpos_nonscaled( screen_device *screen, int scaled_vpos )
232{
233   const rectangle &visarea = screen->visible_area();
234   int offset_y = (scaled_vpos * (visarea.max_y - visarea.min_y)) / 255;
235   return visarea.min_y + offset_y;
236}
237
238
239static void lphaser1_sensor_check( running_machine &machine )
240{
241   sms_state *state = machine.driver_data<sms_state>();
242   const int x = screen_hpos_nonscaled(machine.first_screen(), input_port_read(machine, "LPHASER0"));
243   const int y = screen_vpos_nonscaled(machine.first_screen(), input_port_read(machine, "LPHASER1"));
244
245   if (lgun_bright_aim_area(machine, state->m_lphaser_1_timer, x, y))
246   {
247      if (state->m_lphaser_1_latch == 0)
248      {
249         state->m_lphaser_1_latch = 1;
250         sms_vdp_hcount_lphaser(machine, x);
251      }
252   }
253}
254
255static void lphaser2_sensor_check( running_machine &machine )
256{
257   sms_state *state = machine.driver_data<sms_state>();
258   const int x = screen_hpos_nonscaled(machine.first_screen(), input_port_read(machine, "LPHASER2"));
259   const int y = screen_vpos_nonscaled(machine.first_screen(), input_port_read(machine, "LPHASER3"));
260
261   if (lgun_bright_aim_area(machine, state->m_lphaser_2_timer, x, y))
262   {
263      if (state->m_lphaser_2_latch == 0)
264      {
265         state->m_lphaser_2_latch = 1;
266         sms_vdp_hcount_lphaser(machine, x);
267      }
268   }
269}
270
271
272// at each input port read we check if lightguns are enabled in one of the ports:
273// if so, we turn on crosshair and the lightgun timer
274static TIMER_CALLBACK( lightgun_tick )
275{
276   sms_state *state = machine.driver_data<sms_state>();
277
278   if ((input_port_read_safe(machine, "CTRLSEL", 0x00) & 0x0f) == 0x01)
279   {
280      /* enable crosshair */
281      crosshair_set_screen(machine, 0, CROSSHAIR_SCREEN_ALL);
282      if (!state->m_lphaser_1_timer->enabled())
283         lphaser1_sensor_check(machine);
284   }
285   else
286   {
287      /* disable crosshair */
288      crosshair_set_screen(machine, 0, CROSSHAIR_SCREEN_NONE);
289      state->m_lphaser_1_timer->enable(0);
290   }
291
292   if ((input_port_read_safe(machine, "CTRLSEL", 0x00) & 0xf0) == 0x10)
293   {
294      /* enable crosshair */
295      crosshair_set_screen(machine, 1, CROSSHAIR_SCREEN_ALL);
296      if (!state->m_lphaser_2_timer->enabled())
297         lphaser2_sensor_check(machine);
298   }
299   else
300   {
301      /* disable crosshair */
302      crosshair_set_screen(machine, 1, CROSSHAIR_SCREEN_NONE);
303      state->m_lphaser_2_timer->enable(0);
304   }
305}
306
307
308static TIMER_CALLBACK( lphaser_1_callback )
309{
310   lphaser1_sensor_check(machine);
311}
312
313
314static TIMER_CALLBACK( lphaser_2_callback )
315{
316   lphaser2_sensor_check(machine);
317}
318
319
320INPUT_CHANGED( lgun1_changed )
321{
322   sms_state *state = field.machine().driver_data<sms_state>();
323   if (!state->m_lphaser_1_timer ||
324      (input_port_read_safe(field.machine(), "CTRLSEL", 0x00) & 0x0f) != 0x01)
325      return;
326
327   if (newval != oldval)
328      lphaser1_sensor_check(field.machine());
329}
330
331INPUT_CHANGED( lgun2_changed )
332{
333   sms_state *state = field.machine().driver_data<sms_state>();
334   if (!state->m_lphaser_2_timer ||
335      (input_port_read_safe(field.machine(), "CTRLSEL", 0x00) & 0xf0) != 0x10)
336      return;
337
338   if (newval != oldval)
339      lphaser2_sensor_check(field.machine());
340}
341
342
343static void sms_get_inputs( address_space *space )
344{
345   sms_state *state = space->machine().driver_data<sms_state>();
346   UINT8 data = 0x00;
347   UINT32 cpu_cycles = downcast<cpu_device *>(&space->device())->total_cycles();
348   running_machine &machine = space->machine();
349
350   state->m_input_port0 = 0xff;
351   state->m_input_port1 = 0xff;
352
353   if (cpu_cycles - state->m_last_paddle_read_time > 256)
354   {
355      state->m_paddle_read_state ^= 0xff;
356      state->m_last_paddle_read_time = cpu_cycles;
357   }
358
359   /* Check if lightgun has been chosen as input: if so, enable crosshair */
360   machine.scheduler().timer_set(attotime::zero, FUNC(lightgun_tick));
361
362   /* Player 1 */
363   switch (input_port_read_safe(machine, "CTRLSEL", 0x00) & 0x0f)
364   {
365   case 0x00:  /* Joystick */
366      data = input_port_read(machine, "PORT_DC");
367      /* Check Rapid Fire setting for Button A */
368      if (!(data & 0x10) && (input_port_read(machine, "RFU") & 0x01))
369         data |= state->m_rapid_fire_state_1 & 0x10;
370
371      /* Check Rapid Fire setting for Button B */
372      if (!(data & 0x20) && (input_port_read(machine, "RFU") & 0x02))
373         data |= state->m_rapid_fire_state_1 & 0x20;
374
375      state->m_input_port0 = (state->m_input_port0 & 0xc0) | (data & 0x3f);
376      break;
377
378   case 0x01:  /* Light Phaser */
379      data = (input_port_read(machine, "CTRLIPT") & 0x01) << 4;
380      if (!(data & 0x10))
381      {
382         if (input_port_read(machine, "RFU") & 0x01)
383            data |= state->m_rapid_fire_state_1 & 0x10;
384      }
385      /* just consider the button (trigger) bit */
386      data |= ~0x10;
387      state->m_input_port0 = (state->m_input_port0 & 0xc0) | (data & 0x3f);
388      break;
389
390   case 0x02:  /* Paddle Control */
391      /* Get button A state */
392      data = input_port_read(machine, "PADDLE0");
393
394      if (state->m_paddle_read_state)
395         data = data >> 4;
396
397      state->m_input_port0 = (state->m_input_port0 & 0xc0) | (data & 0x0f) | (state->m_paddle_read_state & 0x20)
398                      | ((input_port_read(machine, "CTRLIPT") & 0x02) << 3);
399      break;
400
401   case 0x03:   /* Sega Sports Pad */
402      switch (state->m_sports_pad_state_1)
403      {
404      case 0:
405         data = (state->m_sports_pad_1_x >> 4) & 0x0f;
406         break;
407      case 1:
408         data = state->m_sports_pad_1_x & 0x0f;
409         break;
410      case 2:
411         data = (state->m_sports_pad_1_y >> 4) & 0x0f;
412         break;
413      case 3:
414         data = state->m_sports_pad_1_y & 0x0f;
415         break;
416      }
417      state->m_input_port0 = (state->m_input_port0 & 0xc0) | data | ((input_port_read(machine, "CTRLIPT") & 0x0c) << 2);
418      break;
419   }
420
421   /* Player 2 */
422   switch (input_port_read_safe(machine, "CTRLSEL", 0x00)  & 0xf0)
423   {
424   case 0x00:   /* Joystick */
425      data = input_port_read(machine, "PORT_DC");
426      state->m_input_port0 = (state->m_input_port0 & 0x3f) | (data & 0xc0);
427
428      data = input_port_read(machine, "PORT_DD");
429      /* Check Rapid Fire setting for Button A */
430      if (!(data & 0x04) && (input_port_read(machine, "RFU") & 0x04))
431         data |= state->m_rapid_fire_state_2 & 0x04;
432
433      /* Check Rapid Fire setting for Button B */
434      if (!(data & 0x08) && (input_port_read(machine, "RFU") & 0x08))
435         data |= state->m_rapid_fire_state_2 & 0x08;
436
437      state->m_input_port1 = (state->m_input_port1 & 0xf0) | (data & 0x0f);
438      break;
439
440   case 0x10:   /* Light Phaser */
441      data = (input_port_read(machine, "CTRLIPT") & 0x10) >> 2;
442      if (!(data & 0x04))
443      {
444         if (input_port_read(machine, "RFU") & 0x04)
445            data |= state->m_rapid_fire_state_2 & 0x04;
446      }
447      /* just consider the button (trigger) bit */
448      data |= ~0x04;
449      state->m_input_port1 = (state->m_input_port1 & 0xf0) | (data & 0x0f);
450      break;
451
452   case 0x20:   /* Paddle Control */
453      /* Get button A state */
454      data = input_port_read(machine, "PADDLE1");
455      if (state->m_paddle_read_state)
456         data = data >> 4;
457
458      state->m_input_port0 = (state->m_input_port0 & 0x3f) | ((data & 0x03) << 6);
459      state->m_input_port1 = (state->m_input_port1 & 0xf0) | ((data & 0x0c) >> 2) | (state->m_paddle_read_state & 0x08)
460                      | ((input_port_read(machine, "CTRLIPT") & 0x20) >> 3);
461      break;
462
463   case 0x30:   /* Sega Sports Pad */
464      switch (state->m_sports_pad_state_2)
465      {
466      case 0:
467         data = state->m_sports_pad_2_x & 0x0f;
468         break;
469      case 1:
470         data = (state->m_sports_pad_2_x >> 4) & 0x0f;
471         break;
472      case 2:
473         data = state->m_sports_pad_2_y & 0x0f;
474         break;
475      case 3:
476         data = (state->m_sports_pad_2_y >> 4) & 0x0f;
477         break;
478      }
479      state->m_input_port0 = (state->m_input_port0 & 0x3f) | ((data & 0x03) << 6);
480      state->m_input_port1 = (state->m_input_port1 & 0xf0) | (data >> 2) | ((input_port_read(machine, "CTRLIPT") & 0xc0) >> 4);
481      break;
482   }
483}
484
485
486WRITE8_HANDLER( sms_fm_detect_w )
487{
488   sms_state *state = space->machine().driver_data<sms_state>();
489
490   if (state->m_has_fm)
491      state->m_fm_detect = (data & 0x01);
492}
493
494
495READ8_HANDLER( sms_fm_detect_r )
496{
497   sms_state *state = space->machine().driver_data<sms_state>();
498
499   if (state->m_has_fm)
500   {
501      return state->m_fm_detect;
502   }
503   else
504   {
505      if (state->m_bios_port & IO_CHIP)
506      {
507         return 0xff;
508      }
509      else
510      {
511         sms_get_inputs(space);
512         return state->m_input_port0;
513      }
514   }
515}
516
517WRITE8_HANDLER( sms_io_control_w )
518{
519   sms_state *state = space->machine().driver_data<sms_state>();
520
521   if (data & 0x08)
522   {
523      /* check if TH pin level is high (1) and was low last time */
524      if (data & 0x80 && !(state->m_ctrl_reg & 0x80))
525      {
526         sms_vdp_hcount_latch(space->machine());
527      }
528      sms_input_write(space, 0, (data & 0x20) >> 5);
529   }
530
531   if (data & 0x02)
532   {
533      if (data & 0x20 && !(state->m_ctrl_reg & 0x20))
534      {
535         sms_vdp_hcount_latch(space->machine());
536      }
537      sms_input_write(space, 1, (data & 0x80) >> 7);
538   }
539
540   state->m_ctrl_reg = data;
541}
542
543
544READ8_HANDLER( sms_count_r )
545{
546   sms_state *state = space->machine().driver_data<sms_state>();
547
548   if (offset & 0x01)
549      return sms_vdp_hcount_latch_r(state->m_vdp, offset);
550   else
551      return sms_vdp_vcount_r(state->m_vdp, offset);
552}
553
554
555/*
556 Check if the pause button is pressed.
557 If the gamegear is in sms mode, check if the start button is pressed.
558 */
559void sms_pause_callback( running_machine &machine )
560{
561   sms_state *state = machine.driver_data<sms_state>();
562
563   if (state->m_is_gamegear && !(state->m_cartridge[state->m_current_cartridge].features & CF_GG_SMS_MODE))
564      return;
565
566   if (!(input_port_read(machine, state->m_is_gamegear ? "START" : "PAUSE") & 0x80))
567   {
568      if (!state->m_paused)
569      {
570         cputag_set_input_line(machine, "maincpu", INPUT_LINE_NMI, PULSE_LINE);
571      }
572      state->m_paused = 1;
573   }
574   else
575   {
576      state->m_paused = 0;
577   }
578
579   /* clear Light Phaser latch flags for next frame */
580   state->m_lphaser_1_latch = 0;
581   state->m_lphaser_2_latch = 0;
582}
583
584READ8_HANDLER( sms_input_port_0_r )
585{
586   sms_state *state = space->machine().driver_data<sms_state>();
587
588   if (state->m_bios_port & IO_CHIP)
589   {
590      return 0xff;
591   }
592   else
593   {
594      sms_get_inputs(space);
595      return state->m_input_port0;
596   }
597}
598
599
600READ8_HANDLER( sms_input_port_1_r )
601{
602   sms_state *state = space->machine().driver_data<sms_state>();
603
604   if (state->m_bios_port & IO_CHIP)
605      return 0xff;
606
607   sms_get_inputs(space);
608
609   /* Reset Button */
610   state->m_input_port1 = (state->m_input_port1 & 0xef) | (input_port_read_safe(space->machine(), "RESET", 0x01) & 0x01) << 4;
611
612   /* Do region detection if TH of ports A and B are set to output (0) */
613   if (!(state->m_ctrl_reg & 0x0a))
614   {
615      /* Move bits 7,5 of IO control port into bits 7, 6 */
616      state->m_input_port1 = (state->m_input_port1 & 0x3f) | (state->m_ctrl_reg & 0x80) | (state->m_ctrl_reg & 0x20) << 1;
617
618      /* Inverse region detect value for Japanese machines */
619      if (state->m_is_region_japan)
620         state->m_input_port1 ^= 0xc0;
621   }
622   else
623   {
624      if (state->m_ctrl_reg & 0x02 && state->m_lphaser_1_latch)
625      {
626         state->m_input_port1 &= ~0x40;
627         state->m_lphaser_1_latch = 0;
628      }
629
630      if (state->m_ctrl_reg & 0x08 && state->m_lphaser_2_latch)
631      {
632         state->m_input_port1 &= ~0x80;
633         state->m_lphaser_2_latch = 0;
634      }
635   }
636
637   return state->m_input_port1;
638}
639
640
641
642WRITE8_HANDLER( sms_ym2413_register_port_0_w )
643{
644   sms_state *state = space->machine().driver_data<sms_state>();
645
646   if (state->m_has_fm)
647      ym2413_w(state->m_ym, 0, (data & 0x3f));
648}
649
650
651WRITE8_HANDLER( sms_ym2413_data_port_0_w )
652{
653   sms_state *state = space->machine().driver_data<sms_state>();
654
655   if (state->m_has_fm)
656   {
657      logerror("data_port_0_w %x %x\n", offset, data);
658      ym2413_w(state->m_ym, 1, data);
659   }
660}
661
662
663READ8_HANDLER( gg_input_port_2_r )
664{
665   sms_state *state = space->machine().driver_data<sms_state>();
666
667   //logerror("joy 2 read, val: %02x, pc: %04x\n", ((state->m_is_region_japan ? 0x00 : 0x40) | (input_port_read(machine, "START") & 0x80)), activecpu_get_pc());
668   return ((state->m_is_region_japan ? 0x00 : 0x40) | (input_port_read(space->machine(), "START") & 0x80));
669}
670
671
672READ8_HANDLER( sms_sscope_r )
673{
674   sms_state *state = space->machine().driver_data<sms_state>();
675   return state->m_sscope_state;
676}
677
678
679WRITE8_HANDLER( sms_sscope_w )
680{
681   sms_state *state = space->machine().driver_data<sms_state>();
682   state->m_sscope_state = data;
683}
684
685
686READ8_HANDLER( sms_mapper_r )
687{
688   sms_state *state = space->machine().driver_data<sms_state>();
689   return state->m_mapper[offset];
690}
691
692/* Terebi Oekaki */
693/* The following code comes from sg1000.c. We should eventually merge these TV Draw implementations */
694static WRITE8_HANDLER( sms_tvdraw_axis_w )
695{
696   sms_state *state = space->machine().driver_data<sms_state>();
697   UINT8 tvboard_on = input_port_read_safe(space->machine(), "TVDRAW", 0x00);
698
699   if (data & 0x01)
700   {
701      state->m_tvdraw_data = tvboard_on ? input_port_read(space->machine(), "TVDRAW_X") : 0x80;
702
703      if (state->m_tvdraw_data < 4) state->m_tvdraw_data = 4;
704      if (state->m_tvdraw_data > 251) state->m_tvdraw_data = 251;
705   }
706   else
707   {
708      state->m_tvdraw_data = tvboard_on ? input_port_read(space->machine(), "TVDRAW_Y") + 0x20 : 0x80;
709   }
710}
711
712static READ8_HANDLER( sms_tvdraw_status_r )
713{
714   UINT8 tvboard_on = input_port_read_safe(space->machine(), "TVDRAW", 0x00);
715   return tvboard_on ? input_port_read(space->machine(), "TVDRAW_PEN") : 0x01;
716}
717
718static READ8_HANDLER( sms_tvdraw_data_r )
719{
720   sms_state *state = space->machine().driver_data<sms_state>();
721   return state->m_tvdraw_data;
722}
723
724
725WRITE8_HANDLER( sms_mapper_w )
726{
727   sms_state *state = space->machine().driver_data<sms_state>();
728   int page;
729   UINT8 *SOURCE_BIOS;
730   UINT8 *SOURCE_CART;
731   UINT8 *SOURCE;
732   UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
733
734   offset &= 3;
735
736   state->m_mapper[offset] = data;
737   state->m_mapper_ram[offset] = data;
738
739   if (state->m_cartridge[state->m_current_cartridge].ROM)
740   {
741      SOURCE_CART = state->m_cartridge[state->m_current_cartridge].ROM + (( rom_page_count > 0) ? data % rom_page_count : 0) * 0x4000;
742   }
743   else
744   {
745      SOURCE_CART = state->m_banking_none[1];
746   }
747
748   if (state->m_BIOS)
749   {
750      SOURCE_BIOS = state->m_BIOS + ((state->m_bios_page_count > 0) ? data % state->m_bios_page_count : 0) * 0x4000;
751   }
752   else
753   {
754      SOURCE_BIOS = state->m_banking_none[1];
755   }
756
757   if (state->m_bios_port & IO_BIOS_ROM || (state->m_is_gamegear && state->m_BIOS == NULL))
758   {
759      if (!(state->m_bios_port & IO_CARTRIDGE) || (state->m_is_gamegear && state->m_BIOS == NULL))
760      {
761         page = (rom_page_count > 0) ? data % rom_page_count : 0;
762         if (!state->m_cartridge[state->m_current_cartridge].ROM)
763            return;
764         SOURCE = SOURCE_CART;
765      }
766      else
767      {
768         /* nothing to page in */
769         return;
770      }
771   }
772   else
773   {
774      page = (state->m_bios_page_count > 0) ? data % state->m_bios_page_count : 0;
775      if (!state->m_BIOS)
776         return;
777      SOURCE = SOURCE_BIOS;
778   }
779
780   switch (offset)
781   {
782   case 0: /* Control */
783      /* Is it ram or rom? */
784      if (data & 0x08) /* it's ram */
785      {
786         state->m_cartridge[state->m_current_cartridge].sram_save = 1;         /* SRAM should be saved on exit. */
787         if (data & 0x04)
788         {
789            LOG(("ram 1 paged.\n"));
790            SOURCE = state->m_cartridge[state->m_current_cartridge].cartSRAM + 0x4000;
791         }
792         else
793         {
794            LOG(("ram 0 paged.\n"));
795            SOURCE = state->m_cartridge[state->m_current_cartridge].cartSRAM;
796         }
797         memory_set_bankptr(space->machine(),  "bank5", SOURCE);
798         memory_set_bankptr(space->machine(),  "bank6", SOURCE + 0x2000);
799      }
800      else /* it's rom */
801      {
802         if (state->m_bios_port & IO_BIOS_ROM || ! state->m_has_bios)
803         {
804            page = (rom_page_count > 0) ? state->m_mapper[3] % rom_page_count : 0;
805            SOURCE = state->m_banking_cart[5];
806         }
807         else
808         {
809            page = (state->m_bios_page_count > 0) ? state->m_mapper[3] % state->m_bios_page_count : 0;
810            SOURCE = state->m_banking_bios[5];
811         }
812         LOG(("rom 2 paged in %x.\n", page));
813         memory_set_bankptr(space->machine(), "bank5", SOURCE);
814         memory_set_bankptr(space->machine(), "bank6", SOURCE + 0x2000);
815      }
816      break;
817
818   case 1: /* Select 16k ROM bank for 0400-3FFF */
819      LOG(("rom 0 paged in %x.\n", page));
820      state->m_banking_bios[2] = SOURCE_BIOS + 0x0400;
821      state->m_banking_cart[2] = SOURCE_CART + 0x0400;
822      if (state->m_is_gamegear)
823         SOURCE = SOURCE_CART;
824
825      memory_set_bankptr(space->machine(), "bank2", SOURCE + 0x0400);
826      break;
827
828   case 2: /* Select 16k ROM bank for 4000-7FFF */
829      LOG(("rom 1 paged in %x.\n", page));
830      state->m_banking_bios[3] = SOURCE_BIOS;
831      state->m_banking_cart[3] = SOURCE_CART;
832      if (state->m_is_gamegear)
833         SOURCE = SOURCE_CART;
834
835      memory_set_bankptr(space->machine(), "bank3", SOURCE);
836      memory_set_bankptr(space->machine(), "bank4", SOURCE + 0x2000);
837      break;
838
839   case 3: /* Select 16k ROM bank for 8000-BFFF */
840      state->m_banking_bios[5] = SOURCE_BIOS;
841      if (state->m_is_gamegear)
842         SOURCE = SOURCE_CART;
843
844      if ( state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
845      {
846         if (SOURCE == SOURCE_CART)
847         {
848            SOURCE = state->m_banking_cart[5];
849         }
850      }
851      else
852      {
853         state->m_banking_cart[5] = SOURCE_CART;
854      }
855
856      if (!(state->m_mapper[0] & 0x08)) /* is RAM disabled? */
857      {
858         LOG(("rom 2 paged in %x.\n", page));
859         memory_set_bankptr(space->machine(), "bank5", SOURCE);
860         memory_set_bankptr(space->machine(), "bank6", SOURCE + 0x2000);
861      }
862      break;
863   }
864}
865
866#ifdef MESS
867static WRITE8_HANDLER( sms_korean_zemina_banksw_w )
868{
869   sms_state *state = space->machine().driver_data<sms_state>();
870
871   if (state->m_cartridge[state->m_current_cartridge].features & CF_KOREAN_ZEMINA_MAPPER)
872   {
873      UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x2000;
874      int page = (rom_page_count > 0) ? data % rom_page_count : 0;
875
876      if (!state->m_cartridge[state->m_current_cartridge].ROM)
877         return;
878
879      switch (offset & 3)
880      {
881         case 0:
882            state->m_banking_cart[5] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x2000;
883            memory_set_bankptr(space->machine(), "bank5", state->m_banking_cart[5]);
884            break;
885         case 1:
886            state->m_banking_cart[6] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x2000;
887            memory_set_bankptr(space->machine(), "bank6", state->m_banking_cart[6]);
888            break;
889         case 2:
890            state->m_banking_cart[3] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x2000;
891            memory_set_bankptr(space->machine(), "bank3", state->m_banking_cart[3]);
892            break;
893         case 3:
894            state->m_banking_cart[4] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x2000;
895            memory_set_bankptr(space->machine(), "bank4", state->m_banking_cart[4]);
896            break;
897      }
898      LOG(("Zemina mapper write: offset %x data %x.\n", offset, page));
899   }
900}
901
902static WRITE8_HANDLER( sms_codemasters_page0_w )
903{
904   sms_state *state = space->machine().driver_data<sms_state>();
905
906   if (state->m_cartridge[state->m_current_cartridge].ROM && state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
907   {
908      UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
909      state->m_banking_cart[1] = state->m_cartridge[state->m_current_cartridge].ROM + ((rom_page_count > 0) ? data % rom_page_count : 0) * 0x4000;
910      state->m_banking_cart[2] = state->m_banking_cart[1] + 0x0400;
911      memory_set_bankptr(space->machine(), "bank1", state->m_banking_cart[1]);
912      memory_set_bankptr(space->machine(), "bank2", state->m_banking_cart[2]);
913   }
914}
915
916
917static WRITE8_HANDLER( sms_codemasters_page1_w )
918{
919   sms_state *state = space->machine().driver_data<sms_state>();
920
921   if (state->m_cartridge[state->m_current_cartridge].ROM && state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
922   {
923      /* Check if we need to switch in some RAM */
924      if (data & 0x80)
925      {
926         state->m_cartridge[state->m_current_cartridge].ram_page = data & 0x07;
927         memory_set_bankptr(space->machine(), "bank6", state->m_cartridge[state->m_current_cartridge].cartRAM + state->m_cartridge[state->m_current_cartridge].ram_page * 0x2000);
928      }
929      else
930      {
931         UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
932         state->m_banking_cart[3] = state->m_cartridge[state->m_current_cartridge].ROM + ((rom_page_count > 0) ? data % rom_page_count : 0) * 0x4000;
933         memory_set_bankptr(space->machine(), "bank3", state->m_banking_cart[3]);
934         memory_set_bankptr(space->machine(), "bank4", state->m_banking_cart[3] + 0x2000);
935         memory_set_bankptr(space->machine(), "bank6", state->m_banking_cart[5] + 0x2000);
936      }
937   }
938}
939#endif
940
941static READ8_HANDLER( sms_kor_nobank_r )
942{
943//  printf("read %x\n", offset);
944   return space->read_byte(0xdffc + offset);
945}
946
947static WRITE8_HANDLER( sms_kor_nobank_w )
948{
949   sms_state *state = space->machine().driver_data<sms_state>();
950   UINT8 *SOURCE_BIOS;
951   UINT8 *SOURCE = NULL;
952
953   space->write_byte(0xdffc + offset, data);
954   state->m_mapper[offset] = data;
955
956   // FIXME: the code below (which only handles the BIOS bankswitch at start) should be cleaned up
957   // currently, it's just a stripped down version of sms_mapper_w
958   if (state->m_BIOS)
959      SOURCE_BIOS = state->m_BIOS + ((state->m_bios_page_count > 0) ? data % state->m_bios_page_count : 0) * 0x4000;
960   else
961      SOURCE_BIOS = state->m_banking_none[1];
962
963   if (state->m_bios_port & IO_BIOS_ROM || (state->m_is_gamegear && state->m_BIOS == NULL))
964      return;
965   else
966   {
967      if (!state->m_BIOS)
968         return;
969      SOURCE = SOURCE_BIOS;
970   }
971
972//  printf("write %x, %x\n", offset, data);
973
974   switch (offset)
975   {
976      case 0: /* Control */
977         if (!(data & 0x08)) /* it's not ram */
978         {
979            if (!(state->m_bios_port & IO_BIOS_ROM) && state->m_has_bios)
980            {
981               SOURCE = state->m_banking_bios[5];
982               memory_set_bankptr(space->machine(), "bank5", SOURCE);
983               memory_set_bankptr(space->machine(), "bank6", SOURCE + 0x2000);
984            }
985         }
986         break;
987
988      case 1: /* Select 16k ROM bank for 0400-3FFF */
989         state->m_banking_bios[2] = SOURCE_BIOS + 0x0400;
990         memory_set_bankptr(space->machine(), "bank2", SOURCE + 0x0400);
991         break;
992
993      case 2: /* Select 16k ROM bank for 4000-7FFF */
994         state->m_banking_bios[3] = SOURCE_BIOS;
995         memory_set_bankptr(space->machine(), "bank3", SOURCE);
996         memory_set_bankptr(space->machine(), "bank4", SOURCE + 0x2000);
997         break;
998
999      case 3: /* Select 16k ROM bank for 8000-BFFF */
1000         state->m_banking_bios[5] = SOURCE_BIOS;
1001         if (!(state->m_mapper[0] & 0x08)) /* is RAM disabled? */
1002         {
1003            memory_set_bankptr(space->machine(), "bank5", SOURCE);
1004            memory_set_bankptr(space->machine(), "bank6", SOURCE + 0x2000);
1005         }
1006         break;
1007   }
1008}
1009
1010WRITE8_HANDLER( sms_bios_w )
1011{
1012   sms_state *state = space->machine().driver_data<sms_state>();
1013   state->m_bios_port = data;
1014
1015   logerror("bios write %02x, pc: %04x\n", data, cpu_get_pc(&space->device()));
1016
1017   setup_rom(space);
1018}
1019
1020
1021WRITE8_HANDLER( sms_cartram2_w )
1022{
1023   sms_state *state = space->machine().driver_data<sms_state>();
1024
1025   if (state->m_mapper[0] & 0x08)
1026   {
1027      logerror("write %02X to cartram at offset #%04X\n", data, offset + 0x2000);
1028      if (state->m_mapper[0] & 0x04)
1029      {
1030         state->m_cartridge[state->m_current_cartridge].cartSRAM[offset + 0x6000] = data;
1031      }
1032      else
1033      {
1034         state->m_cartridge[state->m_current_cartridge].cartSRAM[offset + 0x2000] = data;
1035      }
1036   }
1037
1038   if (state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
1039   {
1040      state->m_cartridge[state->m_current_cartridge].cartRAM[state->m_cartridge[state->m_current_cartridge].ram_page * 0x2000 + offset] = data;
1041   }
1042
1043   if (state->m_cartridge[state->m_current_cartridge].features & CF_KOREAN_MAPPER && offset == 0) /* Dodgeball King mapper */
1044   {
1045      UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
1046      int page = (rom_page_count > 0) ? data % rom_page_count : 0;
1047
1048      if (!state->m_cartridge[state->m_current_cartridge].ROM)
1049         return;
1050
1051      state->m_banking_cart[5] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x4000;
1052      memory_set_bankptr(space->machine(), "bank5", state->m_banking_cart[5]);
1053      memory_set_bankptr(space->machine(), "bank6", state->m_banking_cart[5] + 0x2000);
1054      LOG(("rom 2 paged in %x (Korean mapper).\n", page));
1055   }
1056}
1057
1058
1059WRITE8_HANDLER( sms_cartram_w )
1060{
1061   sms_state *state = space->machine().driver_data<sms_state>();
1062   int page;
1063
1064   if (state->m_mapper[0] & 0x08)
1065   {
1066      logerror("write %02X to cartram at offset #%04X\n", data, offset);
1067      if (state->m_mapper[0] & 0x04)
1068      {
1069         state->m_cartridge[state->m_current_cartridge].cartSRAM[offset + 0x4000] = data;
1070      }
1071      else
1072      {
1073         state->m_cartridge[state->m_current_cartridge].cartSRAM[offset] = data;
1074      }
1075   }
1076   else
1077   {
1078      if (state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER && offset == 0) /* Codemasters mapper */
1079      {
1080         UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
1081         page = (rom_page_count > 0) ? data % rom_page_count : 0;
1082         if (!state->m_cartridge[state->m_current_cartridge].ROM)
1083            return;
1084         state->m_banking_cart[5] = state->m_cartridge[state->m_current_cartridge].ROM + page * 0x4000;
1085         memory_set_bankptr(space->machine(), "bank5", state->m_banking_cart[5]);
1086         memory_set_bankptr(space->machine(), "bank6", state->m_banking_cart[5] + 0x2000);
1087         LOG(("rom 2 paged in %x (Codemasters mapper).\n", page));
1088      }
1089      else if (state->m_cartridge[state->m_current_cartridge].features & CF_ONCART_RAM)
1090      {
1091         state->m_cartridge[state->m_current_cartridge].cartRAM[offset & (state->m_cartridge[state->m_current_cartridge].ram_size - 1)] = data;
1092      }
1093      else
1094      {
1095         logerror("INVALID write %02X to cartram at offset #%04X\n", data, offset);
1096      }
1097   }
1098}
1099
1100
1101WRITE8_HANDLER( gg_sio_w )
1102{
1103   sms_state *state = space->machine().driver_data<sms_state>();
1104   logerror("*** write %02X to SIO register #%d\n", data, offset);
1105
1106   state->m_gg_sio[offset & 0x07] = data;
1107   switch (offset & 7)
1108   {
1109      case 0x00: /* Parallel Data */
1110         break;
1111
1112      case 0x01: /* Data Direction/ NMI Enable */
1113         break;
1114
1115      case 0x02: /* Serial Output */
1116         break;
1117
1118      case 0x03: /* Serial Input */
1119         break;
1120
1121      case 0x04: /* Serial Control / Status */
1122         break;
1123   }
1124}
1125
1126
1127READ8_HANDLER( gg_sio_r )
1128{
1129   sms_state *state = space->machine().driver_data<sms_state>();
1130   logerror("*** read SIO register #%d\n", offset);
1131
1132   switch (offset & 7)
1133   {
1134      case 0x00: /* Parallel Data */
1135         break;
1136
1137      case 0x01: /* Data Direction/ NMI Enable */
1138         break;
1139
1140      case 0x02: /* Serial Output */
1141         break;
1142
1143      case 0x03: /* Serial Input */
1144         break;
1145
1146      case 0x04: /* Serial Control / Status */
1147         break;
1148   }
1149
1150   return state->m_gg_sio[offset];
1151}
1152
1153static void sms_machine_stop( running_machine &machine )
1154{
1155   sms_state *state = machine.driver_data<sms_state>();
1156
1157   /* Does the cartridge have SRAM that should be saved? */
1158   if (state->m_cartridge[state->m_current_cartridge].sram_save) {
1159      device_image_interface *image = dynamic_cast<device_image_interface *>(machine.device("cart1"));
1160      image->battery_save(state->m_cartridge[state->m_current_cartridge].cartSRAM, sizeof(UINT8) * NVRAM_SIZE );
1161   }
1162}
1163
1164
1165static void setup_rom( address_space *space )
1166{
1167   sms_state *state = space->machine().driver_data<sms_state>();
1168   running_machine &machine = space->machine();
1169
1170   /* 1. set up bank pointers to point to nothing */
1171   memory_set_bankptr(machine, "bank1", state->m_banking_none[1]);
1172   memory_set_bankptr(machine, "bank2", state->m_banking_none[2]);
1173   memory_set_bankptr(machine, "bank3", state->m_banking_none[3]);
1174   memory_set_bankptr(machine, "bank4", state->m_banking_none[3] + 0x2000);
1175   memory_set_bankptr(machine, "bank5", state->m_banking_none[5]);
1176   memory_set_bankptr(machine, "bank6", state->m_banking_none[5] + 0x2000);
1177
1178   /* 2. check and set up expansion port */
1179   if (!(state->m_bios_port & IO_EXPANSION) && (state->m_bios_port & IO_CARTRIDGE) && (state->m_bios_port & IO_CARD))
1180   {
1181      /* TODO: Implement me */
1182      logerror("Switching to unsupported expansion port.\n");
1183   }
1184
1185   /* 3. check and set up card rom */
1186   if (!(state->m_bios_port & IO_CARD) && (state->m_bios_port & IO_CARTRIDGE) && (state->m_bios_port & IO_EXPANSION))
1187   {
1188      /* TODO: Implement me */
1189      logerror("Switching to unsupported card rom port.\n");
1190   }
1191
1192   /* 4. check and set up cartridge rom */
1193   /* if ((!(bios_port & IO_CARTRIDGE) && (bios_port & IO_EXPANSION) && (bios_port & IO_CARD)) || state->m_is_gamegear) { */
1194   /* Out Run Europa initially writes a value to port 3E where IO_CARTRIDGE, IO_EXPANSION and IO_CARD are reset */
1195   if ((!(state->m_bios_port & IO_CARTRIDGE)) || state->m_is_gamegear)
1196   {
1197      memory_set_bankptr(machine, "bank1", state->m_banking_cart[1]);
1198      memory_set_bankptr(machine, "bank2", state->m_banking_cart[2]);
1199      memory_set_bankptr(machine, "bank3", state->m_banking_cart[3]);
1200      memory_set_bankptr(machine, "bank4", state->m_banking_cart[3] + 0x2000);
1201      memory_set_bankptr(machine, "bank5", state->m_banking_cart[5]);
1202      memory_set_bankptr(machine, "bank6", state->m_banking_cart[5] + 0x2000);
1203      logerror("Switched in cartridge rom.\n");
1204   }
1205
1206   /* 5. check and set up bios rom */
1207   if (!(state->m_bios_port & IO_BIOS_ROM))
1208   {
1209      /* 0x0400 bioses */
1210      if (state->m_has_bios_0400)
1211      {
1212         memory_set_bankptr(machine, "bank1", state->m_banking_bios[1]);
1213         logerror("Switched in 0x0400 bios.\n");
1214      }
1215      /* 0x2000 bioses */
1216      if (state->m_has_bios_2000)
1217      {
1218         memory_set_bankptr(machine, "bank1", state->m_banking_bios[1]);
1219         memory_set_bankptr(machine, "bank2", state->m_banking_bios[2]);
1220         logerror("Switched in 0x2000 bios.\n");
1221      }
1222      if (state->m_has_bios_full)
1223      {
1224         memory_set_bankptr(machine, "bank1", state->m_banking_bios[1]);
1225         memory_set_bankptr(machine, "bank2", state->m_banking_bios[2]);
1226         memory_set_bankptr(machine, "bank3", state->m_banking_bios[3]);
1227         memory_set_bankptr(machine, "bank4", state->m_banking_bios[3] + 0x2000);
1228         memory_set_bankptr(machine, "bank5", state->m_banking_bios[5]);
1229         memory_set_bankptr(machine, "bank6", state->m_banking_bios[5] + 0x2000);
1230         logerror("Switched in full bios.\n");
1231      }
1232   }
1233
1234   if (state->m_cartridge[state->m_current_cartridge].features & CF_ONCART_RAM)
1235   {
1236      memory_set_bankptr(machine, "bank5", state->m_cartridge[state->m_current_cartridge].cartRAM);
1237      memory_set_bankptr(machine, "bank6", state->m_cartridge[state->m_current_cartridge].cartRAM);
1238   }
1239}
1240
1241
1242static int sms_verify_cart( UINT8 *magic, int size )
1243{
1244   int retval;
1245
1246   retval = IMAGE_VERIFY_FAIL;
1247
1248   /* Verify the file is a valid image - check $7ff0 for "TMR SEGA" */
1249   if (size >= 0x8000)
1250   {
1251      if (!strncmp((char*)&magic[0x7ff0], "TMR SEGA", 8))
1252      {
1253#if 0
1254         /* Technically, it should be this, but remove for now until verified: */
1255         if (!strcmp(sysname, "gamegear"))
1256         {
1257            if ((unsigned char)magic[0x7ffd] < 0x50)
1258               retval = IMAGE_VERIFY_PASS;
1259         }
1260         if (!strcmp(sysname, "sms"))
1261         {
1262            if ((unsigned char)magic[0x7ffd] >= 0x50)
1263               retval = IMAGE_VERIFY_PASS;
1264         }
1265#endif
1266         retval = IMAGE_VERIFY_PASS;
1267      }
1268
1269#if 0
1270      /* Check at $81f0 also */
1271      if (!retval)
1272      {
1273         if (!strncmp(&magic[0x81f0], "TMR SEGA", 8))
1274         {
1275#if 0
1276            /* Technically, it should be this, but remove for now until verified: */
1277            if (!strcmp(sysname, "gamegear"))
1278            {
1279               if ((unsigned char)magic[0x81fd] < 0x50)
1280                  retval = IMAGE_VERIFY_PASS;
1281            }
1282            if (!strcmp(sysname, "sms"))
1283            {
1284               if ((unsigned char)magic[0x81fd] >= 0x50)
1285                  retval = IMAGE_VERIFY_PASS;
1286            }
1287#endif
1288            retval = IMAGE_VERIFY_PASS;
1289         }
1290      }
1291#endif
1292
1293   }
1294
1295   return retval;
1296}
1297
1298#ifdef UNUSED_FUNCTION
1299// For the moment we switch to a different detection routine which allows to detect
1300// in a single run Codemasters mapper, Korean mapper (including Jang Pung 3 which
1301// uses a diff signature then the one below here) and Zemina mapper (used by Wonsiin, etc.).
1302// I leave these here to document alt. detection routines and in the case these functions
1303// can be updated
1304
1305/* Check for Codemasters mapper
1306  0x7FE3 - 93 - sms Cosmis Spacehead
1307              - sms Dinobasher
1308              - sms The Excellent Dizzy Collection
1309              - sms Fantastic Dizzy
1310              - sms Micro Machines
1311              - gamegear Cosmic Spacehead
1312              - gamegear Micro Machines
1313         - 94 - gamegear Dropzone
1314              - gamegear Ernie Els Golf (also has 64KB additional RAM on the cartridge)
1315              - gamegear Pete Sampras Tennis
1316              - gamegear S.S. Lucifer
1317         - 95 - gamegear Micro Machines 2 - Turbo Tournament
1318
1319The Korean game Jang Pung II also seems to use a codemasters style mapper.
1320 */
1321static int detect_codemasters_mapper( UINT8 *rom )
1322{
1323   static const UINT8 jang_pung2[16] = { 0x00, 0xba, 0x38, 0x0d, 0x00, 0xb8, 0x38, 0x0c, 0x00, 0xb6, 0x38, 0x0b, 0x00, 0xb4, 0x38, 0x0a };
1324
1325   if (((rom[0x7fe0] & 0x0f ) <= 9) && (rom[0x7fe3] == 0x93 || rom[0x7fe3] == 0x94 || rom[0x7fe3] == 0x95) &&  rom[0x7fef] == 0x00)
1326      return 1;
1327
1328   if (!memcmp(&rom[0x7ff0], jang_pung2, 16))
1329      return 1;
1330
1331   return 0;
1332}
1333
1334
1335static int detect_korean_mapper( UINT8 *rom )
1336{
1337   static const UINT8 signatures[2][16] =
1338   {
1339      { 0x3e, 0x11, 0x32, 0x00, 0xa0, 0x78, 0xcd, 0x84, 0x85, 0x3e, 0x02, 0x32, 0x00, 0xa0, 0xc9, 0xff }, /* Dodgeball King */
1340      { 0x41, 0x48, 0x37, 0x37, 0x44, 0x37, 0x4e, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20 },   /* Sangokushi 3 */
1341   };
1342   int i;
1343
1344   for (i = 0; i < 2; i++)
1345   {
1346      if (!memcmp(&rom[0x7ff0], signatures[i], 16))
1347      {
1348         return 1;
1349      }
1350   }
1351   return 0;
1352}
1353#endif
1354
1355
1356static int detect_tvdraw( UINT8 *rom )
1357{
1358   static const UINT8 terebi_oekaki[7] = { 0x61, 0x6e, 0x6e, 0x61, 0x6b, 0x6d, 0x6e };   // "annakmn"
1359
1360   if (!memcmp(&rom[0x13b3], terebi_oekaki, 7))
1361      return 1;
1362
1363   return 0;
1364}
1365
1366
1367static int detect_lphaser_xoffset( running_machine &machine, UINT8 *rom )
1368{
1369   sms_state *state = machine.driver_data<sms_state>();
1370
1371   static const UINT8 signatures[6][16] =
1372   {
1373      /* Spacegun */
1374      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0xff, 0xff, 0x9d, 0x99, 0x10, 0x90, 0x00, 0x40 },
1375      /* Gangster Town */
1376      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0x19, 0x87, 0x1b, 0xc9, 0x74, 0x50, 0x00, 0x4f },
1377      /* Shooting Gallery */
1378      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0x20, 0x20, 0x8a, 0x3a, 0x72, 0x50, 0x00, 0x4f },
1379      /* Rescue Mission */
1380      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0x20, 0x20, 0xfb, 0xd3, 0x06, 0x51, 0x00, 0x4f },
1381      /* Laser Ghost */
1382      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0x00, 0x00, 0xb7, 0x55, 0x74, 0x70, 0x00, 0x40 },
1383      /* Assault City */
1384      { 0x54, 0x4d, 0x52, 0x20, 0x53, 0x45, 0x47, 0x41, 0xff, 0xff, 0x9f, 0x74, 0x34, 0x70, 0x00, 0x40 },
1385   };
1386
1387   if (!(state->m_bios_port & IO_CARTRIDGE) && state->m_cartridge[state->m_current_cartridge].size >= 0x8000)
1388   {
1389      if (!memcmp(&rom[0x7ff0], signatures[0], 16) || !memcmp(&rom[0x7ff0], signatures[1], 16))
1390         return 40;
1391
1392      if (!memcmp(&rom[0x7ff0], signatures[2], 16))
1393         return 49;
1394
1395      if (!memcmp(&rom[0x7ff0], signatures[3], 16))
1396         return 47;
1397
1398      if (!memcmp(&rom[0x7ff0], signatures[4], 16))
1399         return 44;
1400
1401      if (!memcmp(&rom[0x7ff0], signatures[5], 16))
1402         return 53;
1403
1404   }
1405   return 50;
1406}
1407
1408
1409DEVICE_START( sms_cart )
1410{
1411   sms_state *state = device->machine().driver_data<sms_state>();
1412   int i;
1413
1414   for (i = 0; i < MAX_CARTRIDGES; i++)
1415   {
1416      state->m_cartridge[i].ROM = NULL;
1417      state->m_cartridge[i].size = 0;
1418      state->m_cartridge[i].features = 0;
1419      state->m_cartridge[i].cartSRAM = NULL;
1420      state->m_cartridge[i].sram_save = 0;
1421      state->m_cartridge[i].cartRAM = NULL;
1422      state->m_cartridge[i].ram_size = 0;
1423      state->m_cartridge[i].ram_page = 0;
1424   }
1425   state->m_current_cartridge = 0;
1426
1427   state->m_bios_port = (IO_EXPANSION | IO_CARTRIDGE | IO_CARD);
1428   if (!state->m_is_gamegear && !state->m_has_bios)
1429   {
1430      state->m_bios_port &= ~(IO_CARTRIDGE);
1431      state->m_bios_port |= IO_BIOS_ROM;
1432   }
1433}
1434
1435
1436DEVICE_IMAGE_LOAD( sms_cart )
1437{
1438   running_machine &machine = image.device().machine();
1439   sms_state *state = machine.driver_data<sms_state>();
1440   int size, index = 0, offset = 0;
1441   const char *extrainfo = NULL;
1442
1443   if (strcmp(image.device().tag(), "cart1") == 0)
1444      index = 0;
1445   if (strcmp(image.device().tag(), "cart2") == 0)
1446      index = 1;
1447   if (strcmp(image.device().tag(), "cart3") == 0)
1448      index = 2;
1449   if (strcmp(image.device().tag(), "cart4") == 0)
1450      index = 3;
1451   if (strcmp(image.device().tag(), "cart5") == 0)
1452      index = 4;
1453   if (strcmp(image.device().tag(), "cart6") == 0)
1454      index = 5;
1455   if (strcmp(image.device().tag(), "cart7") == 0)
1456      index = 6;
1457   if (strcmp(image.device().tag(), "cart8") == 0)
1458      index = 7;
1459   if (strcmp(image.device().tag(), "cart9") == 0)
1460      index = 8;
1461   if (strcmp(image.device().tag(), "cart10") == 0)
1462      index = 9;
1463   if (strcmp(image.device().tag(), "cart11") == 0)
1464      index = 10;
1465   if (strcmp(image.device().tag(), "cart12") == 0)
1466      index = 11;
1467   if (strcmp(image.device().tag(), "cart13") == 0)
1468      index = 12;
1469   if (strcmp(image.device().tag(), "cart14") == 0)
1470      index = 13;
1471   if (strcmp(image.device().tag(), "cart15") == 0)
1472      index = 14;
1473   if (strcmp(image.device().tag(), "cart16") == 0)
1474      index = 15;
1475
1476   state->m_cartridge[index].features = 0;
1477
1478   if (image.software_entry() == NULL)
1479   {
1480      size = image.length();
1481      extrainfo = hashfile_extrainfo(image);
1482   }
1483   else
1484      size = image.get_software_region_length("rom");
1485
1486   /* Check for 512-byte header */
1487   if ((size / 512) & 1)
1488   {
1489      offset = 512;
1490      size -= 512;
1491   }
1492
1493   if (!size)
1494   {
1495      image.seterror(IMAGE_ERROR_UNSPECIFIED, "Invalid ROM image: ROM image is too small");
1496      return IMAGE_INIT_FAIL;
1497   }
1498
1499   /* Create a new memory region to hold the ROM. */
1500   /* Make sure the region holds only complete (0x4000) rom banks */
1501   state->m_cartridge[index].size = (size & 0x3fff) ? (((size >> 14) + 1) << 14) : size;
1502   state->m_cartridge[index].ROM = auto_alloc_array(machine, UINT8, state->m_cartridge[index].size);
1503   state->m_cartridge[index].cartSRAM = auto_alloc_array(machine, UINT8, NVRAM_SIZE);
1504
1505   /* Load ROM banks */
1506   if (image.software_entry() == NULL)
1507   {
1508      image.fseek(offset, SEEK_SET);
1509
1510      if (image.fread( state->m_cartridge[index].ROM, size) != size)
1511         return IMAGE_INIT_FAIL;
1512   }
1513   else
1514   {
1515      memcpy(state->m_cartridge[index].ROM, image.get_software_region("rom") + offset, size);
1516
1517      // check for mapper feature (currently only KOREAN_NOBANK, but eventually we will add the other mappers as well)
1518      if (image.get_feature("pcb") != NULL)
1519      {
1520         const char *mapper = image.get_feature("pcb");
1521         if (!strcmp(mapper, "korean_nobank"))
1522            state->m_cartridge[index].features |= CF_KOREAN_NOBANK_MAPPER;
1523      }
1524   }
1525
1526   /* check the image */
1527   if (!state->m_has_bios)
1528   {
1529      if (sms_verify_cart(state->m_cartridge[index].ROM, size) == IMAGE_VERIFY_FAIL)
1530      {
1531         logerror("Warning loading image: sms_verify_cart failed\n");
1532      }
1533   }
1534
1535   /* Detect special features from the extrainfo field */
1536   if (extrainfo)
1537   {
1538      /* Check for codemasters mapper */
1539      if (strstr(extrainfo, "CODEMASTERS"))
1540         state->m_cartridge[index].features |= CF_CODEMASTERS_MAPPER;
1541
1542      /* Check for korean mapper */
1543      if (strstr(extrainfo, "KOREAN"))
1544         state->m_cartridge[index].features |= CF_KOREAN_MAPPER;
1545
1546      /* Check for special SMS Compatibility mode gamegear cartridges */
1547      if (state->m_is_gamegear && strstr(extrainfo, "GGSMS"))
1548         state->m_cartridge[index].features |= CF_GG_SMS_MODE;
1549
1550      /* Check for 93C46 eeprom */
1551      if (strstr(extrainfo, "93C46"))
1552         state->m_cartridge[index].features |= CF_93C46_EEPROM;
1553
1554      /* Check for 8KB on-cart RAM */
1555      if (strstr(extrainfo, "8KB_CART_RAM"))
1556      {
1557         state->m_cartridge[index].features |= CF_ONCART_RAM;
1558         state->m_cartridge[index].ram_size = 0x2000;
1559         state->m_cartridge[index].cartRAM = auto_alloc_array(machine, UINT8, state->m_cartridge[index].ram_size);
1560      }
1561   }
1562   else
1563   {
1564      /* If no extrainfo information is available try to find special information out on our own */
1565      /* Check for special cartridge features (new routine, courtesy of Omar Cornut, from MEKA)  */
1566      if (size >= 0x8000)
1567      {
1568         int c0002 = 0, c8000 = 0, cA000 = 0, cFFFF = 0, i;
1569         for (i = 0; i < 0x8000; i++)
1570         {
1571            if (state->m_cartridge[index].ROM[i] == 0x32) // Z80 opcode for: LD (xxxx), A
1572            {
1573               UINT16 addr = (state->m_cartridge[index].ROM[i + 2] << 8) | state->m_cartridge[index].ROM[i + 1];
1574               if (addr == 0xFFFF)
1575               { i += 2; cFFFF++; continue; }
1576               if (addr == 0x0002 || addr == 0x0003 || addr == 0x0004)
1577               { i += 2; c0002++; continue; }
1578               if (addr == 0x8000)
1579               { i += 2; c8000++; continue; }
1580               if (addr == 0xA000)
1581               { i += 2; cA000++; continue; }
1582            }
1583         }
1584
1585         LOG(("Mapper test: c002 = %d, c8000 = %d, cA000 = %d, cFFFF = %d\n", c0002, c8000, cA000, cFFFF));
1586
1587         // 2 is a security measure, although tests on existing ROM showed it was not needed
1588         if (c0002 > cFFFF + 2 || (c0002 > 0 && cFFFF == 0))
1589            state->m_cartridge[index].features |= CF_KOREAN_ZEMINA_MAPPER;
1590         else if (c8000 > cFFFF + 2 || (c8000 > 0 && cFFFF == 0))
1591            state->m_cartridge[index].features |= CF_CODEMASTERS_MAPPER;
1592         else if (cA000 > cFFFF + 2 || (cA000 > 0 && cFFFF == 0))
1593            state->m_cartridge[index].features |= CF_KOREAN_MAPPER;
1594
1595      }
1596
1597      /* Check for special SMS Compatibility mode gamegear cartridges */
1598      if (state->m_is_gamegear && image.software_entry() == NULL)   // not sure about how to handle this with softlists
1599      {
1600         /* Just in case someone passes us an sms file */
1601         if (!mame_stricmp (image.filetype(), "sms"))
1602            state->m_cartridge[index].features |= CF_GG_SMS_MODE;
1603      }
1604   }
1605
1606   if (state->m_cartridge[index].features & CF_CODEMASTERS_MAPPER)
1607   {
1608      state->m_cartridge[index].ram_size = 0x10000;
1609      state->m_cartridge[index].cartRAM = auto_alloc_array(machine, UINT8, state->m_cartridge[index].ram_size);
1610      state->m_cartridge[index].ram_page = 0;
1611   }
1612
1613   /* For Light Phaser games, we have to detect the x offset */
1614   state->m_lphaser_x_offs = detect_lphaser_xoffset(machine, state->m_cartridge[index].ROM);
1615
1616   /* Terebi Oekaki (TV Draw) is a SG1000 game with special input device which is compatible with SG1000 Mark III */
1617   if ((detect_tvdraw(state->m_cartridge[index].ROM)) && state->m_is_region_japan)
1618   {
1619      address_space *program = machine.device("maincpu")->memory().space(AS_PROGRAM);
1620      program->install_legacy_write_handler(0x6000, 0x6000, FUNC(sms_tvdraw_axis_w));
1621      program->install_legacy_read_handler(0x8000, 0x8000, FUNC(sms_tvdraw_status_r));
1622      program->install_legacy_read_handler(0xa000, 0xa000, FUNC(sms_tvdraw_data_r));
1623      program->nop_write(0xa000, 0xa000);
1624   }
1625
1626   if (state->m_cartridge[index].features & CF_KOREAN_NOBANK_MAPPER)
1627   {
1628      // FIXME: we should have by default FFFD-FFFF to be only a mirror for DFFD-DFFF (with no bankswitch logic)
1629      // and then the handlers should be installed here for all but the KOREAN_NOBANK carts
1630      // However, to avoid memory map breakage, we currently go the other way around
1631      address_space *program = machine.device("maincpu")->memory().space(AS_PROGRAM);
1632      program->install_legacy_readwrite_handler(0xfffc, 0xffff, FUNC(sms_kor_nobank_r), FUNC(sms_kor_nobank_w));
1633   }
1634
1635   LOG(("Cart Features: %x\n", state->m_cartridge[index].features));
1636
1637   /* Load battery backed RAM, if available */
1638   image.battery_load(state->m_cartridge[index].cartSRAM, sizeof(UINT8) * NVRAM_SIZE, 0x00);
1639
1640   return IMAGE_INIT_PASS;
1641}
1642
1643
1644static void setup_cart_banks( running_machine &machine )
1645{
1646   sms_state *state = machine.driver_data<sms_state>();
1647   if (state->m_cartridge[state->m_current_cartridge].ROM)
1648   {
1649      UINT8 rom_page_count = state->m_cartridge[state->m_current_cartridge].size / 0x4000;
1650      state->m_banking_cart[1] = state->m_cartridge[state->m_current_cartridge].ROM;
1651      state->m_banking_cart[2] = state->m_cartridge[state->m_current_cartridge].ROM + 0x0400;
1652      state->m_banking_cart[3] = state->m_cartridge[state->m_current_cartridge].ROM + ((1 < rom_page_count) ? 0x4000 : 0);
1653      state->m_banking_cart[5] = state->m_cartridge[state->m_current_cartridge].ROM + ((2 < rom_page_count) ? 0x8000 : 0);
1654      /* Codemasters mapper points to bank 0 for page 2 */
1655      if (state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
1656      {
1657         state->m_banking_cart[5] = state->m_cartridge[state->m_current_cartridge].ROM;
1658      }
1659   }
1660   else
1661   {
1662      state->m_banking_cart[1] = state->m_banking_none[1];
1663      state->m_banking_cart[2] = state->m_banking_none[2];
1664      state->m_banking_cart[3] = state->m_banking_none[3];
1665      state->m_banking_cart[5] = state->m_banking_none[5];
1666   }
1667}
1668
1669#ifdef MESS
1670static void setup_banks( running_machine &machine )
1671{
1672   sms_state *state = machine.driver_data<sms_state>();
1673   UINT8 *mem = machine.region("maincpu")->base();
1674   state->m_banking_bios[1] = state->m_banking_cart[1] = state->m_banking_none[1] = mem;
1675   state->m_banking_bios[2] = state->m_banking_cart[2] = state->m_banking_none[2] = mem;
1676   state->m_banking_bios[3] = state->m_banking_cart[3] = state->m_banking_none[3] = mem;
1677   state->m_banking_bios[4] = state->m_banking_cart[4] = state->m_banking_none[4] = mem;
1678   state->m_banking_bios[5] = state->m_banking_cart[5] = state->m_banking_none[5] = mem;
1679   state->m_banking_bios[6] = state->m_banking_cart[6] = state->m_banking_none[6] = mem;
1680
1681   state->m_BIOS = machine.region("user1")->base();
1682
1683   state->m_bios_page_count = (state->m_BIOS ? machine.region("user1")->bytes() / 0x4000 : 0);
1684
1685   setup_cart_banks(machine);
1686
1687   if (state->m_BIOS == NULL || state->m_BIOS[0] == 0x00)
1688   {
1689      state->m_BIOS = NULL;
1690      state->m_bios_port |= IO_BIOS_ROM;
1691   }
1692
1693   if (state->m_BIOS)
1694   {
1695      state->m_banking_bios[1] = state->m_BIOS;
1696      state->m_banking_bios[2] = state->m_BIOS + 0x0400;
1697      state->m_banking_bios[3] = state->m_BIOS + ((1 < state->m_bios_page_count) ? 0x4000 : 0);
1698      state->m_banking_bios[5] = state->m_BIOS + ((2 < state->m_bios_page_count) ? 0x8000 : 0);
1699   }
1700}
1701#endif
1702
1703MACHINE_START( sms )
1704{
1705   sms_state *state = machine.driver_data<sms_state>();
1706
1707   machine.add_notifier(MACHINE_NOTIFY_EXIT, machine_notify_delegate(FUNC(sms_machine_stop),&machine));
1708   state->m_rapid_fire_timer = machine.scheduler().timer_alloc(FUNC(rapid_fire_callback));
1709   state->m_rapid_fire_timer->adjust(attotime::from_hz(10), 0, attotime::from_hz(10));
1710
1711   state->m_lphaser_1_timer = machine.scheduler().timer_alloc(FUNC(lphaser_1_callback));
1712   state->m_lphaser_2_timer = machine.scheduler().timer_alloc(FUNC(lphaser_2_callback));
1713
1714   state->m_main_cpu = machine.device("maincpu");
1715   state->m_control_cpu = machine.device("control");
1716   state->m_vdp = machine.device("sms_vdp");
1717   state->m_ym = machine.device("ym2413");
1718   state->m_main_scr = machine.device("screen");
1719   state->m_left_lcd = machine.device("left_lcd");
1720   state->m_right_lcd = machine.device("right_lcd");
1721
1722   /* Check if lightgun has been chosen as input: if so, enable crosshair */
1723   machine.scheduler().timer_set(attotime::zero, FUNC(lightgun_tick));
1724}
1725
1726#ifdef MESS
1727MACHINE_RESET( sms )
1728{
1729   address_space *space = machine.device("maincpu")->memory().space(AS_PROGRAM);
1730   sms_state *state = machine.driver_data<sms_state>();
1731
1732   state->m_ctrl_reg = 0xff;
1733   if (state->m_has_fm)
1734      state->m_fm_detect = 0x01;
1735
1736   state->m_mapper_ram = (UINT8*)space->get_write_ptr(0xdffc);
1737
1738   state->m_bios_port = 0;
1739
1740   if (state->m_cartridge[state->m_current_cartridge].features & CF_CODEMASTERS_MAPPER)
1741   {
1742      /* Install special memory handlers */
1743      space->install_legacy_write_handler(0x0000, 0x0000, FUNC(sms_codemasters_page0_w));
1744      space->install_legacy_write_handler(0x4000, 0x4000, FUNC(sms_codemasters_page1_w));
1745   }
1746
1747   if (state->m_cartridge[state->m_current_cartridge].features & CF_KOREAN_ZEMINA_MAPPER)
1748      space->install_legacy_write_handler(0x0000, 0x0003, FUNC(sms_korean_zemina_banksw_w));
1749
1750   if (state->m_cartridge[state->m_current_cartridge].features & CF_GG_SMS_MODE)
1751      sms_vdp_set_ggsmsmode(state->m_vdp, 1);
1752
1753   /* Initialize SIO stuff for GG */
1754   state->m_gg_sio[0] = 0x7f;
1755   state->m_gg_sio[1] = 0xff;
1756   state->m_gg_sio[2] = 0x00;
1757   state->m_gg_sio[3] = 0xff;
1758   state->m_gg_sio[4] = 0x00;
1759
1760   state->m_store_control = 0;
1761
1762   setup_banks(machine);
1763
1764   setup_rom(space);
1765
1766   state->m_rapid_fire_state_1 = 0;
1767   state->m_rapid_fire_state_2 = 0;
1768
1769   state->m_last_paddle_read_time = 0;
1770   state->m_paddle_read_state = 0;
1771
1772   state->m_last_sports_pad_time_1 = 0;
1773   state->m_last_sports_pad_time_2 = 0;
1774   state->m_sports_pad_state_1 = 0;
1775   state->m_sports_pad_state_2 = 0;
1776   state->m_sports_pad_last_data_1 = 0;
1777   state->m_sports_pad_last_data_2 = 0;
1778   state->m_sports_pad_1_x = 0;
1779   state->m_sports_pad_1_y = 0;
1780   state->m_sports_pad_2_x = 0;
1781   state->m_sports_pad_2_y = 0;
1782
1783   state->m_lphaser_1_latch = 0;
1784   state->m_lphaser_2_latch = 0;
1785
1786   state->m_sscope_state = 0;
1787
1788   state->m_tvdraw_data = 0;
1789}
1790#endif
1791
1792READ8_HANDLER( sms_store_cart_select_r )
1793{
1794   return 0xff;
1795}
1796
1797
1798WRITE8_HANDLER( sms_store_cart_select_w )
1799{
1800   sms_state *state = space->machine().driver_data<sms_state>();
1801   UINT8 slot = data >> 4;
1802   UINT8 slottype = data & 0x08;
1803
1804   logerror("switching in part of %s slot #%d\n", slottype ? "card" : "cartridge", slot );
1805   /* cartridge? slot #0 */
1806   if (slottype == 0)
1807      state->m_current_cartridge = slot;
1808
1809   setup_cart_banks(space->machine());
1810   memory_set_bankptr(space->machine(), "bank10", state->m_banking_cart[3] + 0x2000);
1811   setup_rom(space);
1812}
1813
1814
1815READ8_HANDLER( sms_store_select1 )
1816{
1817   return 0xff;
1818}
1819
1820
1821READ8_HANDLER( sms_store_select2 )
1822{
1823   return 0xff;
1824}
1825
1826
1827READ8_HANDLER( sms_store_control_r )
1828{
1829   sms_state *state = space->machine().driver_data<sms_state>();
1830   return state->m_store_control;
1831}
1832
1833
1834WRITE8_HANDLER( sms_store_control_w )
1835{
1836   sms_state *state = space->machine().driver_data<sms_state>();
1837   logerror("0x%04X: sms_store_control write 0x%02X\n", cpu_get_pc(&space->device()), data);
1838   if (data & 0x02)
1839   {
1840      space->machine().device<cpu_device>("maincpu")->resume(SUSPEND_REASON_HALT);
1841   }
1842   else
1843   {
1844      /* Pull reset line of CPU #0 low */
1845      space->machine().device("maincpu")->reset();
1846      space->machine().device<cpu_device>("maincpu")->suspend(SUSPEND_REASON_HALT, 1);
1847   }
1848   state->m_store_control = data;
1849}
1850
1851void sms_store_int_callback( running_machine &machine, int state )
1852{
1853   sms_state *driver_state = machine.driver_data<sms_state>();
1854   device_set_input_line(driver_state->m_store_control & 0x01 ? driver_state->m_control_cpu : driver_state->m_main_cpu, 0, state);
1855}
1856
1857
1858static void sms_set_zero_flag( running_machine &machine )
1859{
1860   sms_state *state = machine.driver_data<sms_state>();
1861   state->m_is_gamegear = 0;
1862   state->m_is_region_japan = 0;
1863   state->m_has_bios_0400 = 0;
1864   state->m_has_bios_2000 = 0;
1865   state->m_has_bios_full = 0;
1866   state->m_has_bios = 0;
1867   state->m_has_fm = 0;
1868}
1869
1870DRIVER_INIT( sg1000m3 )
1871{
1872   sms_state *state = machine.driver_data<sms_state>();
1873   sms_set_zero_flag(machine);
1874   state->m_is_region_japan = 1;
1875   state->m_has_fm = 1;
1876}
1877
1878
1879DRIVER_INIT( sms1 )
1880{
1881   sms_state *state = machine.driver_data<sms_state>();
1882   sms_set_zero_flag(machine);
1883   state->m_has_bios_full = 1;
1884}
1885
1886
1887DRIVER_INIT( smsj )
1888{
1889   sms_state *state = machine.driver_data<sms_state>();
1890   sms_set_zero_flag(machine);
1891   state->m_is_region_japan = 1;
1892   state->m_has_bios_2000 = 1;
1893   state->m_has_fm = 1;
1894}
1895
1896
1897DRIVER_INIT( sms2kr )
1898{
1899   sms_state *state = machine.driver_data<sms_state>();
1900   sms_set_zero_flag(machine);
1901   state->m_is_region_japan = 1;
1902   state->m_has_bios_full = 1;
1903   state->m_has_fm = 1;
1904}
1905
1906
1907DRIVER_INIT( smssdisp )
1908{
1909   sms_set_zero_flag(machine);
1910}
1911
1912
1913DRIVER_INIT( gamegear )
1914{
1915   sms_state *state = machine.driver_data<sms_state>();
1916   sms_set_zero_flag(machine);
1917   state->m_is_gamegear = 1;
1918}
1919
1920
1921DRIVER_INIT( gamegeaj )
1922{
1923   sms_state *state = machine.driver_data<sms_state>();
1924   sms_set_zero_flag(machine);
1925   state->m_is_region_japan = 1;
1926   state->m_is_gamegear = 1;
1927   state->m_has_bios_0400 = 1;
1928}
1929
1930
1931static void sms_black_bitmap( const screen_device *screen, bitmap_t *bitmap )
1932{
1933   const int width = screen->width();
1934   const int height = screen->height();
1935   int x, y;
1936
1937   for (y = 0; y < height; y++)
1938      for (x = 0; x < width; x++)
1939         *BITMAP_ADDR32(bitmap, y, x) = MAKE_RGB(0,0,0);
1940}
1941
1942VIDEO_START( sms1 )
1943{
1944   sms_state *state = machine.driver_data<sms_state>();
1945   screen_device *screen = machine.first_screen();
1946   int width = screen->width();
1947   int height = screen->height();
1948
1949   state->m_prevleft_bitmap = auto_bitmap_alloc(machine, width, height, BITMAP_FORMAT_INDEXED32);
1950   state->m_prevright_bitmap = auto_bitmap_alloc(machine, width, height, BITMAP_FORMAT_INDEXED32);
1951   state->save_item(NAME(*state->m_prevleft_bitmap));
1952   state->save_item(NAME(*state->m_prevright_bitmap));
1953}
1954
1955SCREEN_UPDATE( sms1 )
1956{
1957   sms_state *state = screen->machine().driver_data<sms_state>();
1958   UINT8 sscope = input_port_read_safe(screen->machine(), "SEGASCOPE", 0x00);
1959   UINT8 sscope_binocular_hack = input_port_read_safe(screen->machine(), "SSCOPE_BINOCULAR", 0x00);
1960   UINT8 occluded_view = 0;
1961
1962   // without SegaScope, both LCDs for glasses go black
1963   if ((screen != state->m_main_scr) && !sscope)
1964      occluded_view = 1;
1965
1966   // with SegaScope, sscope_state 0 = left screen OFF, right screen ON
1967   if (!(state->m_sscope_state & 0x01) && (screen == state->m_left_lcd))
1968      occluded_view = 1;
1969
1970   // with SegaScope, sscope_state 1 = left screen ON, right screen OFF
1971   if ((state->m_sscope_state & 0x01) && (screen == state->m_right_lcd))
1972      occluded_view = 1;
1973
1974   if (!occluded_view)
1975   {
1976      sms_vdp_update(state->m_vdp, bitmap, cliprect);
1977
1978      // HACK: fake 3D->2D handling (if enabled, it repeats each frame twice on the selected lens)
1979      // save a copy of current bitmap for the binocular hack
1980      if (sscope && (screen == state->m_left_lcd) && (sscope_binocular_hack & 0x01))
1981         copybitmap(state->m_prevleft_bitmap, bitmap, 0, 0, 0, 0, cliprect);
1982      if (sscope && (screen == state->m_right_lcd) && (sscope_binocular_hack & 0x02))
1983         copybitmap(state->m_prevright_bitmap, bitmap, 0, 0, 0, 0, cliprect);
1984   }
1985   else
1986   {
1987      sms_black_bitmap(screen, bitmap);
1988
1989      // HACK: fake 3D->2D handling (if enabled, it repeats each frame twice on the selected lens)
1990      // use the copied bitmap for the binocular hack
1991      if (sscope && (screen == state->m_left_lcd) && (sscope_binocular_hack & 0x01))
1992         copybitmap(bitmap, state->m_prevleft_bitmap, 0, 0, 0, 0, cliprect);
1993      if (sscope && (screen == state->m_right_lcd) && (sscope_binocular_hack & 0x02))
1994         copybitmap(bitmap, state->m_prevright_bitmap, 0, 0, 0, 0, cliprect);
1995   }
1996
1997   return 0;
1998}
1999
2000SCREEN_UPDATE( sms )
2001{
2002   sms_state *state = screen->machine().driver_data<sms_state>();
2003   sms_vdp_update(state->m_vdp, bitmap, cliprect);
2004   return 0;
2005}
2006
2007VIDEO_START( gamegear )
2008{
2009   sms_state *state = machine.driver_data<sms_state>();
2010   screen_device *screen = machine.first_screen();
2011   int width = screen->width();
2012   int height = screen->height();
2013
2014   state->m_prev_bitmap = auto_bitmap_alloc(machine, width, height, BITMAP_FORMAT_INDEXED32);
2015   state->m_tmp_bitmap = auto_bitmap_alloc(machine, width, height, BITMAP_FORMAT_INDEXED32);
2016   state->save_item(NAME(*state->m_prev_bitmap));
2017}
2018
2019SCREEN_UPDATE( gamegear )
2020{
2021   sms_state *state = screen->machine().driver_data<sms_state>();
2022   int width = screen->width();
2023   int height = screen->height();
2024   int x, y;
2025
2026   sms_vdp_update(state->m_vdp, state->m_tmp_bitmap, cliprect);
2027
2028   // HACK: fake LCD persistence effect
2029   // (it would be better to generalize this in the core, to be used for all LCD systems)
2030   for (y = 0; y < height; y++)
2031   {
2032      UINT32 *line0 = BITMAP_ADDR32(state->m_tmp_bitmap, y, 0);
2033      UINT32 *line1 = BITMAP_ADDR32(state->m_prev_bitmap, y, 0);
2034      for (x = 0; x < width; x++)
2035      {
2036         UINT32 color0 = line0[x];
2037         UINT32 color1 = line1[x];
2038         UINT16 r0 = (color0 >> 16) & 0x000000ff;
2039         UINT16 g0 = (color0 >>  8) & 0x000000ff;
2040         UINT16 b0 = (color0 >>  0) & 0x000000ff;
2041         UINT16 r1 = (color1 >> 16) & 0x000000ff;
2042         UINT16 g1 = (color1 >>  8) & 0x000000ff;
2043         UINT16 b1 = (color1 >>  0) & 0x000000ff;
2044         UINT8 r = (UINT8)((r0 + r1) >> 1);
2045         UINT8 g = (UINT8)((g0 + g1) >> 1);
2046         UINT8 b = (UINT8)((b0 + b1) >> 1);
2047         *BITMAP_ADDR32(bitmap, y, x) = (r << 16) | (g << 8) | b;
2048      }
2049   }
2050   copybitmap(state->m_prev_bitmap, state->m_tmp_bitmap, 0, 0, 0, 0, cliprect);
2051
2052   return 0;
2053}
src/mess/layout/pmi80.lay
r12101r12102
1<!-- pmi80.lay -->
2 
3<!-- 2011-06-20: Initial version.  [Robbbert] -->
4 
5<mamelayout version="2">
6 
7   <element name="digit" defstate="0">
8      <led7seg>
9         <color red="1.0" green="0.0" blue="0.0" />
10      </led7seg>
11   </element>
12 
13   <element name="background">
14      <rect>
15         <bounds left="0" top="0" right="1" bottom="1" />
16         <color red="0.0" green="0.0" blue="0.0" />
17      </rect>
18   </element>
19 
20   <view name="LEDs only">
21 
22      <!-- Background -->
23      <backdrop element="background">
24         <bounds left="0" top="0" right="450" bottom="69" />
25      </backdrop>
26 
27      <!-- LEDs -->
28      <bezel name="digit0" element="digit">
29         <bounds left="10" top="10" right="44" bottom="59" />
30      </bezel>
31      <bezel name="digit1" element="digit">
32         <bounds left="54" top="10" right="88" bottom="59" />
33      </bezel>
34      <bezel name="digit2" element="digit">
35         <bounds left="98" top="10" right="132" bottom="59" />
36      </bezel>
37      <bezel name="digit3" element="digit">
38         <bounds left="142" top="10" right="176" bottom="59" />
39      </bezel>
40      <bezel name="digit4" element="digit">
41         <bounds left="186" top="10" right="220" bottom="59" />
42      </bezel>
43      <bezel name="digit5" element="digit">
44         <bounds left="230" top="10" right="264" bottom="59" />
45      </bezel>
46      <bezel name="digit6" element="digit">
47         <bounds left="274" top="10" right="308" bottom="59" />
48      </bezel>
49      <bezel name="digit7" element="digit">
50         <bounds left="318" top="10" right="352" bottom="59" />
51      </bezel>
52      <bezel name="digit8" element="digit">
53         <bounds left="362" top="10" right="396" bottom="59" />
54      </bezel>
55      <bezel name="digitx" element="digit">
56         <bounds left="406" top="10" right="440" bottom="59" />
57      </bezel><!-- This digit shows something but it does not exist on real unit.
58                   Change to 'digit9' to see it in action. -->
59 
60   </view>
61 
62</mamelayout>
1<!-- pmi80.lay -->
2
3<!-- 2011-06-20: Initial version.  [Robbbert] -->
4
5<mamelayout version="2">
6
7   <element name="digit" defstate="0">
8      <led7seg>
9         <color red="1.0" green="0.0" blue="0.0" />
10      </led7seg>
11   </element>
12
13   <element name="background">
14      <rect>
15         <bounds left="0" top="0" right="1" bottom="1" />
16         <color red="0.0" green="0.0" blue="0.0" />
17      </rect>
18   </element>
19
20   <view name="LEDs only">
21
22      <!-- Background -->
23      <backdrop element="background">
24         <bounds left="0" top="0" right="450" bottom="69" />
25      </backdrop>
26
27      <!-- LEDs -->
28      <bezel name="digit0" element="digit">
29         <bounds left="10" top="10" right="44" bottom="59" />
30      </bezel>
31      <bezel name="digit1" element="digit">
32         <bounds left="54" top="10" right="88" bottom="59" />
33      </bezel>
34      <bezel name="digit2" element="digit">
35         <bounds left="98" top="10" right="132" bottom="59" />
36      </bezel>
37      <bezel name="digit3" element="digit">
38         <bounds left="142" top="10" right="176" bottom="59" />
39      </bezel>
40      <bezel name="digit4" element="digit">
41         <bounds left="186" top="10" right="220" bottom="59" />
42      </bezel>
43      <bezel name="digit5" element="digit">
44         <bounds left="230" top="10" right="264" bottom="59" />
45      </bezel>
46      <bezel name="digit6" element="digit">
47         <bounds left="274" top="10" right="308" bottom="59" />
48      </bezel>
49      <bezel name="digit7" element="digit">
50         <bounds left="318" top="10" right="352" bottom="59" />
51      </bezel>
52      <bezel name="digit8" element="digit">
53         <bounds left="362" top="10" right="396" bottom="59" />
54      </bezel>
55      <bezel name="digitx" element="digit">
56         <bounds left="406" top="10" right="440" bottom="59" />
57      </bezel><!-- This digit shows something but it does not exist on real unit.
58                   Change to 'digit9' to see it in action. -->
59
60   </view>
61
62</mamelayout>
src/mess/layout/sdk85.lay
r12101r12102
1<?xml version="1.0"?>
2<mamelayout version="2">
3   <element name="digit" defstate="0">
4      <led7seg>
5         <color red="0.75" green="0.0" blue="0.0" />
6      </led7seg>
7   </element>
8   <element name="background">
9      <rect>
10         <bounds left="0" top="0" right="1" bottom="1" />
11         <color red="0.0" green="0.0" blue="0.0" />
12      </rect>
13   </element>
14 
15   <view name="SDK85">
16      <!-- Black background -->
17      <bezel element="background">
18         <bounds left="1" top="1" right="600" bottom="480" />
19      </bezel>
20      <bezel name="digit0" element="digit">
21         <bounds left="107" top="105" right="155" bottom="185" />
22      </bezel>
23      <bezel name="digit1" element="digit">
24         <bounds left="155" top="105" right="203" bottom="185" />
25      </bezel>
26      <bezel name="digit2" element="digit">
27         <bounds left="203" top="105" right="251" bottom="185" />
28      </bezel>
29      <bezel name="digit3" element="digit">
30         <bounds left="251" top="105" right="299" bottom="185" />
31      </bezel>
32      <bezel name="digit4" element="digit">
33         <bounds left="323" top="105" right="370" bottom="185" />
34      </bezel>
35      <bezel name="digit5" element="digit">
36         <bounds left="371" top="105" right="418" bottom="185" />
37      </bezel>
38   </view>
39</mamelayout>
1<?xml version="1.0"?>
2<mamelayout version="2">
3   <element name="digit" defstate="0">
4      <led7seg>
5         <color red="0.75" green="0.0" blue="0.0" />
6      </led7seg>
7   </element>
8   <element name="background">
9      <rect>
10         <bounds left="0" top="0" right="1" bottom="1" />
11         <color red="0.0" green="0.0" blue="0.0" />
12      </rect>
13   </element>
14
15   <view name="SDK85">
16      <!-- Black background -->
17      <bezel element="background">
18         <bounds left="1" top="1" right="600" bottom="480" />
19      </bezel>
20      <bezel name="digit0" element="digit">
21         <bounds left="107" top="105" right="155" bottom="185" />
22      </bezel>
23      <bezel name="digit1" element="digit">
24         <bounds left="155" top="105" right="203" bottom="185" />
25      </bezel>
26      <bezel name="digit2" element="digit">
27         <bounds left="203" top="105" right="251" bottom="185" />
28      </bezel>
29      <bezel name="digit3" element="digit">
30         <bounds left="251" top="105" right="299" bottom="185" />
31      </bezel>
32      <bezel name="digit4" element="digit">
33         <bounds left="323" top="105" right="370" bottom="185" />
34      </bezel>
35      <bezel name="digit5" element="digit">
36         <bounds left="371" top="105" right="418" bottom="185" />
37      </bezel>
38   </view>
39</mamelayout>
src/mess/layout/sdk86.lay
r12101r12102
1<!-- sdk86.lay -->
2 
3<!-- 2011-06-23: Initial version.  [Robbbert] -->
4 
5<mamelayout version="2">
6 
7   <element name="digit" defstate="0">
8      <led7seg>
9         <color red="1.0" green="0.0" blue="0.0" />
10      </led7seg>
11   </element>
12 
13   <element name="background">
14      <rect>
15         <bounds left="0" top="0" right="1" bottom="1" />
16         <color red="0.0" green="0.0" blue="0.0" />
17      </rect>
18   </element>
19 
20   <view name="SDK86">
21 
22      <!-- Background -->
23      <backdrop element="background">
24         <bounds left="0" top="0" right="406" bottom="69" />
25      </backdrop>
26 
27      <!-- LEDs -->
28      <bezel name="digit7" element="digit">
29         <bounds left="10" top="10" right="44" bottom="59" />
30      </bezel>
31      <bezel name="digit6" element="digit">
32         <bounds left="54" top="10" right="88" bottom="59" />
33      </bezel>
34      <bezel name="digit5" element="digit">
35         <bounds left="98" top="10" right="132" bottom="59" />
36      </bezel>
37      <bezel name="digit4" element="digit">
38         <bounds left="142" top="10" right="176" bottom="59" />
39      </bezel>
40      <bezel name="digit3" element="digit">
41         <bounds left="186" top="10" right="220" bottom="59" />
42      </bezel>
43      <bezel name="digit2" element="digit">
44         <bounds left="230" top="10" right="264" bottom="59" />
45      </bezel>
46      <bezel name="digit1" element="digit">
47         <bounds left="274" top="10" right="308" bottom="59" />
48      </bezel>
49      <bezel name="digit0" element="digit">
50         <bounds left="318" top="10" right="352" bottom="59" />
51      </bezel>
52 
53   </view>
54 
55</mamelayout>
1<!-- sdk86.lay -->
2
3<!-- 2011-06-23: Initial version.  [Robbbert] -->
4
5<mamelayout version="2">
6
7   <element name="digit" defstate="0">
8      <led7seg>
9         <color red="1.0" green="0.0" blue="0.0" />
10      </led7seg>
11   </element>
12
13   <element name="background">
14      <rect>
15         <bounds left="0" top="0" right="1" bottom="1" />
16         <color red="0.0" green="0.0" blue="0.0" />
17      </rect>
18   </element>
19
20   <view name="SDK86">
21
22      <!-- Background -->
23      <backdrop element="background">
24         <bounds left="0" top="0" right="406" bottom="69" />
25      </backdrop>
26
27      <!-- LEDs -->
28      <bezel name="digit7" element="digit">
29         <bounds left="10" top="10" right="44" bottom="59" />
30      </bezel>
31      <bezel name="digit6" element="digit">
32         <bounds left="54" top="10" right="88" bottom="59" />
33      </bezel>
34      <bezel name="digit5" element="digit">
35         <bounds left="98" top="10" right="132" bottom="59" />
36      </bezel>
37      <bezel name="digit4" element="digit">
38         <bounds left="142" top="10" right="176" bottom="59" />
39      </bezel>
40      <bezel name="digit3" element="digit">
41         <bounds left="186" top="10" right="220" bottom="59" />
42      </bezel>
43      <bezel name="digit2" element="digit">
44         <bounds left="230" top="10" right="264" bottom="59" />
45      </bezel>
46      <bezel name="digit1" element="digit">
47         <bounds left="274" top="10" right="308" bottom="59" />
48      </bezel>
49      <bezel name="digit0" element="digit">
50         <bounds left="318" top="10" right="352" bottom="59" />
51      </bezel>
52
53   </view>
54
55</mamelayout>
src/ldplayer/ldplayer.lst
r12101r12102
1/******************************************************************************
2 
3    ldpdriv.c
4 
5    List of all enabled drivers in the system. This file is parsed by
6    makelist.exe, sorted, and output as C code describing the drivers.
7 
8****************************************************************************
9 
10    Copyright Aaron Giles
11    All rights reserved.
12 
13    Redistribution and use in source and binary forms, with or without
14    modification, are permitted provided that the following conditions are
15    met:
16 
17        * Redistributions of source code must retain the above copyright
18          notice, this list of conditions and the following disclaimer.
19        * Redistributions in binary form must reproduce the above copyright
20          notice, this list of conditions and the following disclaimer in
21          the documentation and/or other materials provided with the
22          distribution.
23        * Neither the name 'MAME' nor the names of its contributors may be
24          used to endorse or promote products derived from this software
25          without specific prior written permission.
26 
27    THIS SOFTWARE IS PROVIDED BY AARON GILES ''AS IS'' AND ANY EXPRESS OR
28    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30    DISCLAIMED. IN NO EVENT SHALL AARON GILES BE LIABLE FOR ANY DIRECT,
31    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35    STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
36    IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37    POSSIBILITY OF SUCH DAMAGE.
38 
39******************************************************************************/
40 
41ldv1000      // Pioneer LD-V1000
42pr8210      // Pioneer PR-8210
1/******************************************************************************
2
3    ldpdriv.c
4
5    List of all enabled drivers in the system. This file is parsed by
6    makelist.exe, sorted, and output as C code describing the drivers.
7
8****************************************************************************
9
10    Copyright Aaron Giles
11    All rights reserved.
12
13    Redistribution and use in source and binary forms, with or without
14    modification, are permitted provided that the following conditions are
15    met:
16
17        * Redistributions of source code must retain the above copyright
18          notice, this list of conditions and the following disclaimer.
19        * Redistributions in binary form must reproduce the above copyright
20          notice, this list of conditions and the following disclaimer in
21          the documentation and/or other materials provided with the
22          distribution.
23        * Neither the name 'MAME' nor the names of its contributors may be
24          used to endorse or promote products derived from this software
25          without specific prior written permission.
26
27    THIS SOFTWARE IS PROVIDED BY AARON GILES ''AS IS'' AND ANY EXPRESS OR
28    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
30    DISCLAIMED. IN NO EVENT SHALL AARON GILES BE LIABLE FOR ANY DIRECT,
31    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32    (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
33    SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
35    STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
36    IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37    POSSIBILITY OF SUCH DAMAGE.
38
39******************************************************************************/
40
41ldv1000      // Pioneer LD-V1000
42pr8210      // Pioneer PR-8210

Previous 15994 Revisions Next


© 1998-2018 The MESS Team