├── HTMLPAGE.h
├── LICENSE
├── PCEmulatorPlus.ino
├── README.md
├── bios.cpp
├── bios.h
├── biosrom.h
├── machine.cpp
├── machine.h
├── mconf.cpp
└── mconf.h
/HTMLPAGE.h:
--------------------------------------------------------------------------------
1 | /*
2 |
3 | NOTE: don't edit manually, this is generated by build.sh
4 |
5 | */
6 |
7 | const char* HTMLPAGE="
";
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | GNU GENERAL PUBLIC LICENSE
2 | Version 3, 29 June 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU General Public License is a free, copyleft license for
11 | software and other kinds of works.
12 |
13 | The licenses for most software and other practical works are designed
14 | to take away your freedom to share and change the works. By contrast,
15 | the GNU General Public License is intended to guarantee your freedom to
16 | share and change all versions of a program--to make sure it remains free
17 | software for all its users. We, the Free Software Foundation, use the
18 | GNU General Public License for most of our software; it applies also to
19 | any other work released this way by its authors. You can apply it to
20 | your programs, too.
21 |
22 | When we speak of free software, we are referring to freedom, not
23 | price. Our General Public Licenses are designed to make sure that you
24 | have the freedom to distribute copies of free software (and charge for
25 | them if you wish), that you receive source code or can get it if you
26 | want it, that you can change the software or use pieces of it in new
27 | free programs, and that you know you can do these things.
28 |
29 | To protect your rights, we need to prevent others from denying you
30 | these rights or asking you to surrender the rights. Therefore, you have
31 | certain responsibilities if you distribute copies of the software, or if
32 | you modify it: responsibilities to respect the freedom of others.
33 |
34 | For example, if you distribute copies of such a program, whether
35 | gratis or for a fee, you must pass on to the recipients the same
36 | freedoms that you received. You must make sure that they, too, receive
37 | or can get the source code. And you must show them these terms so they
38 | know their rights.
39 |
40 | Developers that use the GNU GPL protect your rights with two steps:
41 | (1) assert copyright on the software, and (2) offer you this License
42 | giving you legal permission to copy, distribute and/or modify it.
43 |
44 | For the developers' and authors' protection, the GPL clearly explains
45 | that there is no warranty for this free software. For both users' and
46 | authors' sake, the GPL requires that modified versions be marked as
47 | changed, so that their problems will not be attributed erroneously to
48 | authors of previous versions.
49 |
50 | Some devices are designed to deny users access to install or run
51 | modified versions of the software inside them, although the manufacturer
52 | can do so. This is fundamentally incompatible with the aim of
53 | protecting users' freedom to change the software. The systematic
54 | pattern of such abuse occurs in the area of products for individuals to
55 | use, which is precisely where it is most unacceptable. Therefore, we
56 | have designed this version of the GPL to prohibit the practice for those
57 | products. If such problems arise substantially in other domains, we
58 | stand ready to extend this provision to those domains in future versions
59 | of the GPL, as needed to protect the freedom of users.
60 |
61 | Finally, every program is threatened constantly by software patents.
62 | States should not allow patents to restrict development and use of
63 | software on general-purpose computers, but in those that do, we wish to
64 | avoid the special danger that patents applied to a free program could
65 | make it effectively proprietary. To prevent this, the GPL assures that
66 | patents cannot be used to render the program non-free.
67 |
68 | The precise terms and conditions for copying, distribution and
69 | modification follow.
70 |
71 | TERMS AND CONDITIONS
72 |
73 | 0. Definitions.
74 |
75 | "This License" refers to version 3 of the GNU General Public License.
76 |
77 | "Copyright" also means copyright-like laws that apply to other kinds of
78 | works, such as semiconductor masks.
79 |
80 | "The Program" refers to any copyrightable work licensed under this
81 | License. Each licensee is addressed as "you". "Licensees" and
82 | "recipients" may be individuals or organizations.
83 |
84 | To "modify" a work means to copy from or adapt all or part of the work
85 | in a fashion requiring copyright permission, other than the making of an
86 | exact copy. The resulting work is called a "modified version" of the
87 | earlier work or a work "based on" the earlier work.
88 |
89 | A "covered work" means either the unmodified Program or a work based
90 | on the Program.
91 |
92 | To "propagate" a work means to do anything with it that, without
93 | permission, would make you directly or secondarily liable for
94 | infringement under applicable copyright law, except executing it on a
95 | computer or modifying a private copy. Propagation includes copying,
96 | distribution (with or without modification), making available to the
97 | public, and in some countries other activities as well.
98 |
99 | To "convey" a work means any kind of propagation that enables other
100 | parties to make or receive copies. Mere interaction with a user through
101 | a computer network, with no transfer of a copy, is not conveying.
102 |
103 | An interactive user interface displays "Appropriate Legal Notices"
104 | to the extent that it includes a convenient and prominently visible
105 | feature that (1) displays an appropriate copyright notice, and (2)
106 | tells the user that there is no warranty for the work (except to the
107 | extent that warranties are provided), that licensees may convey the
108 | work under this License, and how to view a copy of this License. If
109 | the interface presents a list of user commands or options, such as a
110 | menu, a prominent item in the list meets this criterion.
111 |
112 | 1. Source Code.
113 |
114 | The "source code" for a work means the preferred form of the work
115 | for making modifications to it. "Object code" means any non-source
116 | form of a work.
117 |
118 | A "Standard Interface" means an interface that either is an official
119 | standard defined by a recognized standards body, or, in the case of
120 | interfaces specified for a particular programming language, one that
121 | is widely used among developers working in that language.
122 |
123 | The "System Libraries" of an executable work include anything, other
124 | than the work as a whole, that (a) is included in the normal form of
125 | packaging a Major Component, but which is not part of that Major
126 | Component, and (b) serves only to enable use of the work with that
127 | Major Component, or to implement a Standard Interface for which an
128 | implementation is available to the public in source code form. A
129 | "Major Component", in this context, means a major essential component
130 | (kernel, window system, and so on) of the specific operating system
131 | (if any) on which the executable work runs, or a compiler used to
132 | produce the work, or an object code interpreter used to run it.
133 |
134 | The "Corresponding Source" for a work in object code form means all
135 | the source code needed to generate, install, and (for an executable
136 | work) run the object code and to modify the work, including scripts to
137 | control those activities. However, it does not include the work's
138 | System Libraries, or general-purpose tools or generally available free
139 | programs which are used unmodified in performing those activities but
140 | which are not part of the work. For example, Corresponding Source
141 | includes interface definition files associated with source files for
142 | the work, and the source code for shared libraries and dynamically
143 | linked subprograms that the work is specifically designed to require,
144 | such as by intimate data communication or control flow between those
145 | subprograms and other parts of the work.
146 |
147 | The Corresponding Source need not include anything that users
148 | can regenerate automatically from other parts of the Corresponding
149 | Source.
150 |
151 | The Corresponding Source for a work in source code form is that
152 | same work.
153 |
154 | 2. Basic Permissions.
155 |
156 | All rights granted under this License are granted for the term of
157 | copyright on the Program, and are irrevocable provided the stated
158 | conditions are met. This License explicitly affirms your unlimited
159 | permission to run the unmodified Program. The output from running a
160 | covered work is covered by this License only if the output, given its
161 | content, constitutes a covered work. This License acknowledges your
162 | rights of fair use or other equivalent, as provided by copyright law.
163 |
164 | You may make, run and propagate covered works that you do not
165 | convey, without conditions so long as your license otherwise remains
166 | in force. You may convey covered works to others for the sole purpose
167 | of having them make modifications exclusively for you, or provide you
168 | with facilities for running those works, provided that you comply with
169 | the terms of this License in conveying all material for which you do
170 | not control copyright. Those thus making or running the covered works
171 | for you must do so exclusively on your behalf, under your direction
172 | and control, on terms that prohibit them from making any copies of
173 | your copyrighted material outside their relationship with you.
174 |
175 | Conveying under any other circumstances is permitted solely under
176 | the conditions stated below. Sublicensing is not allowed; section 10
177 | makes it unnecessary.
178 |
179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
180 |
181 | No covered work shall be deemed part of an effective technological
182 | measure under any applicable law fulfilling obligations under article
183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
184 | similar laws prohibiting or restricting circumvention of such
185 | measures.
186 |
187 | When you convey a covered work, you waive any legal power to forbid
188 | circumvention of technological measures to the extent such circumvention
189 | is effected by exercising rights under this License with respect to
190 | the covered work, and you disclaim any intention to limit operation or
191 | modification of the work as a means of enforcing, against the work's
192 | users, your or third parties' legal rights to forbid circumvention of
193 | technological measures.
194 |
195 | 4. Conveying Verbatim Copies.
196 |
197 | You may convey verbatim copies of the Program's source code as you
198 | receive it, in any medium, provided that you conspicuously and
199 | appropriately publish on each copy an appropriate copyright notice;
200 | keep intact all notices stating that this License and any
201 | non-permissive terms added in accord with section 7 apply to the code;
202 | keep intact all notices of the absence of any warranty; and give all
203 | recipients a copy of this License along with the Program.
204 |
205 | You may charge any price or no price for each copy that you convey,
206 | and you may offer support or warranty protection for a fee.
207 |
208 | 5. Conveying Modified Source Versions.
209 |
210 | You may convey a work based on the Program, or the modifications to
211 | produce it from the Program, in the form of source code under the
212 | terms of section 4, provided that you also meet all of these conditions:
213 |
214 | a) The work must carry prominent notices stating that you modified
215 | it, and giving a relevant date.
216 |
217 | b) The work must carry prominent notices stating that it is
218 | released under this License and any conditions added under section
219 | 7. This requirement modifies the requirement in section 4 to
220 | "keep intact all notices".
221 |
222 | c) You must license the entire work, as a whole, under this
223 | License to anyone who comes into possession of a copy. This
224 | License will therefore apply, along with any applicable section 7
225 | additional terms, to the whole of the work, and all its parts,
226 | regardless of how they are packaged. This License gives no
227 | permission to license the work in any other way, but it does not
228 | invalidate such permission if you have separately received it.
229 |
230 | d) If the work has interactive user interfaces, each must display
231 | Appropriate Legal Notices; however, if the Program has interactive
232 | interfaces that do not display Appropriate Legal Notices, your
233 | work need not make them do so.
234 |
235 | A compilation of a covered work with other separate and independent
236 | works, which are not by their nature extensions of the covered work,
237 | and which are not combined with it such as to form a larger program,
238 | in or on a volume of a storage or distribution medium, is called an
239 | "aggregate" if the compilation and its resulting copyright are not
240 | used to limit the access or legal rights of the compilation's users
241 | beyond what the individual works permit. Inclusion of a covered work
242 | in an aggregate does not cause this License to apply to the other
243 | parts of the aggregate.
244 |
245 | 6. Conveying Non-Source Forms.
246 |
247 | You may convey a covered work in object code form under the terms
248 | of sections 4 and 5, provided that you also convey the
249 | machine-readable Corresponding Source under the terms of this License,
250 | in one of these ways:
251 |
252 | a) Convey the object code in, or embodied in, a physical product
253 | (including a physical distribution medium), accompanied by the
254 | Corresponding Source fixed on a durable physical medium
255 | customarily used for software interchange.
256 |
257 | b) Convey the object code in, or embodied in, a physical product
258 | (including a physical distribution medium), accompanied by a
259 | written offer, valid for at least three years and valid for as
260 | long as you offer spare parts or customer support for that product
261 | model, to give anyone who possesses the object code either (1) a
262 | copy of the Corresponding Source for all the software in the
263 | product that is covered by this License, on a durable physical
264 | medium customarily used for software interchange, for a price no
265 | more than your reasonable cost of physically performing this
266 | conveying of source, or (2) access to copy the
267 | Corresponding Source from a network server at no charge.
268 |
269 | c) Convey individual copies of the object code with a copy of the
270 | written offer to provide the Corresponding Source. This
271 | alternative is allowed only occasionally and noncommercially, and
272 | only if you received the object code with such an offer, in accord
273 | with subsection 6b.
274 |
275 | d) Convey the object code by offering access from a designated
276 | place (gratis or for a charge), and offer equivalent access to the
277 | Corresponding Source in the same way through the same place at no
278 | further charge. You need not require recipients to copy the
279 | Corresponding Source along with the object code. If the place to
280 | copy the object code is a network server, the Corresponding Source
281 | may be on a different server (operated by you or a third party)
282 | that supports equivalent copying facilities, provided you maintain
283 | clear directions next to the object code saying where to find the
284 | Corresponding Source. Regardless of what server hosts the
285 | Corresponding Source, you remain obligated to ensure that it is
286 | available for as long as needed to satisfy these requirements.
287 |
288 | e) Convey the object code using peer-to-peer transmission, provided
289 | you inform other peers where the object code and Corresponding
290 | Source of the work are being offered to the general public at no
291 | charge under subsection 6d.
292 |
293 | A separable portion of the object code, whose source code is excluded
294 | from the Corresponding Source as a System Library, need not be
295 | included in conveying the object code work.
296 |
297 | A "User Product" is either (1) a "consumer product", which means any
298 | tangible personal property which is normally used for personal, family,
299 | or household purposes, or (2) anything designed or sold for incorporation
300 | into a dwelling. In determining whether a product is a consumer product,
301 | doubtful cases shall be resolved in favor of coverage. For a particular
302 | product received by a particular user, "normally used" refers to a
303 | typical or common use of that class of product, regardless of the status
304 | of the particular user or of the way in which the particular user
305 | actually uses, or expects or is expected to use, the product. A product
306 | is a consumer product regardless of whether the product has substantial
307 | commercial, industrial or non-consumer uses, unless such uses represent
308 | the only significant mode of use of the product.
309 |
310 | "Installation Information" for a User Product means any methods,
311 | procedures, authorization keys, or other information required to install
312 | and execute modified versions of a covered work in that User Product from
313 | a modified version of its Corresponding Source. The information must
314 | suffice to ensure that the continued functioning of the modified object
315 | code is in no case prevented or interfered with solely because
316 | modification has been made.
317 |
318 | If you convey an object code work under this section in, or with, or
319 | specifically for use in, a User Product, and the conveying occurs as
320 | part of a transaction in which the right of possession and use of the
321 | User Product is transferred to the recipient in perpetuity or for a
322 | fixed term (regardless of how the transaction is characterized), the
323 | Corresponding Source conveyed under this section must be accompanied
324 | by the Installation Information. But this requirement does not apply
325 | if neither you nor any third party retains the ability to install
326 | modified object code on the User Product (for example, the work has
327 | been installed in ROM).
328 |
329 | The requirement to provide Installation Information does not include a
330 | requirement to continue to provide support service, warranty, or updates
331 | for a work that has been modified or installed by the recipient, or for
332 | the User Product in which it has been modified or installed. Access to a
333 | network may be denied when the modification itself materially and
334 | adversely affects the operation of the network or violates the rules and
335 | protocols for communication across the network.
336 |
337 | Corresponding Source conveyed, and Installation Information provided,
338 | in accord with this section must be in a format that is publicly
339 | documented (and with an implementation available to the public in
340 | source code form), and must require no special password or key for
341 | unpacking, reading or copying.
342 |
343 | 7. Additional Terms.
344 |
345 | "Additional permissions" are terms that supplement the terms of this
346 | License by making exceptions from one or more of its conditions.
347 | Additional permissions that are applicable to the entire Program shall
348 | be treated as though they were included in this License, to the extent
349 | that they are valid under applicable law. If additional permissions
350 | apply only to part of the Program, that part may be used separately
351 | under those permissions, but the entire Program remains governed by
352 | this License without regard to the additional permissions.
353 |
354 | When you convey a copy of a covered work, you may at your option
355 | remove any additional permissions from that copy, or from any part of
356 | it. (Additional permissions may be written to require their own
357 | removal in certain cases when you modify the work.) You may place
358 | additional permissions on material, added by you to a covered work,
359 | for which you have or can give appropriate copyright permission.
360 |
361 | Notwithstanding any other provision of this License, for material you
362 | add to a covered work, you may (if authorized by the copyright holders of
363 | that material) supplement the terms of this License with terms:
364 |
365 | a) Disclaiming warranty or limiting liability differently from the
366 | terms of sections 15 and 16 of this License; or
367 |
368 | b) Requiring preservation of specified reasonable legal notices or
369 | author attributions in that material or in the Appropriate Legal
370 | Notices displayed by works containing it; or
371 |
372 | c) Prohibiting misrepresentation of the origin of that material, or
373 | requiring that modified versions of such material be marked in
374 | reasonable ways as different from the original version; or
375 |
376 | d) Limiting the use for publicity purposes of names of licensors or
377 | authors of the material; or
378 |
379 | e) Declining to grant rights under trademark law for use of some
380 | trade names, trademarks, or service marks; or
381 |
382 | f) Requiring indemnification of licensors and authors of that
383 | material by anyone who conveys the material (or modified versions of
384 | it) with contractual assumptions of liability to the recipient, for
385 | any liability that these contractual assumptions directly impose on
386 | those licensors and authors.
387 |
388 | All other non-permissive additional terms are considered "further
389 | restrictions" within the meaning of section 10. If the Program as you
390 | received it, or any part of it, contains a notice stating that it is
391 | governed by this License along with a term that is a further
392 | restriction, you may remove that term. If a license document contains
393 | a further restriction but permits relicensing or conveying under this
394 | License, you may add to a covered work material governed by the terms
395 | of that license document, provided that the further restriction does
396 | not survive such relicensing or conveying.
397 |
398 | If you add terms to a covered work in accord with this section, you
399 | must place, in the relevant source files, a statement of the
400 | additional terms that apply to those files, or a notice indicating
401 | where to find the applicable terms.
402 |
403 | Additional terms, permissive or non-permissive, may be stated in the
404 | form of a separately written license, or stated as exceptions;
405 | the above requirements apply either way.
406 |
407 | 8. Termination.
408 |
409 | You may not propagate or modify a covered work except as expressly
410 | provided under this License. Any attempt otherwise to propagate or
411 | modify it is void, and will automatically terminate your rights under
412 | this License (including any patent licenses granted under the third
413 | paragraph of section 11).
414 |
415 | However, if you cease all violation of this License, then your
416 | license from a particular copyright holder is reinstated (a)
417 | provisionally, unless and until the copyright holder explicitly and
418 | finally terminates your license, and (b) permanently, if the copyright
419 | holder fails to notify you of the violation by some reasonable means
420 | prior to 60 days after the cessation.
421 |
422 | Moreover, your license from a particular copyright holder is
423 | reinstated permanently if the copyright holder notifies you of the
424 | violation by some reasonable means, this is the first time you have
425 | received notice of violation of this License (for any work) from that
426 | copyright holder, and you cure the violation prior to 30 days after
427 | your receipt of the notice.
428 |
429 | Termination of your rights under this section does not terminate the
430 | licenses of parties who have received copies or rights from you under
431 | this License. If your rights have been terminated and not permanently
432 | reinstated, you do not qualify to receive new licenses for the same
433 | material under section 10.
434 |
435 | 9. Acceptance Not Required for Having Copies.
436 |
437 | You are not required to accept this License in order to receive or
438 | run a copy of the Program. Ancillary propagation of a covered work
439 | occurring solely as a consequence of using peer-to-peer transmission
440 | to receive a copy likewise does not require acceptance. However,
441 | nothing other than this License grants you permission to propagate or
442 | modify any covered work. These actions infringe copyright if you do
443 | not accept this License. Therefore, by modifying or propagating a
444 | covered work, you indicate your acceptance of this License to do so.
445 |
446 | 10. Automatic Licensing of Downstream Recipients.
447 |
448 | Each time you convey a covered work, the recipient automatically
449 | receives a license from the original licensors, to run, modify and
450 | propagate that work, subject to this License. You are not responsible
451 | for enforcing compliance by third parties with this License.
452 |
453 | An "entity transaction" is a transaction transferring control of an
454 | organization, or substantially all assets of one, or subdividing an
455 | organization, or merging organizations. If propagation of a covered
456 | work results from an entity transaction, each party to that
457 | transaction who receives a copy of the work also receives whatever
458 | licenses to the work the party's predecessor in interest had or could
459 | give under the previous paragraph, plus a right to possession of the
460 | Corresponding Source of the work from the predecessor in interest, if
461 | the predecessor has it or can get it with reasonable efforts.
462 |
463 | You may not impose any further restrictions on the exercise of the
464 | rights granted or affirmed under this License. For example, you may
465 | not impose a license fee, royalty, or other charge for exercise of
466 | rights granted under this License, and you may not initiate litigation
467 | (including a cross-claim or counterclaim in a lawsuit) alleging that
468 | any patent claim is infringed by making, using, selling, offering for
469 | sale, or importing the Program or any portion of it.
470 |
471 | 11. Patents.
472 |
473 | A "contributor" is a copyright holder who authorizes use under this
474 | License of the Program or a work on which the Program is based. The
475 | work thus licensed is called the contributor's "contributor version".
476 |
477 | A contributor's "essential patent claims" are all patent claims
478 | owned or controlled by the contributor, whether already acquired or
479 | hereafter acquired, that would be infringed by some manner, permitted
480 | by this License, of making, using, or selling its contributor version,
481 | but do not include claims that would be infringed only as a
482 | consequence of further modification of the contributor version. For
483 | purposes of this definition, "control" includes the right to grant
484 | patent sublicenses in a manner consistent with the requirements of
485 | this License.
486 |
487 | Each contributor grants you a non-exclusive, worldwide, royalty-free
488 | patent license under the contributor's essential patent claims, to
489 | make, use, sell, offer for sale, import and otherwise run, modify and
490 | propagate the contents of its contributor version.
491 |
492 | In the following three paragraphs, a "patent license" is any express
493 | agreement or commitment, however denominated, not to enforce a patent
494 | (such as an express permission to practice a patent or covenant not to
495 | sue for patent infringement). To "grant" such a patent license to a
496 | party means to make such an agreement or commitment not to enforce a
497 | patent against the party.
498 |
499 | If you convey a covered work, knowingly relying on a patent license,
500 | and the Corresponding Source of the work is not available for anyone
501 | to copy, free of charge and under the terms of this License, through a
502 | publicly available network server or other readily accessible means,
503 | then you must either (1) cause the Corresponding Source to be so
504 | available, or (2) arrange to deprive yourself of the benefit of the
505 | patent license for this particular work, or (3) arrange, in a manner
506 | consistent with the requirements of this License, to extend the patent
507 | license to downstream recipients. "Knowingly relying" means you have
508 | actual knowledge that, but for the patent license, your conveying the
509 | covered work in a country, or your recipient's use of the covered work
510 | in a country, would infringe one or more identifiable patents in that
511 | country that you have reason to believe are valid.
512 |
513 | If, pursuant to or in connection with a single transaction or
514 | arrangement, you convey, or propagate by procuring conveyance of, a
515 | covered work, and grant a patent license to some of the parties
516 | receiving the covered work authorizing them to use, propagate, modify
517 | or convey a specific copy of the covered work, then the patent license
518 | you grant is automatically extended to all recipients of the covered
519 | work and works based on it.
520 |
521 | A patent license is "discriminatory" if it does not include within
522 | the scope of its coverage, prohibits the exercise of, or is
523 | conditioned on the non-exercise of one or more of the rights that are
524 | specifically granted under this License. You may not convey a covered
525 | work if you are a party to an arrangement with a third party that is
526 | in the business of distributing software, under which you make payment
527 | to the third party based on the extent of your activity of conveying
528 | the work, and under which the third party grants, to any of the
529 | parties who would receive the covered work from you, a discriminatory
530 | patent license (a) in connection with copies of the covered work
531 | conveyed by you (or copies made from those copies), or (b) primarily
532 | for and in connection with specific products or compilations that
533 | contain the covered work, unless you entered into that arrangement,
534 | or that patent license was granted, prior to 28 March 2007.
535 |
536 | Nothing in this License shall be construed as excluding or limiting
537 | any implied license or other defenses to infringement that may
538 | otherwise be available to you under applicable patent law.
539 |
540 | 12. No Surrender of Others' Freedom.
541 |
542 | If conditions are imposed on you (whether by court order, agreement or
543 | otherwise) that contradict the conditions of this License, they do not
544 | excuse you from the conditions of this License. If you cannot convey a
545 | covered work so as to satisfy simultaneously your obligations under this
546 | License and any other pertinent obligations, then as a consequence you may
547 | not convey it at all. For example, if you agree to terms that obligate you
548 | to collect a royalty for further conveying from those to whom you convey
549 | the Program, the only way you could satisfy both those terms and this
550 | License would be to refrain entirely from conveying the Program.
551 |
552 | 13. Use with the GNU Affero General Public License.
553 |
554 | Notwithstanding any other provision of this License, you have
555 | permission to link or combine any covered work with a work licensed
556 | under version 3 of the GNU Affero General Public License into a single
557 | combined work, and to convey the resulting work. The terms of this
558 | License will continue to apply to the part which is the covered work,
559 | but the special requirements of the GNU Affero General Public License,
560 | section 13, concerning interaction through a network will apply to the
561 | combination as such.
562 |
563 | 14. Revised Versions of this License.
564 |
565 | The Free Software Foundation may publish revised and/or new versions of
566 | the GNU General Public License from time to time. Such new versions will
567 | be similar in spirit to the present version, but may differ in detail to
568 | address new problems or concerns.
569 |
570 | Each version is given a distinguishing version number. If the
571 | Program specifies that a certain numbered version of the GNU General
572 | Public License "or any later version" applies to it, you have the
573 | option of following the terms and conditions either of that numbered
574 | version or of any later version published by the Free Software
575 | Foundation. If the Program does not specify a version number of the
576 | GNU General Public License, you may choose any version ever published
577 | by the Free Software Foundation.
578 |
579 | If the Program specifies that a proxy can decide which future
580 | versions of the GNU General Public License can be used, that proxy's
581 | public statement of acceptance of a version permanently authorizes you
582 | to choose that version for the Program.
583 |
584 | Later license versions may give you additional or different
585 | permissions. However, no additional obligations are imposed on any
586 | author or copyright holder as a result of your choosing to follow a
587 | later version.
588 |
589 | 15. Disclaimer of Warranty.
590 |
591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
599 |
600 | 16. Limitation of Liability.
601 |
602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
610 | SUCH DAMAGES.
611 |
612 | 17. Interpretation of Sections 15 and 16.
613 |
614 | If the disclaimer of warranty and limitation of liability provided
615 | above cannot be given local legal effect according to their terms,
616 | reviewing courts shall apply local law that most closely approximates
617 | an absolute waiver of all civil liability in connection with the
618 | Program, unless a warranty or assumption of liability accompanies a
619 | copy of the Program in return for a fee.
620 |
621 | END OF TERMS AND CONDITIONS
622 |
623 | How to Apply These Terms to Your New Programs
624 |
625 | If you develop a new program, and you want it to be of the greatest
626 | possible use to the public, the best way to achieve this is to make it
627 | free software which everyone can redistribute and change under these terms.
628 |
629 | To do so, attach the following notices to the program. It is safest
630 | to attach them to the start of each source file to most effectively
631 | state the exclusion of warranty; and each file should have at least
632 | the "copyright" line and a pointer to where the full notice is found.
633 |
634 |
635 | Copyright (C)
636 |
637 | This program is free software: you can redistribute it and/or modify
638 | it under the terms of the GNU General Public License as published by
639 | the Free Software Foundation, either version 3 of the License, or
640 | (at your option) any later version.
641 |
642 | This program is distributed in the hope that it will be useful,
643 | but WITHOUT ANY WARRANTY; without even the implied warranty of
644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
645 | GNU General Public License for more details.
646 |
647 | You should have received a copy of the GNU General Public License
648 | along with this program. If not, see .
649 |
650 | Also add information on how to contact you by electronic and paper mail.
651 |
652 | If the program does terminal interaction, make it output a short
653 | notice like this when it starts in an interactive mode:
654 |
655 | Copyright (C)
656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
657 | This is free software, and you are welcome to redistribute it
658 | under certain conditions; type `show c' for details.
659 |
660 | The hypothetical commands `show w' and `show c' should show the appropriate
661 | parts of the General Public License. Of course, your program's commands
662 | might be different; for a GUI interface, you would use an "about box".
663 |
664 | You should also get your employer (if you work as a programmer) or school,
665 | if any, to sign a "copyright disclaimer" for the program, if necessary.
666 | For more information on this, and how to apply and follow the GNU GPL, see
667 | .
668 |
669 | The GNU General Public License does not permit incorporating your program
670 | into proprietary programs. If your program is a subroutine library, you
671 | may consider it more useful to permit linking proprietary applications with
672 | the library. If this is what you want to do, use the GNU Lesser General
673 | Public License instead of this License. But first, please read
674 | .
675 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # PCEmulatorPlus
2 | The PCEmulator by Fabrizio Di Vittorio plus applications for a more complete system. For the TTGO VGA32 1.4 board and compatibles.
3 |
4 | - What is this for?
5 |
6 | This code allows you to use a microcontroller-based board (Esp32) as if it were a computer. This is accomplished by combining a 16-bit PC emulator (8086-CGA/Hercules) with more modern applications (MP3 player, JPG image viewer, microSD card file manager, etc.).
7 |
8 | - What are the advantages?
9 |
10 | This allows you to have a low-power, cheap, offgrid, and highly modular computer. Suitable for extreme contexts (lack of electricity, market crisis, etc.)
11 |
12 | - What are the disadvantages?
13 |
14 | The computer built using this code has a much lower processing capacity than what is normally seen. You won't be able to watch videos (properly), surf the Internet or play photorealistic action games.
15 |
16 | Apps:
17 |
18 | **With Display:**
19 | - PCEmulator (IBM PC/XT emulator with CGA and Hercules graphics);
20 | - ChatterBox (Horizontal chat server);
21 | - File Browser;
22 | - Jpg View (see images from SD card) (only 400x300, 64 colors max.);
23 | - Web Radios (Online Radios from many countrys);
24 | - Audio Player (play .mp3, .wav, etc. from SD);
25 | - Wi-fi Safe (test security of wifi);
26 | - Commodore VIC-20 emulator (Only PAL, only crt and prg files).
27 |
28 | **With Audio:**
29 | 1) Web Radios
30 | 2) Chat Server
31 | 3) Audio Maker
32 | 4) Timer
33 | 5) Audio Player (play .mp3, .wav, etc. from SD);
34 |
35 | See the wiki for limitations and how to use this system.
36 |
37 | The system is being designed to work even if part of the hardware is unavailable, thus avoiding the production of electronic waste in the event of a component failure.
38 | What works without...
39 | - microSD card: ChatterBox, Web Radios, Wi-fi Safe, Audio Maker, VIC-20 emulator
40 | - Sound output: Wi-fi Safe, File Browser, Jpg View, PCEmulator, VIC-20 emulator (some apps)
41 | - Wi-Fi: Audio Player, File Browser, Jpg View, PCEmulator, Audio Maker, VIC-20 emulator
42 | - Mouse: PCEmulator, File Browser, Jpg View, Audio Player, Timer, Audio Maker, VIC-20 emulator
43 | - Keyboard: File Browser, ChatterBox, Wi-fi Safe, VIC-20 emulator (some apps)
44 | - PSRAM: ChatterBox, Wi-fi Safe, File Browser, Timer,
45 | - VGA output: ChatterBox, Web Radios, Audio Maker, Audio Player, Timer,
46 |
47 | If you don't use the video output you will need to use a keyboard and the audio output.
48 | Theoretically, some applications can even be used by a blind person if an accessible keyboard is connected.
49 |
50 | Disks for the PCEmulator:
51 | https://drive.google.com/drive/folders/1OFRNx3wDCHZ4ajmrT9v_o90OhCe7HoOJ?usp=share_link
52 |
53 | Used libs:
54 | - FabGL by Fabrizio Di Vittorio version 1.0.9 https://github.com/fdivitto/FabGL/releases/tag/v1.0.9
55 | - TJpg_Decoder by Bodmer version 0.0.2 https://github.com/Bodmer/TJpg_Decoder/releases/tag/0.0.2
56 | - ESP32-audioI2S por Wolle version 3.0.0 https://github.com/schreibfaul1/ESP32-audioI2S/releases/tag/3.0.0
57 | And
58 | - Board arduino-esp32 by espressif version 1.0.6 https://github.com/espressif/arduino-esp32/releases/tag/1.0.6
59 | - Arduino IDE version 1.8.13 https://github.com/arduino/Arduino/releases/tag/1.8.13
60 |
--------------------------------------------------------------------------------
/bios.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) -
3 | Copyright (c) 2019-2022 Fabrizio Di Vittorio.
4 | All rights reserved.
5 |
6 |
7 | * Please contact fdivitto2013@gmail.com if you need a commercial license.
8 |
9 |
10 | * This library and related software is available under GPL v3.
11 |
12 | FabGL is free software: you can redistribute it and/or modify
13 | it under the terms of the GNU General Public License as published by
14 | the Free Software Foundation, either version 3 of the License, or
15 | (at your option) any later version.
16 |
17 | FabGL is distributed in the hope that it will be useful,
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 | GNU General Public License for more details.
21 |
22 | You should have received a copy of the GNU General Public License
23 | along with FabGL. If not, see .
24 | */
25 |
26 |
27 |
28 | #include "bios.h"
29 | #include "machine.h"
30 | #include "emudevs/i8086.h"
31 |
32 |
33 | using fabgl::i8086;
34 |
35 |
36 | // To compile BIOS execute "cbios.sh"
37 | static const uint8_t biosrom[] = {
38 | #include "biosrom.h"
39 | };
40 |
41 |
42 | BIOS::BIOS()
43 | : m_memory(nullptr)
44 | {
45 | }
46 |
47 |
48 | void BIOS::init(Machine * machine)
49 | {
50 | m_machine = machine;
51 | m_memory = m_machine->memory();
52 | m_i8042 = m_machine->getI8042();
53 | m_keyboard = m_i8042->keyboard();
54 | m_mouse = m_i8042->mouse();
55 | m_MC146818 = m_machine->getMC146818();
56 |
57 | // copy bios
58 | memcpy(m_memory + BIOS_ADDR, biosrom, sizeof(biosrom));
59 |
60 | // setup bootstrap code (starting from 0xFFFF0)
61 | // for example: "JMP FC00:0100"
62 | m_memory[0xffff0] = 0xea;
63 | m_memory[0xffff1] = BIOS_OFF & 0xff;
64 | m_memory[0xffff2] = BIOS_OFF >> 8;
65 | m_memory[0xffff3] = BIOS_SEG & 0xff;
66 | m_memory[0xffff4] = BIOS_SEG >> 8;
67 | }
68 |
69 |
70 | void BIOS::reset()
71 | {
72 | m_kbdScancodeComp = 0;
73 | m_memory[BIOS_DATAAREA_ADDR + BIOS_NUMHD] = (bool)(m_machine->disk(2)) + (bool)(m_machine->disk(3));
74 | }
75 |
76 |
77 | // drive:
78 | // 0 = floppy 0 (get address from INT 1E)
79 | // 1 = floppy 1 (get address from INT 1E)
80 | // 2 = HD 0 (get address from INT 41)
81 | // 3 = HD 1 (get address from INT 46)
82 | uint32_t BIOS::getDriveMediaTableAddr(int drive)
83 | {
84 | int intNum = drive < 2 ? 0x1e : (drive == 2 ? 0x41 : 0x46);
85 | uint16_t * intAddr = (uint16_t*)(m_memory + intNum * 4);
86 | return intAddr[0] + intAddr[1] * 16;
87 | }
88 |
89 |
90 | bool BIOS::checkDriveMediaType(int drive)
91 | {
92 | if (drive < 2) {
93 | // FDD
94 | if (m_mediaType[drive] == mediaUnknown) {
95 | MediaType mt = mediaUnknown;
96 | int h = m_machine->diskHeads(drive);
97 | int t = m_machine->diskCylinders(drive);
98 | int s = m_machine->diskSectors(drive);
99 | if (h == 1 && t == 40 && s == 8)
100 | mt = floppy160KB;
101 | else if (h == 1 && t == 40 && s == 9)
102 | mt = floppy180KB;
103 | else if (h == 2 && t == 40 && s == 8)
104 | mt = floppy320KB;
105 | else if (h == 2 && t == 40 && s == 9)
106 | mt = floppy360KB;
107 | else if (h == 2 && t == 80 && s == 9)
108 | mt = floppy720KB;
109 | else if (h == 2 && t == 80 && s == 15)
110 | mt = floppy1M2K;
111 | else if (h == 2 && t == 80 && s == 18)
112 | mt = floppy1M44K;
113 | else if (h == 2 && t == 80 && s == 36)
114 | mt = floppy2M88K;
115 | setDriveMediaType(drive, mt);
116 | }
117 | } else {
118 | // HDD
119 | if (m_machine->disk(drive))
120 | setDriveMediaType(drive, HDD);
121 | }
122 | return m_mediaType[drive] != mediaUnknown;
123 | }
124 |
125 |
126 | void BIOS::setDriveMediaType(int drive, MediaType media)
127 | {
128 | m_mediaType[drive] = media;
129 |
130 | if (drive < 2) {
131 | // FDD
132 |
133 | // updates BIOS data area
134 | uint8_t knownMedia = 0x10; // default set bit 4 (known media)
135 | uint8_t doubleStepping = 0x00; // reset bit 5 (double stepping)
136 | uint8_t dataRate = 0x00;
137 | uint8_t defs = 0x00;
138 | switch (media) {
139 | case floppy160KB:
140 | case floppy180KB:
141 | case floppy320KB:
142 | case floppy360KB:
143 | doubleStepping = 0x20; // set bit 5 (double stepping)
144 | dataRate = 0b01000000; // 300 KBS
145 | defs = 0b00000100; // Known 360K media in 1.2MB drive
146 | break;
147 | case floppy720KB:
148 | dataRate = 0b10000000; // 250 KBS
149 | defs = 0b00000111; // 720K media in 720K drive or 1.44MB media in 1.44MB drive
150 | break;
151 | case floppy1M2K:
152 | dataRate = 0b00000000; // 500 KBS
153 | defs = 0b00000101; // Known 1.2MB media in 1.2MB drive
154 | break;
155 | case floppy1M44K:
156 | dataRate = 0b00000000; // 500 KBS
157 | defs = 0b00000111; // 720K media in 720K drive or 1.44MB media in 1.44MB drive
158 | break;
159 | case floppy2M88K:
160 | dataRate = 0b11000000; // 1 MBS
161 | defs = 0b00000111; // right?
162 | break;
163 | case mediaUnknown:
164 | default:
165 | knownMedia = 0x00; // reset bit 4 (known media)
166 | break;
167 | }
168 | if (m_memory && drive < 2) {
169 | // BIOS data area
170 | m_memory[BIOS_DATAAREA_ADDR + BIOS_DRIVE0MEDIATYPE + drive] = knownMedia | doubleStepping | dataRate | defs;
171 | //printf("%05X = %02X\n", BIOS_DATAAREA_ADDR + BIOS_DRIVE0MEDIATYPE + drive, m_memory[BIOS_DATAAREA_ADDR + BIOS_DRIVE0MEDIATYPE + drive]);
172 |
173 | // INT 1E
174 | uint32_t maddr = getDriveMediaTableAddr(drive);
175 | m_memory[maddr + 0x04] = m_machine->diskSectors(drive);
176 |
177 | // original INT 1E (returned in ES:DI, int 13h, serv 08h)
178 | m_memory[m_origInt1EAddr + 0x04] = m_machine->diskSectors(drive);
179 | m_memory[m_origInt1EAddr + 0x0b] = m_machine->diskCylinders(drive) - 1;
180 | }
181 |
182 | } else if (media == HDD) {
183 |
184 | // HDD
185 |
186 | // fill tables pointed by INT 41h or 46h
187 | uint32_t mtableAddr = getDriveMediaTableAddr(drive);
188 | *(uint16_t*)(m_memory + mtableAddr + 0x00) = m_machine->diskCylinders(drive);
189 | *(uint8_t*)(m_memory + mtableAddr + 0x02) = m_machine->diskHeads(drive);
190 | *(uint8_t*)(m_memory + mtableAddr + 0x0e) = m_machine->diskSectors(drive);
191 |
192 | }
193 | }
194 |
195 |
196 | // AH = select the helper function
197 | void BIOS::helpersEntry()
198 | {
199 | switch (i8086::AH()) {
200 |
201 | // AH = 0x00, perform some INT 9 tasks (keyboard interrupt handler)
202 | case 0x00:
203 | getKeyFromKeyboard();
204 | break;
205 |
206 | // AH = 0x01, get or extract key from keyboard buffer
207 | case 0x01:
208 | getKeyFromBuffer();
209 | break;
210 |
211 | // AH = 0x02, get shift flags or extended shift flags
212 | case 0x02:
213 | getKeyboardFlags();
214 | break;
215 |
216 | // AH = 0x03, set keyboard typematic rate and delay
217 | case 0x03:
218 | setKeyboardTypematicAndDelay();
219 | break;
220 |
221 | // AH = 0x05, store keyboard key data
222 | case 0x05:
223 | storeKeyboardKeyData();
224 | break;
225 |
226 | // AH = 0x06, pointing device interface
227 | case 0x06:
228 | pointingDeviceInterface();
229 | break;
230 |
231 | // AH = 0x07, synchronize system ticks with RTC
232 | case 0x07:
233 | syncTicksWithRTC();
234 | break;
235 |
236 | // AH = 0x08, set media drive parameters tables address
237 | // ES:BX = int 1Eh address (in bios.asm) for floppy drivers
238 | case 0x08:
239 | m_origInt1EAddr = i8086::ES() * 16 + i8086::BX();
240 | // set initial media
241 | for (int i = 0; i < DISKCOUNT; ++i)
242 | setDriveMediaType(i, mediaUnknown);
243 | break;
244 |
245 | default:
246 | break;
247 |
248 | };
249 | }
250 |
251 |
252 |
253 | // convert keypad scancode to number, or -1 if not applicable
254 | int convKeypadScancodeToNum(uint8_t scancode)
255 | {
256 | // LUT to convert scancode 0x47 to 0x52
257 | static const int8_t CONV[12] = { 7, 8, 9, -1, 4, 5, 6, -1, 1, 2, 3, 0 };
258 | return scancode >= 0x47 && scancode <= 0x52 ? CONV[scancode - 0x47] : -1;
259 | }
260 |
261 |
262 | // - update keyboard shift flags
263 | // - adapt fabgl scancode to PC scancodes
264 | // ret: true if the key need to be inserted into the keyboard buffer
265 | bool BIOS::processScancode(int scancode, uint16_t * syscode)
266 | {
267 | struct ScanCode2SysCode {
268 | uint8_t scancode;
269 | uint16_t syscode[4]; // 0 = normal, 1 = shifted, 2 = control, 3 = alt
270 | };
271 | static const ScanCode2SysCode SCODE2SYSCODE[] = {
272 | { 0x29, 0x2960, 0x297e, 0xffff, 0x2900 }, { 0x02, 0x0231, 0x0221, 0xffff, 0x7800 }, { 0x03, 0x0332, 0x0340, 0x0300, 0x7900 },
273 | { 0x04, 0x0433, 0x0423, 0xffff, 0x7a00 }, { 0x05, 0x0534, 0x0524, 0xffff, 0x7b00 }, { 0x06, 0x0635, 0x0625, 0xffff, 0x7c00 },
274 | { 0x07, 0x0736, 0x075e, 0x071e, 0x7d00 }, { 0x08, 0x0837, 0x0826, 0xffff, 0x7e00 }, { 0x09, 0x0938, 0x092a, 0xffff, 0x7f00 },
275 | { 0x0a, 0x0a39, 0x0a28, 0xffff, 0x8000 }, { 0x0b, 0x0b30, 0x0b29, 0xffff, 0x8100 }, { 0x0c, 0x0c2d, 0x0c5f, 0x0c1f, 0x8200 },
276 | { 0x0d, 0x0d3d, 0x0d2b, 0xffff, 0x8300 }, { 0x0e, 0x0e08, 0x0e08, 0x0e7f, 0x0e00 }, { 0x0f, 0x0f09, 0x0f00, 0x9400, 0xa500 },
277 | { 0x10, 0x1071, 0x1051, 0x1011, 0x1000 }, { 0x11, 0x1177, 0x1157, 0x1117, 0x1100 }, { 0x12, 0x1265, 0x1245, 0x1205, 0x1200 },
278 | { 0x13, 0x1372, 0x1352, 0x1312, 0x1300 }, { 0x14, 0x1474, 0x1454, 0x1414, 0x1400 }, { 0x15, 0x1579, 0x1559, 0x1519, 0x1500 },
279 | { 0x16, 0x1675, 0x1655, 0x1615, 0x1600 }, { 0x17, 0x1769, 0x1749, 0x1709, 0x1700 }, { 0x18, 0x186f, 0x184f, 0x180f, 0x1800 },
280 | { 0x19, 0x1970, 0x1950, 0x1910, 0x1900 }, { 0x1a, 0x1a5b, 0x1a7b, 0x1a1b, 0x1a00 }, { 0x1b, 0x1b5d, 0x1b7d, 0x1b1d, 0x1b00 },
281 | { 0x2b, 0x2b5c, 0x2b7c, 0x2b1c, 0x2b00 }, { 0x1e, 0x1e61, 0x1e41, 0x1e01, 0x1e00 }, { 0x1f, 0x1f73, 0x1f53, 0x1f13, 0x1f00 },
282 | { 0x20, 0x2064, 0x2044, 0x2004, 0x2000 }, { 0x21, 0x2166, 0x2146, 0x2106, 0x2100 }, { 0x22, 0x2267, 0x2247, 0x2207, 0x2200 },
283 | { 0x23, 0x2368, 0x2348, 0x2308, 0x2300 }, { 0x24, 0x246a, 0x244a, 0x240a, 0x2400 }, { 0x25, 0x256b, 0x254b, 0x250b, 0x2500 },
284 | { 0x26, 0x266c, 0x264c, 0x260c, 0x2600 }, { 0x27, 0x273b, 0x273a, 0xffff, 0x2700 }, { 0x28, 0x2827, 0x2822, 0xffff, 0x2800 },
285 | { 0x2b, 0x2b5c, 0x2b7c, 0x2b1c, 0xffff }, { 0x1c, 0x1c0d, 0x1c0d, 0x1c0a, 0x1c00 }, { 0x56, 0x565c, 0x567c, 0xffff, 0xffff },
286 | { 0x2c, 0x2c7a, 0x2c5a, 0x2c1a, 0x2c00 }, { 0x2d, 0x2d78, 0x2d58, 0x2d18, 0x2d00 }, { 0x2e, 0x2e63, 0x2e43, 0x2e03, 0x2e00 },
287 | { 0x2f, 0x2f76, 0x2f56, 0x2f16, 0x2f00 }, { 0x30, 0x3062, 0x3042, 0x3002, 0x3000 }, { 0x31, 0x316e, 0x314e, 0x310e, 0x3100 },
288 | { 0x32, 0x326d, 0x324d, 0x320d, 0x3200 }, { 0x33, 0x332c, 0x333c, 0xffff, 0x3300 }, { 0x34, 0x342e, 0x343e, 0xffff, 0x3400 },
289 | { 0x35, 0x352f, 0x353f, 0xffff, 0x3500 }, { 0x39, 0x3920, 0x3920, 0x3920, 0x3920 }, { 0x47, 0x4700, 0x4737, 0x7700, 0xffff },
290 | { 0x4b, 0x4b00, 0x4b34, 0x7300, 0xffff }, { 0x4f, 0x4f00, 0x4f31, 0x7500, 0xffff }, { 0x48, 0x4800, 0x4838, 0x8d00, 0xffff },
291 | { 0x4c, 0x4c00, 0x4c35, 0x8f00, 0xffff }, { 0x50, 0x5000, 0x5032, 0x9100, 0xffff }, { 0x52, 0x5200, 0x5230, 0x9200, 0xffff },
292 | { 0x37, 0x372a, 0x372a, 0x9600, 0x3700 }, { 0x49, 0x4900, 0x4939, 0x8400, 0xffff }, { 0x4d, 0x4d00, 0x4d36, 0x7400, 0xffff },
293 | { 0x51, 0x5100, 0x5133, 0x7600, 0xffff }, { 0x53, 0x5300, 0x532e, 0x9300, 0xffff }, { 0x4a, 0x4a2d, 0x4a2d, 0x8e00, 0x4a00 },
294 | { 0x4e, 0x4e2b, 0x4e2b, 0x9000, 0x4e00 }, { 0x01, 0x011b, 0x011b, 0x011b, 0x0100 }, { 0x3b, 0x3b00, 0x5400, 0x5e00, 0x6800 },
295 | { 0x3c, 0x3c00, 0x5500, 0x5f00, 0x6900 }, { 0x3d, 0x3d00, 0x5600, 0x6000, 0x6a00 }, { 0x3e, 0x3e00, 0x5700, 0x6100, 0x6b00 },
296 | { 0x3f, 0x3f00, 0x5800, 0x6200, 0x6c00 }, { 0x40, 0x4000, 0x5900, 0x6300, 0x6d00 }, { 0x41, 0x4100, 0x5a00, 0x6400, 0x6e00 },
297 | { 0x42, 0x4200, 0x5b00, 0x6500, 0x6f00 }, { 0x43, 0x4300, 0x5c00, 0x6600, 0x7000 }, { 0x44, 0x4400, 0x5d00, 0x6700, 0x7100 },
298 | { 0x57, 0x8500, 0x8700, 0x8900, 0x8b00 }, { 0x58, 0x8600, 0x8800, 0x8a00, 0x8c00 },
299 | { 0x00, 0xffff, 0xffff, 0xffff, 0xffff } // ending code
300 | };
301 | static const ScanCode2SysCode ESCODE2SYSCODE[] = {
302 | { 0x52, 0x52e0, 0x52e0, 0x92e0, 0xa200 }, { 0x53, 0x53e0, 0x53e0, 0x93e0, 0xa300 }, { 0x4b, 0x4be0, 0x4be0, 0x73e0, 0x9b00 },
303 | { 0x47, 0x47e0, 0x47e0, 0x77e0, 0x9700 }, { 0x4f, 0x4fe0, 0x4fe0, 0x75e0, 0x9f00 }, { 0x48, 0x48e0, 0x48e0, 0x8de0, 0x9800 },
304 | { 0x50, 0x50e0, 0x50e0, 0x91e0, 0xa000 }, { 0x49, 0x49e0, 0x49e0, 0x84e0, 0x9900 }, { 0x51, 0x51e0, 0x51e0, 0x76e0, 0xa100 },
305 | { 0x4d, 0x4de0, 0x4de0, 0x74e0, 0x9d00 }, { 0x35, 0xe02f, 0xe02f, 0x9500, 0xa400 }, { 0x1c, 0xe00d, 0xe00d, 0xe00a, 0xa600 },
306 | { 0x37, 0xffff, 0xffff, 0x7200, 0xffff }, // CTRL + PRINTSCREEN
307 | { 0x46, 0xffff, 0xffff, 0x0000, 0xffff }, // CTRL + PAUSE (BREAK)
308 | { 0x00, 0xffff, 0xffff, 0xffff, 0xffff } // ending code
309 | };
310 |
311 | //printf(" %02X\n", scancode);
312 |
313 | *syscode = 0xffff;
314 |
315 | // 3 = RALT, 2 = RCTRL, 1 = E0, 0 = E1
316 | uint8_t * mode = m_memory + BIOS_DATAAREA_ADDR + BIOS_KBDMODE;
317 |
318 | // save and reset e0 and e1 flags
319 | bool e0 = *mode & 0x02;
320 | bool e1 = *mode & 0x01;
321 | *mode &= 0xfc;
322 |
323 | // e0?
324 | if (scancode == 0xe0) {
325 | *mode |= 0x02;
326 | return false;
327 | }
328 |
329 | // e1?
330 | if (scancode == 0xe1) {
331 | *mode |= 0x01;
332 | return false;
333 | }
334 |
335 | bool down = !(scancode & 0x80); // down if bit 7 = 0
336 | scancode &= 0x7f;
337 |
338 | //printf(" e0 = %d, down = %d\n", e0, down);
339 |
340 | // 7 = INS ON, 6 = CAPS ON, 5 = NUMLCK ON, 4 = SCRLCK ON, 3 = ALT, 2 = CTRL, 1 = LSHIFT, 0 = RSHIFT
341 | uint8_t * flags1 = m_memory + BIOS_DATAAREA_ADDR + BIOS_KBDSHIFTFLAGS1;
342 |
343 | // 7 = INS, 6 = CAPS, 5 = NUMLCK, 4 = SCRLCK, 3 = CTRL+NUMLCK ON (PAUSE), 2 = SYSREQ, 1 = LALT, 0 = LCTRL
344 | uint8_t * flags2 = m_memory + BIOS_DATAAREA_ADDR + BIOS_KBDSHIFTFLAGS2;
345 |
346 | // 2 = CAPS LED, 1 = NUMLCK LED, 0 = SCRLCK LED
347 | uint8_t * LEDs = m_memory + BIOS_DATAAREA_ADDR + BIOS_KBDLEDS;
348 |
349 | uint8_t * altKeypadEntry = m_memory + BIOS_DATAAREA_ADDR + BIOS_KBDALTKEYPADENTRY;
350 |
351 | if (e0) {
352 | // extended code (0xe0 ...)
353 | switch (scancode) {
354 | // RCTRL
355 | case 0x1d:
356 | *flags1 = (*flags1 & ~0x04) | (0x04 * down); // bit 2 on flags1 (1 = down)
357 | *mode = (*mode & ~0x04) | (0x04 * down); // bit 2 on mode (1 = down)
358 | break;
359 | // RALT
360 | case 0x38:
361 | *flags1 = (*flags1 & ~0x08) | (0x08 * down); // bit 3 on flags1 (1 = down)
362 | *mode = (*mode & ~0x08) | (0x08 * down); // bit 3 on mode (1 = down)
363 | break;
364 | // INSERT
365 | case 0x52:
366 | *flags1 ^= 0x80 * !down; // bit 7 on flags1 (toggle when up)
367 | *flags2 = (*flags2 & ~0x80) | (0x80 * down); // bit 7 on flags2 (1 = down)
368 | break;
369 | // PRINTSCREEN or SYSREQ
370 | case 0x37:
371 | // not shifts, PRINTSCREEN
372 | if (down && (*flags1 & 0x0f) == 0)
373 | m_memory[BIOS_DATAAREA_ADDR + BIOS_PRINTSCREENFLAG] = 1;
374 | // ALT + PRINTSCREEN = SYSREQ
375 | else if (*flags1 & 0x08)
376 | *flags2 |= 0x04;
377 | break;
378 | // CTRL + BREAK (CTRL + PAUSE)
379 | case 0x46:
380 | emptyKbdBuffer();
381 | m_memory[BIOS_DATAAREA_ADDR + BIOS_CTRLBREAKFLAG] = 0x80;
382 | break;
383 | // bypass (e0 2a / e0 aa)
384 | case 0x2a:
385 | return false;
386 | }
387 | } else if (e1 || m_kbdScancodeComp > 0) {
388 | // extended code (0xe1 ...)
389 | if ((m_kbdScancodeComp == 0 && scancode == 0x1d) || (m_kbdScancodeComp == 1 && scancode == 0x45) || (m_kbdScancodeComp == 2 && scancode == 0x1d)) {
390 | ++m_kbdScancodeComp;
391 | return false;
392 | } else if (m_kbdScancodeComp == 3 && scancode == 0x45) {
393 | // PAUSE key completed (e1 1d 45 e1 9d c5)
394 | *flags2 |= 0x08; // bit 3 on flags2 (always set)
395 | m_kbdScancodeComp = 0;
396 | return false;
397 | }
398 | m_kbdScancodeComp = 0;
399 | } else {
400 | // normal code
401 | m_kbdScancodeComp = 0;
402 | switch (scancode) {
403 | // LALT
404 | case 0x38:
405 | *flags1 = (*flags1 & ~0x08) | (0x08 * down); // bit 3 on flags1 (1 = down)
406 | *flags2 = (*flags2 & ~0x02) | (0x02 * down); // bit 1 on flags2 (1 = down)
407 | break;
408 | // LSHIFT
409 | case 0x2a:
410 | *flags1 = (*flags1 & ~0x02) | (0x02 * down); // bit 1 on flags1 (1 = down)
411 | break;
412 | // RSHIFT
413 | case 0x36:
414 | *flags1 = (*flags1 & ~0x01) | (0x01 * down); // bit 0 on flags1 (1 = down)
415 | break;
416 | // LCTRL
417 | case 0x1d:
418 | *flags1 = (*flags1 & ~0x04) | (0x04 * down); // bit 2 on flags1 (1 = down)
419 | *flags2 = (*flags2 & ~0x01) | (0x01 * down); // bit 0 on flags2 (1 = down)
420 | break;
421 | // SCROLLLOCK
422 | case 0x46:
423 | *flags1 ^= 0x10 * !down; // bit 4 on flags1 (toggle when up)
424 | *flags2 = (*flags2 & ~0x10) | (0x10 * down); // bit 4 on flags2 (1 = down)
425 | *LEDs ^= 0x01 * !down; // bit 0 on LEDs (toggle when up)
426 | break;
427 | // NUMLOCK
428 | case 0x45:
429 | *flags1 ^= 0x20 * !down; // bit 5 on flags1 (toggle when up)
430 | *flags2 = (*flags2 & ~0x20) | (0x20 * down); // bit 5 on flags2 (1 = down)
431 | *LEDs ^= 0x02 * !down; // bit 1 on LEDs (toggle when up)
432 | break;
433 | // CAPSLOCK
434 | case 0x3a:
435 | *flags1 ^= 0x40 * !down; // bit 6 on flags1 (toggle when up)
436 | *flags2 = (*flags2 & ~0x40) | (0x40 * down); // bit 6 on flags2 (1 = down)
437 | *LEDs ^= 0x04 * !down; // bit 2 on LEDs (toggle when up)
438 | break;
439 | // KEYPAD INS (KEYPAD 0)
440 | case 0x52:
441 | // NUMLOCK = off ?, interpret keypad0 as INSERT toggle
442 | if ((*flags1 & 0x20) == 0)
443 | *flags1 ^= 0x80 * !down; // bit 7 on flags1 (toggle when up)
444 | break;
445 | }
446 | }
447 |
448 | bool lalt = *flags1 & 0x08;
449 |
450 | // manage LALT + KEYPAD NUM
451 | if (lalt && scancode != 0x38 && !e0) {
452 | // ALT was down, is this a keypad number?
453 | int num = convKeypadScancodeToNum(scancode);
454 | if (num >= 0) {
455 | // yes this is a keypad num, if down update altKeypadEntry
456 | if (down)
457 | *altKeypadEntry = (*altKeypadEntry * 10 + num) & 0xff;
458 | return false;
459 | } else {
460 | // no, back to normal case
461 | *altKeypadEntry = 0;
462 | }
463 | } else if (*altKeypadEntry > 0 && scancode == 0x38 && !down) {
464 | // ALT is up and altKeypadEntry contains a valid value, add it
465 | *syscode = *altKeypadEntry; // high byte will be 0x00, low byte is the ascii value just typed
466 | *altKeypadEntry = 0;
467 | return true;
468 | }
469 |
470 | if (down) {
471 | bool shift = *flags1 & 0x03;
472 | bool capslock = *flags1 & 0x40;
473 | bool numlock = *flags1 & 0x20;
474 | bool ctrl = *flags1 & 0x04;
475 |
476 | // CAPSLOCK enabled and letter
477 | if (capslock && ((scancode >= 0x10 && scancode <= 0x19) || (scancode >= 0x1e && scancode <= 0x26) || (scancode >= 0x2c && scancode <= 0x32)))
478 | shift = !shift;
479 |
480 | // NUMLOCK and keypad
481 | if (numlock && (scancode >= 0x47 && scancode <= 0x53))
482 | shift = !shift;
483 |
484 | // convert scancode to system code
485 | for (auto CONV = (e0 ? ESCODE2SYSCODE : SCODE2SYSCODE); CONV->scancode; ++CONV)
486 | if (CONV->scancode == scancode) {
487 | *syscode = CONV->syscode[shift ? 1 : ctrl ? 2 : lalt ? 3 : 0];
488 | return *syscode != 0xffff;
489 | }
490 | }
491 |
492 | return false;
493 | }
494 |
495 |
496 | // ret false on buffer full
497 | bool BIOS::storeKeyInKbdBuffer(uint16_t syscode)
498 | {
499 | // check space in BIOS keyboard buffer
500 | auto head = (uint16_t*)(m_memory + BIOS_DATAAREA_ADDR + BIOS_KBDBUFHEAD);
501 | auto tail = (uint16_t*)(m_memory + BIOS_DATAAREA_ADDR + BIOS_KBDBUFTAIL);
502 | if (*head - 2 != *tail && (*head != BIOS_KBDBUF || *tail != BIOS_KBDBUF + 30)) {
503 | // insert key into the keyboard buffer
504 | *(uint16_t*)(m_memory + BIOS_DATAAREA_ADDR + *tail) = syscode;
505 | *tail = (*tail == BIOS_KBDBUF + 30 ? BIOS_KBDBUF : *tail + 2);
506 | return true; // success
507 | }
508 | // buffer full
509 | //printf("kbd buffer full\n");
510 | return false;
511 | }
512 |
513 |
514 | // perform some INT 9 tasks (keyboard interrupt handler)
515 | // intput AL:
516 | // scancode as read from port 0x60
517 | // output AH:
518 | // 0 : normal key
519 | // 2 : CTRL + ALT + DEL
520 | // 3 : PRINTSCREEN
521 | // 4 : CTRL-BREAK (CTRL + PAUSE)
522 | // 5 : SYSREQ (ALT + PRINTSCREEN), down (AL = 0), up (AL = 1)
523 | void BIOS::getKeyFromKeyboard()
524 | {
525 | uint8_t * flags1 = m_memory + BIOS_DATAAREA_ADDR + BIOS_KBDSHIFTFLAGS1;
526 | uint8_t * flags2 = m_memory + BIOS_DATAAREA_ADDR + BIOS_KBDSHIFTFLAGS2;
527 | // saves current pause state
528 | bool onPause = *flags2 & 0x08;
529 | // update keyboard decoding state
530 | uint16_t syscode; // low byte = ASCII value, high byte = scancode
531 | bool toAdd = processScancode(i8086::AL(), &syscode);
532 | if (toAdd) {
533 | if (onPause) {
534 | // just disable pause state and discard key
535 | *flags2 &= ~0x08;
536 | } else {
537 | // we need to add this key to the keyboard buffer
538 | //printf("kbd store %04X\n", syscode);
539 | if (!storeKeyInKbdBuffer(syscode)) {
540 | // buffer full
541 | //printf("kbd buffer full\n");
542 | }
543 | }
544 | }
545 | // check for special syskeys
546 | if ((*flags1 & 0x04) && (*flags1 & 0x08) && (syscode == 0x53e0 || syscode == 0x93e0 || syscode == 0xa300)) {
547 | i8086::setAH(2);
548 | } else if (m_memory[BIOS_DATAAREA_ADDR + BIOS_PRINTSCREENFLAG] == 1) {
549 | i8086::setAH(3);
550 | } else if (syscode == 0x0000) {
551 | i8086::setAH(4);
552 | } else if (*flags2 & 0x04) {
553 | i8086::setAH(5);
554 | i8086::setAL((bool)i8086::AL());
555 | } else
556 | i8086::setAH(0);
557 | }
558 |
559 |
560 | // get or extract key from keyboard buffer
561 | // updates keyboard LEDs
562 | // input AL:
563 | // bit 0 : 0 = check only, 1 = extract
564 | // bit 1 : 0 = do not filter, 1 = filter extended keys
565 | // output:
566 | // AX : ASCII (AL) and scancode (AH)
567 | // ZF : 0 = key present, 1 = key not present
568 | void BIOS::getKeyFromBuffer()
569 | {
570 | // this LUT transform extended keyboard syskeys to XT syscodes
571 | struct Ext2XT {
572 | uint16_t esyscode;
573 | uint16_t xsyscode; // 0xffff = don't return
574 | };
575 | static const Ext2XT EXT2XT[] = {
576 | { 0x2900, 0xffff }, { 0x0e00, 0xffff }, { 0x9400, 0xffff }, { 0xa500, 0xffff }, { 0x1a00, 0xffff }, { 0x1b00, 0xffff }, { 0x2b00, 0xffff },
577 | { 0x2700, 0xffff }, { 0x2800, 0xffff }, { 0x1c00, 0xffff }, { 0x3300, 0xffff }, { 0x3400, 0xffff }, { 0x3500, 0xffff }, { 0x52e0, 0x5200 },
578 | { 0x92e0, 0xffff }, { 0xa200, 0xffff }, { 0x53e0, 0x5300 }, { 0x93e0, 0xffff }, { 0xa300, 0xffff }, { 0x4be0, 0x4b00 }, { 0x73e0, 0x7300 },
579 | { 0x9b00, 0xffff }, { 0x47e0, 0x4700 }, { 0x77e0, 0x7700 }, { 0x9700, 0xffff }, { 0x4fe0, 0x4f00 }, { 0x75e0, 0x7500 }, { 0x9f00, 0xffff },
580 | { 0x48e0, 0x4800 }, { 0x8de0, 0xffff }, { 0x9800, 0xffff }, { 0x50e0, 0x5000 }, { 0x91e0, 0xffff }, { 0xa000, 0xffff }, { 0x49e0, 0x4900 },
581 | { 0x84e0, 0x8400 }, { 0x9900, 0xffff }, { 0x51e0, 0x5100 }, { 0x76e0, 0x7600 }, { 0xa100, 0xffff }, { 0x4de0, 0x4d00 }, { 0x74e0, 0x7400 },
582 | { 0x9d00, 0xffff }, { 0xe02f, 0x352f }, { 0x9500, 0xffff }, { 0xa400, 0xffff }, { 0x8d00, 0xffff }, { 0x8f00, 0xffff }, { 0x9100, 0xffff },
583 | { 0x9200, 0xffff }, { 0x9600, 0xffff }, { 0x3700, 0xffff }, { 0x9300, 0xffff }, { 0x8e00, 0xffff }, { 0x4a00, 0xffff }, { 0x9000, 0xffff },
584 | { 0x4e00, 0xffff }, { 0xe00d, 0x1c0d }, { 0xe00a, 0x1c0a }, { 0xa600, 0xffff }, { 0x0100, 0xffff }, { 0x8500, 0xffff }, { 0x8700, 0xffff },
585 | { 0x8900, 0xffff }, { 0x8b00, 0xffff }, { 0x8600, 0xffff }, { 0x8800, 0xffff }, { 0x8a00, 0xffff }, { 0x8c00, 0xffff },
586 | };
587 |
588 | // return value is not valid (ZF = 1)
589 | i8086::setFlagZF(1);
590 |
591 | auto head = (uint16_t*)(m_memory + BIOS_DATAAREA_ADDR + BIOS_KBDBUFHEAD);
592 | auto tail = (uint16_t*)(m_memory + BIOS_DATAAREA_ADDR + BIOS_KBDBUFTAIL);
593 |
594 | if (*head != *tail) {
595 |
596 | // get key from buffer head
597 | uint16_t k = * (uint16_t*) (m_memory + BIOS_DATAAREA_ADDR + *head);
598 |
599 | bool filtered = false;
600 |
601 | // filter extended keys?
602 | if (i8086::AL() & 0x02) {
603 | for (int i = 0; i < sizeof(EXT2XT) / sizeof(Ext2XT); ++i) {
604 | if (EXT2XT[i].esyscode == k) {
605 | if (EXT2XT[i].xsyscode == 0xffff)
606 | filtered = true; // don't return
607 | else
608 | k = EXT2XT[i].xsyscode; // replace
609 | break;
610 | }
611 | }
612 | }
613 |
614 | // remove from buffer?
615 | if (filtered || (i8086::AL() & 0x01))
616 | *head = (*head == BIOS_KBDBUF + 30 ? BIOS_KBDBUF : *head + 2);
617 |
618 | if (!filtered) {
619 | // return value is valid (ZF = 0)
620 | i8086::setFlagZF(0);
621 | i8086::setAX(k);
622 | }
623 |
624 | }
625 |
626 | // update LEDs
627 | bool numLockLED, capsLockLED, scrollLockLED;
628 | m_keyboard->getLEDs(&numLockLED, &capsLockLED, &scrollLockLED);
629 | uint8_t LEDs = m_memory[BIOS_DATAAREA_ADDR + BIOS_KBDLEDS];
630 | if (numLockLED != (bool)(LEDs & 0x02) || capsLockLED != (bool)(LEDs & 0x04) || scrollLockLED != (bool)(LEDs & 0x01))
631 | m_keyboard->setLEDs((bool)(LEDs & 0x02), (bool)(LEDs & 0x04), (bool)(LEDs & 0x01));
632 | }
633 |
634 |
635 | void BIOS::emptyKbdBuffer()
636 | {
637 | auto head = (uint16_t*)(m_memory + BIOS_DATAAREA_ADDR + BIOS_KBDBUFHEAD);
638 | auto tail = (uint16_t*)(m_memory + BIOS_DATAAREA_ADDR + BIOS_KBDBUFTAIL);
639 | *tail = *head;
640 | }
641 |
642 |
643 | // get shift flags or extended shift flags
644 | // input AL:
645 | // bit 0 : 0 = normal flags in AL, 1 = normal flags in AL and extended flags in AH
646 | // output AL or AX
647 | void BIOS::getKeyboardFlags()
648 | {
649 | uint8_t * flags1 = m_memory + BIOS_DATAAREA_ADDR + BIOS_KBDSHIFTFLAGS1;
650 | uint8_t * flags2 = m_memory + BIOS_DATAAREA_ADDR + BIOS_KBDSHIFTFLAGS2;
651 | uint8_t * mode = m_memory + BIOS_DATAAREA_ADDR + BIOS_KBDMODE;
652 | if (i8086::AL() & 1)
653 | i8086::setAH( (*flags2 & 0xf3) | (*mode & 0x0c) );
654 | i8086::setAL(*flags1);
655 | }
656 |
657 |
658 | // inputs:
659 | // AL : service (0x05 = set typematic rate and delay)
660 | // BH : delay value (as expected by BIOS, 0 = 250ms, 1 = 500ms, etc...)
661 | // BL : typematic rate (as expected by BIOS, 0 = 30, 1 = 26.7, etc...)
662 | // notes:
663 | // this method doesn't use 0x60 and 0x64 ports, but directly interfaces to the Keyboard object
664 | void BIOS::setKeyboardTypematicAndDelay()
665 | {
666 | if (i8086::AL() == 0x05) {
667 | // send command "set typematic rate and delay" (0xF3) to the keyboard and wait for ACK (0xFA)
668 | if (!m_keyboard->sendCommand(0xF3, 0xFA))
669 | return;
670 | // send parameters
671 | m_keyboard->sendCommand(i8086::BL() | (i8086::BH() << 5), 0xFA);
672 | }
673 | }
674 |
675 |
676 | // inputs:
677 | // CL : ASCII character
678 | // CH : scan code
679 | // outputs:
680 | // AL : 0 = no error, 1 = keyboard buffer full
681 | // CF : 0 = no error, 1 = keyboard buffer full
682 | void BIOS::storeKeyboardKeyData()
683 | {
684 | bool r = storeKeyInKbdBuffer(i8086::CX());
685 | i8086::setAL(!r);
686 | i8086::setFlagCF(!r);
687 | }
688 |
689 |
690 | // Implements all services of "INT 15 Function C2h"
691 | // inputs:
692 | // AL : subfunction
693 | // .. : depends by the subfunction
694 | // outputs:
695 | // AH : 0 = success, >0 = error (see "INT 15h Function C2h - Pointing Device Interface")
696 | // CF : 0 = successful, 1 = unsuccessful
697 | // .. : depends by the subfunction
698 | void BIOS::pointingDeviceInterface()
699 | {
700 | if (m_mouse->isMouseAvailable()) {
701 |
702 | i8086::setAH(0x00);
703 | i8086::setFlagCF(0);
704 |
705 | switch (i8086::AL()) {
706 |
707 | // Enable/disable pointing device
708 | // inputs:
709 | // AL : 0x00
710 | // BH : 0 = disable, 1 = enable
711 | case 0x00:
712 | m_i8042->enableMouse(i8086::BH());
713 | break;
714 |
715 | // Reset pointing device
716 | // inputs:
717 | // AL : 0x01
718 | // outputs:
719 | // BH : Device ID
720 | case 0x01:
721 | m_i8042->enableMouse(false); // mouse disabled
722 | m_mouse->setSampleRate(100); // 100 reports/second
723 | m_mouse->setResolution(2); // 4 counts/millimeter
724 | m_mouse->setScaling(1); // 1:1 scaling
725 | i8086::setBH(m_mouse->deviceID() & 0xff);
726 | break;
727 |
728 | // Set sample rate
729 | // inputs:
730 | // AL : 0x02
731 | // BH : Sample rate
732 | case 0x02:
733 | m_mouse->setSampleRate(i8086::BH());
734 | break;
735 |
736 | // Set resolution
737 | // inputs:
738 | // AL : 0x03
739 | // BH : Resolution value
740 | case 0x03:
741 | m_mouse->setResolution(i8086::BH());
742 | break;
743 |
744 | // Read device type
745 | // inputs:
746 | // AL : 0x04
747 | case 0x04:
748 | i8086::setBH(m_mouse->deviceID() & 0xff);
749 | break;
750 |
751 | // Initialize pointing device interface
752 | // inputs:
753 | // AL : 0x05
754 | // BH : Data package size (1-8, in bytes)
755 | // note: this value is acqually ignored because we get actual packet size from Mouse object
756 | case 0x05:
757 | {
758 | m_i8042->enableMouse(false); // mouse disabled
759 | m_mouse->setSampleRate(100); // 100 reports/second
760 | m_mouse->setResolution(2); // 4 counts/millimeter
761 | m_mouse->setScaling(1); // 1:1 scaling
762 | uint8_t * EBDA = m_memory + EBDA_ADDR;
763 | EBDA[EBDA_DRIVER_OFFSET] = 0x0000;
764 | EBDA[EBDA_DRIVER_SEG] = 0x0000;
765 | EBDA[EBDA_FLAGS1] = 0x00;
766 | EBDA[EBDA_FLAGS2] = m_mouse->getPacketSize(); // instead of i8086::BH()!!
767 | break;
768 | }
769 |
770 | // Set scaling or get status
771 | // inputs:
772 | // AL : 0x06
773 | // BH : subfunction
774 | case 0x06:
775 | switch (i8086::BH()) {
776 | // Set scaling factor to 1:1
777 | // inputs:
778 | // BH : 0x01
779 | case 0x01:
780 | m_mouse->setScaling(1);
781 | break;
782 | // Set scaling factor to 2:1
783 | // inputs:
784 | // BH : 0x02
785 | case 0x02:
786 | m_mouse->setScaling(2);
787 | break;
788 | default:
789 | // not implements
790 | printf("Pointing device function 06:%02X not implemented\n", i8086::BH());
791 | i8086::setAH(0x86);
792 | i8086::setFlagCF(1);
793 | break;
794 | }
795 | break;
796 |
797 | // Set pointing device handler address
798 | // inputs:
799 | // AL = 0x07
800 | // ES:BX : Pointer to application-program's device driver
801 | case 0x07:
802 | {
803 | uint8_t * EBDA = m_memory + EBDA_ADDR;
804 | *(uint16_t*)(EBDA + EBDA_DRIVER_OFFSET) = i8086::BX();
805 | *(uint16_t*)(EBDA + EBDA_DRIVER_SEG) = i8086::ES();
806 | EBDA[EBDA_FLAGS2] |= 0x80; // set handler installed flag
807 | break;
808 | }
809 |
810 | default:
811 | // not implements
812 | printf("Pointing device function %02X not implemented\n", i8086::AL());
813 | i8086::setAH(0x86);
814 | i8086::setFlagCF(1);
815 | break;
816 | }
817 |
818 | } else {
819 | // mouse not available
820 | i8086::setAH(0x03); // 0x03 = interface error
821 | i8086::setFlagCF(1);
822 | }
823 | }
824 |
825 |
826 | // convert packed BCD to decimal
827 | static uint8_t BCDtoByte(uint8_t v)
828 | {
829 | return (v & 0x0F) + (v >> 4) * 10;
830 | }
831 |
832 |
833 | // synchronize system ticks with RTC
834 | void BIOS::syncTicksWithRTC()
835 | {
836 | m_MC146818->updateTime();
837 | int ss = BCDtoByte(m_MC146818->reg(0x00));
838 | int mm = BCDtoByte(m_MC146818->reg(0x02));
839 | int hh = BCDtoByte(m_MC146818->reg(0x04));
840 | int totSecs = ss + mm * 60 + hh * 3600 + 1000;
841 | int64_t pitTicks = (int64_t)totSecs * PIT_TICK_FREQ;
842 | *(uint32_t*)(m_memory + BIOS_DATAAREA_ADDR + BIOS_SYSTICKS) = (uint32_t) (pitTicks / 65536);
843 | }
844 |
845 |
846 | // INT 13 services
847 | void BIOS::diskHandlerEntry()
848 | {
849 | if (i8086::DL() < 2)
850 | diskHandler_floppy();
851 | else
852 | diskHandler_HD();
853 | }
854 |
855 |
856 | void BIOS::diskHandler_floppy()
857 | {
858 | int drive = i8086::DL();
859 | int service = i8086::AH();
860 |
861 | //printf("INT 13h, FDD (%d), service %02X\n", drive, service);
862 |
863 | if (m_machine->disk(drive) == nullptr) {
864 | // invalid drive
865 | diskHandler_floppyExit(0x80, true);
866 | return;
867 | }
868 |
869 | switch (service) {
870 |
871 | // Reset Diskette System
872 | case 0x00:
873 | diskHandler_floppyExit(m_mediaType[drive] == mediaUnknown ? 0x06 : 0x00, true);
874 | return;
875 |
876 | // Read Diskette Status
877 | case 0x01:
878 | diskHandler_floppyExit(m_memory[BIOS_DATAAREA_ADDR + BIOS_DISKLASTSTATUS], false);
879 | m_memory[BIOS_DATAAREA_ADDR + BIOS_DISKLASTSTATUS] = 0; // this function resets BIOS_DISKLASTSTATUS
880 | return;
881 |
882 | // Read Diskette Sectors
883 | case 0x02:
884 | // Write Diskette Sectors
885 | case 0x03:
886 | // Verify Diskette Sectors
887 | case 0x04:
888 | {
889 | if (!checkDriveMediaType(drive)) {
890 | diskHandler_floppyExit(6, true);
891 | return;
892 | }
893 | uint32_t pos, dest, count;
894 | if (!diskHandler_calcAbsAddr(drive, &pos, &dest, &count)) {
895 | diskHandler_floppyExit(4, true); // sector not found
896 | return;
897 | }
898 | fseek(m_machine->disk(drive), pos, 0);
899 | size_t sects = i8086::AL();
900 | if (service != 0x04) {
901 | sects = service == 0x02 ?
902 | fread(m_memory + dest, 1, count, m_machine->disk(drive)) :
903 | fwrite(m_memory + dest, 1, count, m_machine->disk(drive));
904 | sects /= 512;
905 | }
906 | i8086::setAL(sects);
907 | diskHandler_floppyExit(sects == 0 ? 4 : 0, true);
908 | return;
909 | }
910 |
911 | // Format Diskette Track
912 | case 0x05:
913 | {
914 | int sectsCountToFormat = i8086::AL();
915 | int track = i8086::CH();
916 | int head = i8086::DH();
917 | uint32_t tableAddr = i8086::ES() * 16 + i8086::BX();
918 |
919 | int SPT = m_machine->diskSectors(drive);
920 | int tracksCount = m_machine->diskCylinders(drive);
921 |
922 | uint8_t fillByte = m_memory[getDriveMediaTableAddr(drive) + 8];
923 |
924 | uint8_t * buf = (uint8_t*) malloc(512);
925 | memset(buf, fillByte, 512);
926 |
927 | for (int i = 0; i < sectsCountToFormat; ++i) {
928 | int ttrack = m_memory[tableAddr++];
929 | int thead = m_memory[tableAddr++];
930 | int tsect = m_memory[tableAddr++];
931 | int tsectSz = 128 << m_memory[tableAddr++];
932 | if (ttrack != track || thead > 1 || tsect > SPT || tsectSz != 512 || track >= tracksCount) {
933 | // error
934 | free(buf);
935 | diskHandler_floppyExit(0x04, true);
936 | return;
937 | }
938 | fseek(m_machine->disk(drive), 512 * ((track * 2 + head) * SPT + (tsect - 1)), 0);
939 | fwrite(buf, 1, 512, m_machine->disk(drive));
940 | //printf(" track=%d head=%d sect=%d pos=%d fill=%02X\n", track, head, tsect, 512 * ((track * 2 + head) * SPT + (tsect - 1)), fillByte);
941 | }
942 | free(buf);
943 | diskHandler_floppyExit(0x00, true);
944 | return;
945 | }
946 |
947 | // Read Drive Parameters
948 | case 0x08:
949 | i8086::setAX(0x0000);
950 | i8086::setBH(0x00);
951 | if (!checkDriveMediaType(drive)) {
952 | i8086::setCX(0x0000);
953 | i8086::setDX(0x0000);
954 | i8086::setES(0x0000);
955 | i8086::setDI(0x0000);
956 | diskHandler_floppyExit(6, true);
957 | return;
958 | }
959 | switch (m_mediaType[drive]) {
960 | case floppy160KB:
961 | case floppy180KB:
962 | case floppy320KB:
963 | // @TODO: check this
964 | i8086::setBL(0x01);
965 | break;
966 | case floppy360KB:
967 | i8086::setBL(0x01);
968 | break;
969 | case floppy720KB:
970 | i8086::setBL(0x03);
971 | break;
972 | case floppy1M2K:
973 | i8086::setBL(0x02);
974 | break;
975 | case floppy1M44K:
976 | i8086::setBL(0x04);
977 | break;
978 | case floppy2M88K:
979 | i8086::setBL(0x05);
980 | break;
981 | case mediaUnknown:
982 | default:
983 | // not possible here
984 | break;
985 | }
986 | i8086::setCH(m_machine->diskCylinders(drive) - 1); // max usable track number
987 | i8086::setCL(m_machine->diskSectors(drive)); // max usable sector number
988 | i8086::setDH(m_machine->diskHeads(drive) - 1); // max usable head number
989 | i8086::setDL((bool)(m_machine->disk(0)) + (bool)(m_machine->disk(1))); // number of diskette installed
990 | // Pointer to Diskette Parameters table for the maximum media type supported on the specified drive
991 | i8086::setES(BIOS_SEG);
992 | i8086::setDI(m_origInt1EAddr - BIOS_SEG * 16);
993 | diskHandler_floppyExit(0, true);
994 | //printf(" INT 13h, FDD, 08h: BL=%d, CH=%d, CL=%d, DL=%d\n", i8086::BL(), i8086::CH(), i8086::CL(), i8086::DL());
995 | return;
996 |
997 | // Read Drive Type
998 | case 0x15:
999 | diskHandler_floppyExit(0, true);
1000 | i8086::setAH(m_machine->disk(drive) ? 0x02 : 0x00);
1001 | return;
1002 |
1003 | // Detect Media Change
1004 | case 0x16:
1005 | diskHandler_floppyExit(m_mediaType[drive] == mediaUnknown ? 0x06 : 0x00, true);
1006 | return;
1007 |
1008 | // Set Diskette Type
1009 | case 0x17:
1010 | switch (i8086::AL()) {
1011 |
1012 | // 320K/360K
1013 | case 0x01:
1014 | diskHandler_floppyExit(m_mediaType[drive] != floppy360KB &&
1015 | m_mediaType[drive] != floppy320KB &&
1016 | m_mediaType[drive] != floppy180KB &&
1017 | m_mediaType[drive] != floppy160KB, true);
1018 | break;
1019 |
1020 | // 360K
1021 | case 0x02:
1022 | diskHandler_floppyExit(m_mediaType[drive] != floppy360KB, true);
1023 | break;
1024 |
1025 | // 1.2MB
1026 | case 0x03:
1027 | diskHandler_floppyExit(m_mediaType[drive] != floppy1M2K, true);
1028 | break;
1029 |
1030 | // 720KB
1031 | case 0x04:
1032 | diskHandler_floppyExit(m_mediaType[drive] != floppy720KB, true);
1033 | break;
1034 |
1035 | // error
1036 | default:
1037 | diskHandler_floppyExit(1, true);
1038 | break;
1039 |
1040 | }
1041 | return;
1042 |
1043 | // Set Media Type for Format
1044 | case 0x18:
1045 | {
1046 | // check if proposed media type matches with current
1047 | int propTracks = i8086::CH();
1048 | int propSPT = i8086::CL();
1049 | int tracks = m_machine->diskCylinders(drive) - 1;
1050 | int SPT = m_machine->diskSectors(drive);
1051 | if (propTracks == tracks && propSPT == SPT) {
1052 | // match ok
1053 | diskHandler_floppyExit(0x00, true);
1054 | i8086::setES(BIOS_SEG);
1055 | i8086::setDI(m_origInt1EAddr - BIOS_SEG * 16);
1056 | } else {
1057 | // not supported
1058 | diskHandler_floppyExit(0x0c, true);
1059 | printf(" INT 13h, FDD, 18h: unsupported media type, t=%d (%d), s=%d (%d)\n", propTracks, tracks, propSPT, SPT);
1060 | }
1061 | return;
1062 | }
1063 |
1064 | default:
1065 | // invalid function
1066 | diskHandler_floppyExit(1, true);
1067 | printf(" INT 13h, FDD, invalid service %02X\n", service);
1068 | return;
1069 |
1070 | }
1071 | }
1072 |
1073 |
1074 | // CH : low 8 bits of track number
1075 | // CL 6...7 : high 2 bits of track number
1076 | // CL 0...5 : sector number
1077 | // DH : head number
1078 | // AL : number of sectors to read
1079 | // ES:BX : destination address
1080 | bool BIOS::diskHandler_calcAbsAddr(int drive, uint32_t * pos, uint32_t * dest, uint32_t * count)
1081 | {
1082 | int sectorsPerTrack = m_machine->diskSectors(drive);
1083 | int heads = m_machine->diskHeads(drive);
1084 | int track = i8086::CH() | (((uint16_t)i8086::CL() & 0xc0) << 2);
1085 | int sector = i8086::CL() & 0x3f;
1086 | int head = i8086::DH();
1087 | if (sector > sectorsPerTrack)
1088 | return false;
1089 | *pos = 512 * ((track * heads + head) * sectorsPerTrack + (sector - 1));
1090 | *dest = i8086::ES() * 16 + i8086::BX();
1091 | *count = i8086::AL() * 512;
1092 | return true;
1093 | }
1094 |
1095 |
1096 | void BIOS::diskHandler_floppyExit(uint8_t err, bool setErrStat)
1097 | {
1098 | i8086::setAH(err);
1099 | i8086::setFlagCF(err ? 1 : 0);
1100 | if (setErrStat)
1101 | m_memory[BIOS_DATAAREA_ADDR + BIOS_DISKLASTSTATUS] = err;
1102 |
1103 | /*
1104 | if (err > 0)
1105 | printf(" err = %d\n", err);
1106 | */
1107 | }
1108 |
1109 |
1110 | void BIOS::diskHandler_HD()
1111 | {
1112 | int drive = (i8086::DL() & 1) + 2; // 2 = HD0, 3 = HD1
1113 | int service = i8086::AH();
1114 |
1115 | //printf("INT 13h, HDD (%d), service %02X [AH=%02X DL=%02X]\n", drive, service, i8086::AH(), i8086::DL());
1116 |
1117 | if (m_machine->disk(drive) == nullptr || i8086::DL() > 0x81) {
1118 | // invalid drive
1119 | diskHandler_HDExit(0x80, true);
1120 | return;
1121 | }
1122 |
1123 | switch (service) {
1124 |
1125 | // Reset Fixed Disk System
1126 | case 0x00:
1127 | diskHandler_HDExit(checkDriveMediaType(drive) ? 0x00 : 0x80, true);
1128 | return;
1129 |
1130 | // Read Disk Status
1131 | case 0x01:
1132 | diskHandler_HDExit(m_memory[BIOS_DATAAREA_ADDR + BIOS_HDLASTSTATUS], false);
1133 | m_memory[BIOS_DATAAREA_ADDR + BIOS_HDLASTSTATUS] = 0; // this function resets BIOS_HDLASTSTATUS
1134 | return;
1135 |
1136 | // Read Fixed Disk Sectors
1137 | case 0x02:
1138 | // Write Fixed Disk Sectors
1139 | case 0x03:
1140 | // Verify Fixed Disk Sectors
1141 | case 0x04:
1142 | {
1143 | if (!checkDriveMediaType(drive)) {
1144 | diskHandler_HDExit(0x80, true);
1145 | return;
1146 | }
1147 | uint32_t pos, dest, count;
1148 | if (!diskHandler_calcAbsAddr(drive, &pos, &dest, &count)) {
1149 | diskHandler_HDExit(4, true); // sector not found
1150 | return;
1151 | }
1152 | fseek(m_machine->disk(drive), pos, 0);
1153 | size_t sects = i8086::AL();
1154 | if (service != 0x04) {
1155 | sects = service == 0x02 ?
1156 | fread(m_memory + dest, 1, count, m_machine->disk(drive)) :
1157 | fwrite(m_memory + dest, 1, count, m_machine->disk(drive));
1158 | sects /= 512;
1159 | }
1160 | i8086::setAL(sects);
1161 | diskHandler_HDExit(sects == 0 ? 4 : 0, true);
1162 | return;
1163 | }
1164 |
1165 | // Format Disk Cylinder
1166 | case 0x05:
1167 | diskHandler_HDExit(0x00, true);
1168 | return;
1169 |
1170 | // Read Drive Parameters
1171 | case 0x08:
1172 | i8086::setAL(0x00);
1173 | if (checkDriveMediaType(drive)) {
1174 | uint16_t maxUsableCylNum = m_machine->diskCylinders(drive) - 1;
1175 | uint8_t maxUsableSecNum = m_machine->diskSectors(drive);
1176 | uint8_t maxUsableHeadNum = m_machine->diskHeads(drive) - 1;
1177 | i8086::setCH(maxUsableCylNum & 0xff); // Maximum usable cylinder number (low 8 bits)
1178 | i8086::setCL(((maxUsableCylNum >> 2) & 0xc0) | // Bits 7-6 = Maximum usable cylinder number (high 2 bits)
1179 | (maxUsableSecNum & 0x3f)); // Bits 5-0 = Maximum usable sector number
1180 | i8086::setDH(maxUsableHeadNum); // Maximum usable head number
1181 | i8086::setDL((bool)(m_machine->disk(2)) + (bool)(m_machine->disk(3))); // Number of drives (@TODO: correct? Docs not clear)
1182 | // Address of Fixed Disk Parameters table
1183 | // *** note: some texts tell ES:DI should return a pointer to parameters table. IBM docs don't. Actually
1184 | // returning ES:DI may crash old MSDOS versions!
1185 | //i8086::setES(BIOS_SEG);
1186 | //i8086::setDI(getDriveMediaTableAddr(drive) - BIOS_SEG * 16);
1187 | /*
1188 | printf("CH = %02X CL = %02X DH = %02X DL = %02X\n", i8086::CH(), i8086::CL(), i8086::DH(), i8086::DL());
1189 | printf("ES = %04X DI = %04X\n", i8086::ES(), i8086::DI());
1190 | printf("ES:DI [00-01] = %d\n", (int) *(uint16_t*)(m_memory + i8086::ES() * 16 + i8086::DI() + 0x00));
1191 | printf("ES:DI [02] = %d\n", (int) *(uint8_t*)(m_memory + i8086::ES() * 16 + i8086::DI() + 0x02));
1192 | printf("ES:DI [0e] = %d\n", (int) *(uint8_t*)(m_memory + i8086::ES() * 16 + i8086::DI() + 0x0e));
1193 | */
1194 | diskHandler_HDExit(0x00, true);
1195 | } else {
1196 | i8086::setCX(0x0000);
1197 | i8086::setDX(0x0000);
1198 | diskHandler_HDExit(0x80, true);
1199 | }
1200 | return;
1201 |
1202 | // Initialize Drive Parameters
1203 | case 0x09:
1204 | // Seek to Cylinder
1205 | case 0x0c:
1206 | // Recalibrate Drive
1207 | case 0x11:
1208 | // Controller Internal Diagnostic
1209 | case 0x14:
1210 | diskHandler_HDExit(checkDriveMediaType(drive) ? 0x00 : 0x80, true);
1211 | return;
1212 |
1213 | // Test for Drive Ready
1214 | case 0x10:
1215 | diskHandler_HDExit(checkDriveMediaType(drive) ? 0x00 : 0xAA, true);
1216 | return;
1217 |
1218 | // Read Disk Type
1219 | case 0x15:
1220 | if (checkDriveMediaType(drive)) {
1221 | diskHandler_HDExit(0x00, true);
1222 | i8086::setAH(0x03);// drive present
1223 | auto sectors = m_machine->diskSize(drive) / 512;
1224 | i8086::setDX(sectors & 0xffff);
1225 | i8086::setCX(sectors >> 16);
1226 | } else {
1227 | i8086::setAX(0x0000);
1228 | i8086::setCX(0x0000);
1229 | i8086::setDX(0x0000);
1230 | diskHandler_HDExit(0x00, true); // yes, it is 0x00!
1231 | }
1232 | return;
1233 |
1234 | default:
1235 | // invalid function
1236 | printf("INT 13h, HDD, invalid service %02X\n", service);
1237 | diskHandler_HDExit(1, true);
1238 | return;
1239 |
1240 | }
1241 | }
1242 |
1243 |
1244 | void BIOS::diskHandler_HDExit(uint8_t err, bool setErrStat)
1245 | {
1246 | i8086::setAH(err);
1247 | i8086::setFlagCF(err ? 1 : 0);
1248 | if (setErrStat)
1249 | m_memory[BIOS_DATAAREA_ADDR + BIOS_HDLASTSTATUS] = err;
1250 | }
1251 |
1252 |
1253 | void BIOS::videoHandlerEntry()
1254 | {
1255 | auto ga = m_machine->graphicsAdapter();
1256 | auto fb = m_machine->frameBuffer();
1257 |
1258 | switch (i8086::AH()) {
1259 |
1260 | // Write Pixel
1261 | case 0x0c:
1262 | switch (ga->emulation()) {
1263 |
1264 | case GraphicsAdapter::Emulation::PC_Graphics_320x200_4Colors:
1265 | {
1266 | constexpr uint32_t rowlen = 320 / 4;
1267 | const uint8_t value = i8086::AL() & 0b11;
1268 | const bool xored = i8086::AL() & 0x80;
1269 | const uint32_t col = i8086::CX();
1270 | const uint32_t row = i8086::DX();
1271 | const uint32_t addr = (row >> 1) * rowlen + (col >> 2) + (row & 1) * 0x2000;
1272 | const uint32_t shift = 6 - (col & 3) * 2;
1273 | if (xored)
1274 | fb[addr] ^= value << shift;
1275 | else
1276 | fb[addr] = (fb[addr] & ~(0b11 << shift)) | (value << shift);
1277 | break;
1278 | }
1279 |
1280 | case GraphicsAdapter::Emulation::PC_Graphics_640x200_2Colors:
1281 | printf("INT 10h, write pixel, unsupported 640x200x2 resolution\n");
1282 | break;
1283 |
1284 | case GraphicsAdapter::Emulation::PC_Graphics_HGC_720x348:
1285 | printf("INT 10h, write pixel, unsupported 720x348x2 resolution\n");
1286 | break;
1287 |
1288 | default:
1289 | printf("INT 10h, write pixel, unsupported resolution\n");
1290 | break;
1291 |
1292 | }
1293 | break;
1294 |
1295 | default:
1296 | printf("unsupported INT 10h, AX = %04X\n", i8086::AX());
1297 | break;
1298 |
1299 | }
1300 | }
1301 |
--------------------------------------------------------------------------------
/bios.h:
--------------------------------------------------------------------------------
1 | /*
2 | Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) -
3 | Copyright (c) 2019-2022 Fabrizio Di Vittorio.
4 | All rights reserved.
5 |
6 |
7 | * Please contact fdivitto2013@gmail.com if you need a commercial license.
8 |
9 |
10 | * This library and related software is available under GPL v3.
11 |
12 | FabGL is free software: you can redistribute it and/or modify
13 | it under the terms of the GNU General Public License as published by
14 | the Free Software Foundation, either version 3 of the License, or
15 | (at your option) any later version.
16 |
17 | FabGL is distributed in the hope that it will be useful,
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 | GNU General Public License for more details.
21 |
22 | You should have received a copy of the GNU General Public License
23 | along with FabGL. If not, see .
24 | */
25 |
26 |
27 | #pragma once
28 |
29 |
30 | #include "fabgl.h"
31 | #include "emudevs/i8042.h"
32 | #include "emudevs/MC146818.h"
33 |
34 |
35 |
36 | // 0 = floppy 0 (fd0, A:)
37 | // 1 = floppy 1 (fd1, B:)
38 | // 2 = hard disk 0 (hd0, C: or D:, depends by partitions)
39 | // 3 = hard disk 1 (hd1)
40 | constexpr int DISKCOUNT = 4;
41 |
42 |
43 | #define BIOS_SEG 0xF000
44 | #define BIOS_OFF 0x0100
45 | #define BIOS_ADDR (BIOS_SEG * 16 + BIOS_OFF)
46 |
47 |
48 | // BIOS Data Area
49 |
50 | #define BIOS_DATAAREA_SEG 0x40
51 | #define BIOS_DATAAREA_ADDR (BIOS_DATAAREA_SEG << 4)
52 |
53 | #define BIOS_KBDSHIFTFLAGS1 0x17 // keyboard shift flags
54 | #define BIOS_KBDSHIFTFLAGS2 0x18 // more keyboard shift flags
55 | #define BIOS_KBDALTKEYPADENTRY 0x19 // Storage for alternate keypad entry
56 | #define BIOS_KBDBUFHEAD 0x1a // pointer to next character in keyboard buffer
57 | #define BIOS_KBDBUFTAIL 0x1c // pointer to first available spot in keyboard buffer
58 | #define BIOS_KBDBUF 0x1e // keyboard buffer (32 bytes, 16 keys, but actually 15)
59 | #define BIOS_DISKLASTSTATUS 0x41 // diskette status return code
60 | #define BIOS_SYSTICKS 0x6c // system ticks (dword)
61 | #define BIOS_CLKROLLOVER 0x70 // system tick rollover flag (24h)
62 | #define BIOS_CTRLBREAKFLAG 0x71 // Ctrl-Break flag
63 | #define BIOS_HDLASTSTATUS 0x74 // HD status return code
64 | #define BIOS_NUMHD 0x75 // number of fixed disk drives
65 | #define BIOS_DRIVE0MEDIATYPE 0x90 // media type of drive 0
66 | #define BIOS_DRIVE1MEDIATYPE 0x91 // media type of drive 1
67 | #define BIOS_KBDMODE 0x96 // keyboard mode and other shift flags
68 | #define BIOS_KBDLEDS 0x97 // keyboard LEDs
69 | #define BIOS_PRINTSCREENFLAG 0x100 // PRINTSCREEN flag
70 |
71 |
72 | // Extended BIOS Data Area (EBDA)
73 |
74 | #define EBDA_SEG 0x9fc0 // EBDA Segment, must match with same value in bios.asm
75 | #define EBDA_ADDR (EBDA_SEG << 4)
76 |
77 | #define EBDA_DRIVER_OFFSET 0x22 // Pointing device device driver far call offset
78 | #define EBDA_DRIVER_SEG 0x24 // Pointing device device driver far call segment
79 | #define EBDA_FLAGS1 0x26 // Flags 1 (bits 0-2: recv data index)
80 | #define EBDA_FLAGS2 0x27 // Flags 2 (bits 0-2: packet size, bit 7: device handler installed)
81 | #define EBDA_PACKET 0x28 // Start of packet
82 |
83 |
84 |
85 |
86 | using fabgl::PS2Controller;
87 | using fabgl::Keyboard;
88 | using fabgl::Mouse;
89 | using fabgl::i8042;
90 | using fabgl::MC146818;
91 |
92 |
93 | enum MediaType {
94 | mediaUnknown,
95 | floppy160KB,
96 | floppy180KB,
97 | floppy320KB,
98 | floppy360KB,
99 | floppy720KB,
100 | floppy1M2K,
101 | floppy1M44K,
102 | floppy2M88K,
103 |
104 | HDD,
105 | };
106 |
107 | class Machine;
108 |
109 |
110 | class BIOS {
111 |
112 | public:
113 |
114 | BIOS();
115 |
116 | void init(Machine * machine);
117 | void reset();
118 |
119 | void helpersEntry();
120 |
121 | void diskHandlerEntry();
122 |
123 | void videoHandlerEntry();
124 |
125 | void setDriveMediaType(int drive, MediaType media);
126 |
127 | private:
128 |
129 | void getKeyFromKeyboard();
130 | void getKeyFromBuffer();
131 | void getKeyboardFlags();
132 | void setKeyboardTypematicAndDelay();
133 | void storeKeyboardKeyData();
134 |
135 | bool storeKeyInKbdBuffer(uint16_t syscode);
136 | bool processScancode(int scancode, uint16_t * syscode);
137 | void emptyKbdBuffer();
138 |
139 | void pointingDeviceInterface();
140 |
141 | void syncTicksWithRTC();
142 |
143 | void diskHandler_floppy();
144 | bool diskHandler_calcAbsAddr(int drive, uint32_t * pos, uint32_t * dest, uint32_t * count);
145 | void diskHandler_floppyExit(uint8_t err, bool setErrStat);
146 | void diskHandler_HD();
147 | void diskHandler_HDExit(uint8_t err, bool setErrStat);
148 |
149 | bool checkDriveMediaType(int drive);
150 | uint32_t getDriveMediaTableAddr(int drive);
151 |
152 |
153 | Machine * m_machine;
154 | uint8_t * m_memory;
155 | Keyboard * m_keyboard;
156 | Mouse * m_mouse;
157 | i8042 * m_i8042;
158 | MC146818 * m_MC146818;
159 |
160 | // state of multibyte scancode intermediate reception:
161 | // 0 = none
162 | // pause (0xe1 0x1d 0x45 0xe1 0x9d 0xc5): 1 = 0x1d, 2 = 0x45, 3 = 0x9d
163 | uint8_t m_kbdScancodeComp;
164 |
165 | // address of bios.asm:media_drive_parameters (0, 1)
166 | // address of int41 (2), int46 (3)
167 | //uint32_t m_mediaDriveParametersAddr[4];
168 |
169 | // original int 1e address (may be changed by O.S.)
170 | uint32_t m_origInt1EAddr;
171 |
172 | // media type for floppy (0,1) and HD (>=2)
173 | MediaType m_mediaType[DISKCOUNT];
174 |
175 |
176 | };
177 |
--------------------------------------------------------------------------------
/machine.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) -
3 | Copyright (c) 2019-2022 Fabrizio Di Vittorio.
4 | All rights reserved.
5 |
6 |
7 | * Please contact fdivitto2013@gmail.com if you need a commercial license.
8 |
9 |
10 | * This library and related software is available under GPL v3.
11 |
12 | FabGL is free software: you can redistribute it and/or modify
13 | it under the terms of the GNU General Public License as published by
14 | the Free Software Foundation, either version 3 of the License, or
15 | (at your option) any later version.
16 |
17 | FabGL is distributed in the hope that it will be useful,
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 | GNU General Public License for more details.
21 |
22 | You should have received a copy of the GNU General Public License
23 | along with FabGL. If not, see .
24 | */
25 |
26 |
27 | #include "machine.h"
28 |
29 | #include
30 | #include
31 | #include
32 | #include
33 | #include
34 |
35 |
36 | #pragma GCC optimize ("O3")
37 |
38 |
39 |
40 |
41 | // CGA Craphics Card Ports Bits
42 |
43 | #define CGA_MODECONTROLREG_TEXT80 0x01 // 0 = 40x25, 1 = 80x25
44 | #define CGA_MODECONTROLREG_GRAPHICS 0x02 // 0 = text, 1 = graphics
45 | #define CGA_MODECONTROLREG_COLOR 0x04 // 0 = color, 1 = monochrome
46 | #define CGA_MODECONTROLREG_ENABLED 0x08 // 0 = video off, 1 = video on
47 | #define CGA_MODECONTROLREG_GRAPH640 0x10 // 0 = 320x200 graphics, 1 = 640x200 graphics
48 | #define CGA_MODECONTROLREG_BIT7BLINK 0x20 // 0 = text mode bit 7 controls background, 1 = text mode bit 7 controls blinking
49 |
50 | #define CGA_COLORCONTROLREG_BACKCOLR_MASK 0x0f // mask for 320x200 background color index (on 640x200 is the foreground)
51 | #define CGA_COLORCONTROLREG_HIGHINTENSITY 0x10 // select high intensity colors
52 | #define CGA_COLORCONTROLREG_PALETTESEL 0x20 // 0 is Green, red and brown, 1 is Cyan, magenta and white
53 |
54 |
55 | // Hercules (HGC) Ports Bits
56 |
57 | #define HGC_MODECONTROLREG_GRAPHICS 0x02 // 0 = text mode, 1 = graphics mode
58 | #define HGC_MODECONTROLREG_ENABLED 0x08 // 0 = video off, 1 = video on
59 | #define HGC_MODECONTROLREG_BIT7BLINK 0x20 // 0 = text mode bit 7 controls background, 1 = text mode bit 7 controls blinking
60 | #define HGC_MODECONTROLREG_GRAPHICSPAGE 0x80 // 0 = graphics mapped on page 0 (0xB0000), 1 = graphics mapped on page 1 (0xB8000)
61 |
62 | #define HGC_CONFSWITCH_ALLOWGRAPHICSMODE 0x01 // 0 = prevents graphics mode, 1 = allows graphics mode
63 | #define HGC_CONFSWITCH_ALLOWPAGE1 0x02 // 0 = prevents access to page 1, 1 = allows access to page 1
64 |
65 |
66 | // I/O expander (based on MCP23S17) ports
67 |
68 | #define EXTIO_CONFIG 0x00e0 // configuration port (see EXTIO_CONFIG_.... flags)
69 | // whole 8 bit ports handling
70 | #define EXTIO_DIRA 0x00e1 // port A direction (0 = input, 1 = output)
71 | #define EXTIO_DIRB 0x00e2 // port B direction (0 = input, 1 = output)
72 | #define EXTIO_PULLUPA 0x00e3 // port A pullup enable (0 = disabled, 1 = enabled)
73 | #define EXTIO_PULLUPB 0x00e4 // port B pullup enable (0 = disabled, 1 = enabled)
74 | #define EXTIO_PORTA 0x00e5 // port A read/write
75 | #define EXTIO_PORTB 0x00e6 // port B read/write
76 | // single GPIO handling
77 | #define EXTIO_GPIOSEL 0x00e7 // GPIO selection (0..7 = PA0..PA7, 8..15 = PB0..PB8)
78 | #define EXTIO_GPIOCONF 0x00e8 // selected GPIO direction and pullup (0 = input, 1 = output, 2 = input with pullup)
79 | #define EXTIO_GPIO 0x00e9 // selected GPIO read or write (0 = low, 1 = high)
80 |
81 | // I/O expander configuration bits
82 | #define EXTIO_CONFIG_AVAILABLE 0x01 // 1 = external IO available, 0 = not available
83 | #define EXTIO_CONFIG_INT_POLARITY 0x02 // 1 = positive polarity, 0 = negative polarity (default)
84 |
85 |
86 |
87 | //////////////////////////////////////////////////////////////////////////////////////
88 | // Machine
89 |
90 |
91 | using fabgl::i8086;
92 |
93 |
94 | uint8_t * Machine::s_memory;
95 | uint8_t * Machine::s_videoMemory;
96 |
97 |
98 | Machine::Machine() :
99 | #ifdef FABGL_EMULATED
100 | m_stepCallback(nullptr),
101 | #endif
102 | m_diskFilename(),
103 | m_disk(),
104 | m_frameBuffer(nullptr),
105 | m_bootDrive(0),
106 | m_sysReqCallback(nullptr),
107 | m_baseDir(nullptr)
108 | {
109 | }
110 |
111 |
112 | Machine::~Machine()
113 | {
114 | for (int i = 0; i < DISKCOUNT; ++i) {
115 | free(m_diskFilename[i]);
116 | if (m_disk[i])
117 | fclose(m_disk[i]);
118 | }
119 | vTaskDelete(m_taskHandle);
120 | free(s_videoMemory);
121 | }
122 |
123 |
124 | void Machine::init()
125 | {
126 | srand((uint32_t)time(NULL));
127 |
128 | // to avoid PSRAM bug without -mfix-esp32-psram-cache-issue
129 | // core 0 can only work reliably with the lower 2 MB and core 1 only with the higher 2 MB.
130 | s_memory = (uint8_t*)(SOC_EXTRAM_DATA_LOW + (xPortGetCoreID() == 1 ? 2 * 1024 * 1024 : 0));
131 |
132 | s_videoMemory = (uint8_t*)heap_caps_malloc(VIDEOMEMSIZE, MALLOC_CAP_8BIT | MALLOC_CAP_INTERNAL);
133 |
134 | memset(s_memory, 0, RAM_SIZE);
135 |
136 | m_soundGen.play(true);
137 | m_soundGen.attach(&m_sinWaveGen);
138 |
139 | m_i8042.init();
140 | m_i8042.setCallbacks(this, keyboardInterrupt, mouseInterrupt, resetMachine, sysReq);
141 |
142 | m_PIT8253.setCallbacks(this, PITChangeOut);
143 | m_PIT8253.reset();
144 |
145 | m_MC146818.init("PCEmulator");
146 | m_MC146818.setCallbacks(this, MC146818Interrupt);
147 |
148 | m_MCP23S17.begin();
149 | m_MCP23S17Sel = 0;
150 |
151 | m_BIOS.init(this);
152 |
153 | i8086::setCallbacks(this, readPort, writePort, writeVideoMemory8, writeVideoMemory16, readVideoMemory8, readVideoMemory16, interrupt);
154 | i8086::setMemory(s_memory);
155 |
156 | m_reset = true;
157 | }
158 |
159 |
160 | void Machine::setDriveImage(int drive, char const * filename, int cylinders, int heads, int sectors)
161 | {
162 | if (m_disk[drive]) {
163 | fclose(m_disk[drive]);
164 | m_disk[drive] = nullptr;
165 | }
166 |
167 | if (m_diskFilename[drive]) {
168 | free(m_diskFilename[drive]);
169 | m_diskFilename[drive] = nullptr;
170 | }
171 |
172 | m_BIOS.setDriveMediaType(drive, mediaUnknown);
173 |
174 | m_diskCylinders[drive] = cylinders;
175 | m_diskHeads[drive] = heads;
176 | m_diskSectors[drive] = sectors;
177 |
178 | if (filename) {
179 | m_diskFilename[drive] = strdup(filename);
180 | m_disk[drive] = FileBrowser(m_baseDir).openFile(filename, "r+b");
181 | if (m_disk[drive]) {
182 |
183 | // get image file size
184 | fseek(m_disk[drive], 0L, SEEK_END);
185 | m_diskSize[drive] = ftell(m_disk[drive]);
186 |
187 | // need to detect geometry?
188 | if (cylinders == 0 || heads == 0 || sectors == 0)
189 | autoDetectDriveGeometry(drive);
190 | }
191 | }
192 | }
193 |
194 |
195 | void Machine::autoDetectDriveGeometry(int drive)
196 | {
197 | // well known floppy formats
198 | static const struct {
199 | uint16_t tracks;
200 | uint8_t sectors;
201 | uint8_t heads;
202 | } FLOPPYFORMATS[] = {
203 | { 40, 8, 1 }, // 163840 bytes (160K, 5.25 inch)
204 | { 40, 9, 1 }, // 184320 bytes (180K, 5.25 inch)
205 | { 40, 8, 2 }, // 327680 bytes (320K, 5.25 inch)
206 | { 40, 9, 2 }, // 368640 bytes (360K, 5.25 inch)
207 | { 80, 9, 2 }, // 737280 bytes (720K, 3.5 inch)
208 | { 80, 15, 2 }, // 1228800 bytes (1200K, 5.25 inch)
209 | { 80, 18, 2 }, // 1474560 bytes (1440K, 3.5 inch)
210 | { 80, 36, 2 }, // 2949120 bytes (2880K, 3.5 inch)
211 | };
212 |
213 | // look for well known floppy formats
214 | for (auto const & ff : FLOPPYFORMATS) {
215 | if (512 * ff.tracks * ff.sectors * ff.heads == m_diskSize[drive]) {
216 | m_diskCylinders[drive] = ff.tracks;
217 | m_diskHeads[drive] = ff.heads;
218 | m_diskSectors[drive] = ff.sectors;
219 | //printf("autoDetectDriveGeometry, found floppy: t=%d s=%d h=%d\n", ff.tracks, ff.sectors, ff.heads);
220 | return;
221 | }
222 | }
223 |
224 | // maybe an hard disk, try to calculate geometry (max 528MB, common lower end for BIOS and MSDOS: https://tldp.org/HOWTO/Large-Disk-HOWTO-4.html)
225 | constexpr int MAXCYLINDERS = 1024; // Cylinders : 1...1024
226 | constexpr int MAXHEADS = 16; // Heads : 1...16 (actual limit is 256)
227 | constexpr int MAXSECTORS = 63; // Sectors : 1...63
228 | int c = 1, h = 1;
229 | int s = (int)(m_diskSize[drive] / 512);
230 | if (s > MAXSECTORS) {
231 | h = s / MAXSECTORS;
232 | s = MAXSECTORS;
233 | }
234 | if (h > MAXHEADS) {
235 | c = h / MAXHEADS;
236 | h = MAXHEADS;
237 | }
238 | if (c > MAXCYLINDERS)
239 | c = MAXCYLINDERS;
240 | m_diskCylinders[drive] = c;
241 | m_diskHeads[drive] = h;
242 | m_diskSectors[drive] = s;
243 | //printf("autoDetectDriveGeometry, found HD: c=%d h=%d s=%d (tot=%d filesz=%d)\n", m_diskCylinders[drive], m_diskHeads[drive], m_diskSectors[drive], 512 * m_diskCylinders[drive] * m_diskHeads[drive] * m_diskSectors[drive], (int)m_diskSize[drive]);
244 |
245 | }
246 |
247 |
248 | void Machine::reset()
249 | {
250 | m_reset = false;
251 |
252 | m_ticksCounter = 0;
253 |
254 | m_CGAMemoryOffset = 0;
255 | m_CGAModeReg = 0;
256 | m_CGAColorReg = 0;
257 | m_CGAVSyncQuery = 0;
258 |
259 | m_HGCMemoryOffset = 0;
260 | m_HGCModeReg = 0;
261 | m_HGCSwitchReg = 0;
262 | m_HGCVSyncQuery = 0;
263 |
264 | m_speakerDataEnable = false;
265 |
266 | m_i8042.reset();
267 |
268 | m_PIC8259A.reset();
269 | m_PIC8259B.reset();
270 |
271 | m_PIT8253.reset();
272 | m_PIT8253.setGate(0, true);
273 | //m_PIT8253.setGate(1, true); // @TODO: timer 1 used for DRAM refresh, required to run?
274 |
275 | m_MC146818.reset();
276 |
277 | memset(m_CGA6845, 0, sizeof(m_CGA6845));
278 | memset(m_HGC6845, 0, sizeof(m_HGC6845));
279 |
280 | m_BIOS.reset();
281 |
282 | i8086::reset();
283 |
284 | // set boot drive (0, 1, 0x80, 0x81)
285 | i8086::setDL((m_bootDrive & 1) | (m_bootDrive > 1 ? 0x80 : 0x00));
286 | }
287 |
288 |
289 | void Machine::run()
290 | {
291 | xTaskCreatePinnedToCore(&runTask, "", 4000, this, 5, &m_taskHandle, CoreUsage::quietCore());
292 | }
293 |
294 |
295 | void IRAM_ATTR Machine::runTask(void * pvParameters)
296 | {
297 | auto m = (Machine*)pvParameters;
298 |
299 | m->init();
300 |
301 | while (true) {
302 |
303 | if (m->m_reset)
304 | m->reset();
305 |
306 | #ifdef FABGL_EMULATED
307 | pthread_testcancel();
308 | if (m->m_stepCallback)
309 | m->m_stepCallback(m);
310 | #endif
311 |
312 | i8086::step();
313 | m->tick();
314 |
315 | }
316 | }
317 |
318 |
319 | void Machine::tick()
320 | {
321 | ++m_ticksCounter;
322 |
323 | if ((m_ticksCounter & 0x7f) == 0x7f) {
324 | m_PIT8253.tick();
325 | // run keyboard controller every PIT tick (just to not overload CPU with continous checks)
326 | m_i8042.tick();
327 | }
328 |
329 | if (m_PIC8259A.pendingInterrupt() && i8086::IRQ(m_PIC8259A.pendingInterruptNum()))
330 | m_PIC8259A.ackPendingInterrupt();
331 | if (m_PIC8259B.pendingInterrupt() && i8086::IRQ(m_PIC8259B.pendingInterruptNum()))
332 | m_PIC8259B.ackPendingInterrupt();
333 |
334 | }
335 |
336 |
337 | void Machine::setCGA6845Register(uint8_t value)
338 | {
339 | m_CGA6845[m_CGA6845SelectRegister] = value;
340 |
341 | switch (m_CGA6845SelectRegister) {
342 |
343 | // cursor start: (bits 5,6 = cursor blink and visibility control), bits 0..4 cursor start scanline
344 | case 0x0a:
345 | m_graphicsAdapter.setCursorVisible((m_CGA6845[0xa] >> 5) >= 2);
346 | // no break!
347 | // cursor end: bits 0..4 cursor end scanline
348 | case 0x0b:
349 | m_graphicsAdapter.setCursorShape(2 * (m_CGA6845[0xa] & 0x1f), 2 * (m_CGA6845[0xb] & 0x1f));
350 | break;
351 |
352 | // video memory start offset (0x0c = H, 0x0d = L)
353 | case 0x0c:
354 | case 0x0d:
355 | m_CGAMemoryOffset = ((m_CGA6845[0xc] << 8) | m_CGA6845[0xd]) << 1;
356 | setCGAMode();
357 | break;
358 |
359 | // cursor position (0x0e = H and 0x0f = L)
360 | case 0x0e:
361 | case 0x0f:
362 | {
363 | int pos = (m_CGA6845[0xe] << 8) | m_CGA6845[0xf];
364 | m_graphicsAdapter.setCursorPos(pos / m_graphicsAdapter.getTextColumns(), pos % m_graphicsAdapter.getTextColumns());
365 | break;
366 | }
367 |
368 | }
369 | }
370 |
371 |
372 | void Machine::setCGAMode()
373 | {
374 | if ((m_CGAModeReg & CGA_MODECONTROLREG_ENABLED) == 0) {
375 |
376 | // video disabled
377 | //printf("CGA, video disabled\n");
378 | m_graphicsAdapter.enableVideo(false);
379 |
380 | } else if ((m_CGAModeReg & CGA_MODECONTROLREG_TEXT80) == 0 && (m_CGAModeReg & CGA_MODECONTROLREG_GRAPHICS) == 0) {
381 |
382 | // 40 column text mode
383 | //printf("CGA, 40 columns text mode\n");
384 | m_frameBuffer = s_videoMemory + 0x8000 + m_CGAMemoryOffset;
385 | m_graphicsAdapter.setVideoBuffer(m_frameBuffer);
386 | m_graphicsAdapter.setEmulation(GraphicsAdapter::Emulation::PC_Text_40x25_16Colors);
387 | m_graphicsAdapter.setBit7Blink(m_CGAModeReg & CGA_MODECONTROLREG_BIT7BLINK);
388 | m_graphicsAdapter.enableVideo(true);
389 |
390 | } else if ((m_CGAModeReg & CGA_MODECONTROLREG_TEXT80) && (m_CGAModeReg & CGA_MODECONTROLREG_GRAPHICS) == 0) {
391 |
392 | // 80 column text mode
393 | //printf("CGA, 80 columns text mode\n");
394 | m_frameBuffer = s_videoMemory + 0x8000 + m_CGAMemoryOffset;
395 | m_graphicsAdapter.setVideoBuffer(m_frameBuffer);
396 | m_graphicsAdapter.setEmulation(GraphicsAdapter::Emulation::PC_Text_80x25_16Colors);
397 | m_graphicsAdapter.setBit7Blink(m_CGAModeReg & CGA_MODECONTROLREG_BIT7BLINK);
398 | m_graphicsAdapter.enableVideo(true);
399 |
400 | } else if ((m_CGAModeReg & CGA_MODECONTROLREG_GRAPHICS) && (m_CGAModeReg & CGA_MODECONTROLREG_GRAPH640) == 0) {
401 |
402 | // 320x200 graphics
403 | //printf("CGA, 320x200 graphics mode\n");
404 | m_frameBuffer = s_videoMemory + 0x8000 + m_CGAMemoryOffset;
405 | m_graphicsAdapter.setVideoBuffer(m_frameBuffer);
406 | m_graphicsAdapter.setEmulation(GraphicsAdapter::Emulation::PC_Graphics_320x200_4Colors);
407 | int paletteIndex = (bool)(m_CGAColorReg & CGA_COLORCONTROLREG_PALETTESEL) * 2 + (bool)(m_CGAColorReg & CGA_COLORCONTROLREG_HIGHINTENSITY);
408 | m_graphicsAdapter.setPCGraphicsPaletteInUse(paletteIndex);
409 | m_graphicsAdapter.setPCGraphicsBackgroundColorIndex(m_CGAColorReg & CGA_COLORCONTROLREG_BACKCOLR_MASK);
410 | m_graphicsAdapter.enableVideo(true);
411 |
412 | } else if ((m_CGAModeReg & CGA_MODECONTROLREG_GRAPHICS) && (m_CGAModeReg & CGA_MODECONTROLREG_GRAPH640)) {
413 |
414 | // 640x200 graphics
415 | //printf("CGA, 640x200 graphics mode\n");
416 | m_frameBuffer = s_videoMemory + 0x8000 + m_CGAMemoryOffset;
417 | m_graphicsAdapter.setVideoBuffer(m_frameBuffer);
418 | m_graphicsAdapter.setEmulation(GraphicsAdapter::Emulation::PC_Graphics_640x200_2Colors);
419 | m_graphicsAdapter.setPCGraphicsForegroundColorIndex(m_CGAColorReg & CGA_COLORCONTROLREG_BACKCOLR_MASK);
420 | m_graphicsAdapter.enableVideo(true);
421 |
422 | }
423 | }
424 |
425 |
426 | void Machine::setHGC6845Register(uint8_t value)
427 | {
428 | m_HGC6845[m_HGC6845SelectRegister] = value;
429 | switch (m_HGC6845SelectRegister) {
430 |
431 | // cursor start: (bits 5,6 = cursor blink and visibility control), bits 0..4 cursor start scanline
432 | case 0x0a:
433 | m_graphicsAdapter.setCursorVisible((m_HGC6845[0xa] >> 5) >= 2);
434 | // cursor end: bits 0..4 cursor end scanline
435 | case 0x0b:
436 | m_graphicsAdapter.setCursorShape((m_HGC6845[0xa] & 0x1f), (m_HGC6845[0xb] & 0x1f));
437 | break;
438 |
439 | // video memory start offset (0x0c = H, 0x0d = L)
440 | case 0x0c:
441 | case 0x0d:
442 | m_HGCMemoryOffset = ((m_HGC6845[0xc] << 8) | m_HGC6845[0xd]) << 1;
443 | setHGCMode();
444 | break;
445 |
446 | // cursor position (0x0e = H and 0x0f = L)
447 | case 0x0e:
448 | case 0x0f:
449 | {
450 | int pos = (m_HGC6845[0xe] << 8) | m_HGC6845[0xf];
451 | m_graphicsAdapter.setCursorPos(pos / m_graphicsAdapter.getTextColumns(), pos % m_graphicsAdapter.getTextColumns());
452 | break;
453 | }
454 |
455 | }
456 | }
457 |
458 |
459 | void Machine::setHGCMode()
460 | {
461 | constexpr int HGC_OFFSET_PAGE0 = 0x0000;
462 | constexpr int HGC_OFFSET_PAGE1 = 0x8000;
463 |
464 | if ((m_HGCModeReg & HGC_MODECONTROLREG_ENABLED) == 0) {
465 |
466 | // video disabled
467 | //printf("Hercules, video disabled\n");
468 | m_graphicsAdapter.enableVideo(false);
469 |
470 | } else if ((m_HGCModeReg & HGC_MODECONTROLREG_GRAPHICS) == 0 || (m_HGCSwitchReg & HGC_CONFSWITCH_ALLOWGRAPHICSMODE) == 0) {
471 |
472 | // text mode
473 | //printf("Hercules, text mode\n");
474 | m_frameBuffer = s_videoMemory + HGC_OFFSET_PAGE0;
475 | m_graphicsAdapter.setVideoBuffer(m_frameBuffer);
476 | m_graphicsAdapter.setEmulation(GraphicsAdapter::Emulation::PC_Text_80x25_16Colors);
477 | m_graphicsAdapter.setBit7Blink(m_HGCModeReg & HGC_MODECONTROLREG_BIT7BLINK);
478 | m_graphicsAdapter.enableVideo(true);
479 |
480 | } else if ((m_HGCModeReg &HGC_MODECONTROLREG_GRAPHICS)) {
481 |
482 | // graphics mode
483 | //printf("Hercules, graphics mode\n");
484 | int offset = (m_HGCModeReg & HGC_MODECONTROLREG_GRAPHICSPAGE) && (m_HGCSwitchReg & HGC_CONFSWITCH_ALLOWPAGE1) ? HGC_OFFSET_PAGE1 : HGC_OFFSET_PAGE0;
485 | m_frameBuffer = s_videoMemory + offset;
486 | m_graphicsAdapter.setVideoBuffer(m_frameBuffer);
487 | m_graphicsAdapter.setEmulation(GraphicsAdapter::Emulation::PC_Graphics_HGC_720x348);
488 | m_graphicsAdapter.enableVideo(true);
489 |
490 | }
491 |
492 | }
493 |
494 |
495 | void Machine::writePort(void * context, int address, uint8_t value)
496 | {
497 | auto m = (Machine*)context;
498 |
499 | switch (address) {
500 |
501 | // PIC8259A
502 | case 0x20:
503 | case 0x21:
504 | m->m_PIC8259A.write(address & 1, value);
505 | break;
506 |
507 | // PIC8259B
508 | case 0xa0:
509 | case 0xa1:
510 | m->m_PIC8259B.write(address & 1, value);
511 | break;
512 |
513 | // PIT8253
514 | case 0x0040:
515 | case 0x0041:
516 | case 0x0042:
517 | case 0x0043:
518 | m->m_PIT8253.write(address & 3, value);
519 | if ((address == 0x43 && (value >> 6) == 2) || address == 0x42)
520 | m->speakerSetFreq();
521 | break;
522 |
523 | // 8042 keyboard controller input
524 | case 0x0060:
525 | m->m_i8042.write(0, value);
526 | break;
527 |
528 | // PortB
529 | // bit 1 : speaker data enable
530 | // bit 0 : timer 2 gate
531 | case 0x0061:
532 | m->m_speakerDataEnable = value & 0x02;
533 | m->m_PIT8253.setGate(2, value & 0x01);
534 | m->speakerEnableDisable();
535 | break;
536 |
537 | // 8042 keyboard controller input
538 | case 0x0064:
539 | m->m_i8042.write(1, value);
540 | break;
541 |
542 | // MC146818 RTC & RAM
543 | case 0x0070:
544 | case 0x0071:
545 | m->m_MC146818.write(address & 1, value);
546 | break;
547 |
548 | // CGA - CRT 6845 - register selection register
549 | case 0x3d4:
550 | m->m_CGA6845SelectRegister = value;
551 | break;
552 |
553 | // CGA - CRT 6845 - selected register write
554 | case 0x3d5:
555 | m->setCGA6845Register(value);
556 | break;
557 |
558 | // CGA - Mode Control Register
559 | case 0x3d8:
560 | m->m_CGAModeReg = value;
561 | m->setCGAMode();
562 | break;
563 |
564 | // CGA - Color Select register
565 | case 0x3d9:
566 | m->m_CGAColorReg = value;
567 | m->setCGAMode();
568 | break;
569 |
570 | // Hercules (HGC) - CRT 6845 - register selection register
571 | case 0x3b4:
572 | m->m_HGC6845SelectRegister = value;
573 | break;
574 |
575 | // Hercules (HGC) - CRT 6845 - selected register write
576 | case 0x3b5:
577 | m->setHGC6845Register(value);
578 | break;
579 |
580 | // Hercules (HGC) - Display Mode Control Port
581 | case 0x3b8:
582 | m->m_HGCModeReg = value;
583 | m->setHGCMode();
584 | break;
585 |
586 | // Hercules (HGC) - Configuration Switch
587 | case 0x3bf:
588 | m->m_HGCSwitchReg = value;
589 | m->setHGCMode();
590 | break;
591 |
592 | // I/O expander - Configuration
593 | case EXTIO_CONFIG:
594 | m->m_MCP23S17.setINTActiveHigh(value & EXTIO_CONFIG_INT_POLARITY);
595 | break;
596 |
597 | // I/O expander - Port A/B Direction
598 | case EXTIO_DIRA ... EXTIO_DIRB:
599 | m->m_MCP23S17.setPortDir(address - EXTIO_DIRA + MCP_PORTA, ~value);
600 | printf("dir %d = %02X\n", address - EXTIO_DIRA + MCP_PORTA, ~value);
601 | break;
602 |
603 | // I/O expander - Port A/B pullup
604 | case EXTIO_PULLUPA ... EXTIO_PULLUPB:
605 | m->m_MCP23S17.enablePortPullUp(address - EXTIO_PULLUPA + MCP_PORTA, value);
606 | break;
607 |
608 | // I/O expander - Port A/B write
609 | case EXTIO_PORTA ... EXTIO_PORTB:
610 | m->m_MCP23S17.writePort(address - EXTIO_PORTA + MCP_PORTA, value);
611 | printf("set %d = %02X\n", address - EXTIO_PORTA + MCP_PORTA, value);
612 | break;
613 |
614 | // I/O expander - GPIO selection
615 | case EXTIO_GPIOSEL:
616 | m->m_MCP23S17Sel = value & 0xf;
617 | break;
618 |
619 | // I/O expander - GPIO direction and pullup
620 | case EXTIO_GPIOCONF:
621 | m->m_MCP23S17.configureGPIO(m->m_MCP23S17Sel, value & 1 ? fabgl::MCPDir::Output : fabgl::MCPDir::Input, value & 2);
622 | break;
623 |
624 | // I/O expander - GPIO write
625 | case EXTIO_GPIO:
626 | m->m_MCP23S17.writeGPIO(m->m_MCP23S17Sel, value);
627 | break;
628 |
629 | default:
630 | //printf("OUT %04x=%02x\n", address, value);
631 | break;
632 | }
633 | }
634 |
635 |
636 | uint8_t Machine::readPort(void * context, int address)
637 | {
638 | auto m = (Machine*)context;
639 |
640 | switch (address) {
641 |
642 | // PIC8259A
643 | case 0x0020:
644 | case 0x0021:
645 | return m->m_PIC8259A.read(address & 1);
646 |
647 | // PIC8259B
648 | case 0x00a0:
649 | case 0x00a1:
650 | return m->m_PIC8259B.read(address & 1);
651 |
652 | // PIT8253
653 | case 0x0040:
654 | case 0x0041:
655 | case 0x0042:
656 | case 0x0043:
657 | return m->m_PIT8253.read(address & 3);
658 |
659 | // 8042 keyboard controller output
660 | case 0x0060:
661 | return m->m_i8042.read(0);
662 |
663 | // Port B
664 | // bit 5 : timer 2 out
665 | // bit 4 : toggles every 15.085us (DMA refresh)
666 | // bit 1 : speaker data enable
667 | // bit 0 : timer 2 gate
668 | case 0x0061:
669 | return ((int)m->m_PIT8253.getOut(2) << 5) | // bit 5
670 | (esp_timer_get_time() & 0x10) | // bit 4 (toggles every 16us)
671 | ((int)m->m_speakerDataEnable << 1) | // bit 1
672 | ((int)m->m_PIT8253.getGate(2)); // bit 0
673 |
674 | // I/O port
675 | case 0x0062:
676 | return 0x20 * m->m_PIT8253.getOut(2); // bit 5 = timer 2 output
677 |
678 | // 8042 keyboard controller status register
679 | case 0x0064:
680 | return m->m_i8042.read(1);
681 |
682 | // MC146818 RTC & RAM
683 | case 0x0070:
684 | case 0x0071:
685 | return m->m_MC146818.read(address & 1);
686 |
687 | // CGA - CRT 6845 - register selection register
688 | case 0x3d4:
689 | return 0x00; // not readable
690 |
691 | // CGA - CRT 6845 - selected register read
692 | case 0x3d5:
693 | return m->m_CGA6845SelectRegister >= 14 && m->m_CGA6845SelectRegister < 16 ? m->m_CGA6845[m->m_CGA6845SelectRegister] : 0x00;
694 |
695 | // CGA - Color Select register
696 | // note: this register should be write-only, but some games do not work if it isn't readable
697 | case 0x3d9:
698 | return m->m_CGAColorReg;
699 |
700 | // CGA - Status Register
701 | // real vertical sync is too fast for our slowly emulated 8086, so
702 | // here it is just a fake, just to allow programs that check it to keep going anyway.
703 | case 0x3da:
704 | m->m_CGAVSyncQuery += 1;
705 | return (m->m_CGAVSyncQuery & 0x7) != 0 ? 0x09 : 0x00; // "not VSync" (0x00) every 7 queries
706 |
707 | // Hercules (HGC) - register selection register
708 | case 0x3b4:
709 | return 0x00; // not readable
710 |
711 | // Hercules (HGC) - selected register read
712 | case 0x3b5:
713 | return m->m_HGC6845SelectRegister >= 14 && m->m_HGC6845SelectRegister < 16 ? m->m_HGC6845[m->m_HGC6845SelectRegister] : 0x00;
714 |
715 | // Hercules (HGC) - Display Status Port
716 | // real vertical sync is too fast for our slowly emulated 8086, so
717 | // here it is just a fake, just to allow programs that check it to keep going anyway.
718 | case 0x3ba:
719 | m->m_HGCVSyncQuery += 1;
720 | return (m->m_HGCVSyncQuery & 0x7) != 0 ? 0x00 : 0x80; // "not VSync" (0x80) every 7 queries
721 |
722 | // I/O expander - Configuration
723 | case EXTIO_CONFIG:
724 | return (m->m_MCP23S17.available() ? EXTIO_CONFIG_AVAILABLE : 0) |
725 | (m->m_MCP23S17.getINTActiveHigh() ? EXTIO_CONFIG_INT_POLARITY : 0);
726 |
727 | // I/O expander - Port A/B Direction
728 | case EXTIO_DIRA ... EXTIO_DIRB:
729 | return m->m_MCP23S17.getPortDir(address - EXTIO_DIRA + MCP_PORTA);
730 |
731 | // I/O expander - Port A/B pullup
732 | case EXTIO_PULLUPA ... EXTIO_PULLUPB:
733 | return m->m_MCP23S17.getPortPullUp(address - EXTIO_PULLUPA + MCP_PORTA);
734 |
735 | // I/O expander - Port A/B read
736 | case EXTIO_PORTA ... EXTIO_PORTB:
737 | return m->m_MCP23S17.readPort(address - EXTIO_PORTA + MCP_PORTA);
738 |
739 | // I/O expander - GPIO selection
740 | case EXTIO_GPIOSEL:
741 | return m->m_MCP23S17Sel;
742 |
743 | // I/O expander - GPIO read
744 | case EXTIO_GPIO:
745 | return m->m_MCP23S17.readGPIO(m->m_MCP23S17Sel);
746 |
747 | }
748 |
749 | //printf("IN %04X\n", address);
750 | return 0xff;
751 | }
752 |
753 |
754 | void Machine::PITChangeOut(void * context, int timerIndex)
755 | {
756 | auto m = (Machine*)context;
757 |
758 | // timer 0 trigged?
759 | if (timerIndex == 0 && m->m_PIT8253.getOut(0) == true) {
760 | // yes, report 8259A-IR0 (IRQ0, INT 08h)
761 | m->m_PIC8259A.signalInterrupt(0);
762 | }
763 | }
764 |
765 |
766 | // reset from 8042
767 | bool Machine::resetMachine(void * context)
768 | {
769 | auto m = (Machine*)context;
770 | m->trigReset();
771 | return true;
772 | }
773 |
774 | // SYSREQ (ALT + PRINTSCREEN)
775 | bool Machine::sysReq(void * context)
776 | {
777 | auto m = (Machine*)context;
778 | if (m->m_sysReqCallback)
779 | m->m_sysReqCallback();
780 | return true;
781 | }
782 |
783 |
784 | // 8259A-IR1 (IRQ1, INT 09h)
785 | bool Machine::keyboardInterrupt(void * context)
786 | {
787 | auto m = (Machine*)context;
788 | return m->m_PIC8259A.signalInterrupt(1);
789 | }
790 |
791 |
792 | // 8259B-IR4 (IRQ12, INT 074h)
793 | bool Machine::mouseInterrupt(void * context)
794 | {
795 | auto m = (Machine*)context;
796 | return m->m_PIC8259B.signalInterrupt(4);
797 | }
798 |
799 |
800 | // interrupt from MC146818, trig 8259B-IR0 (IRQ8, INT 70h)
801 | bool Machine::MC146818Interrupt(void * context)
802 | {
803 | auto m = (Machine*)context;
804 | return m->m_PIC8259B.signalInterrupt(0);
805 | }
806 |
807 |
808 | void Machine::writeVideoMemory8(void * context, int address, uint8_t value)
809 | {
810 | //printf("WVMEM %05X <= %02X\n", address, value);
811 | if (address >= 0xb0000)
812 | s_videoMemory[address - 0xb0000] = value;
813 | }
814 |
815 |
816 | void Machine::writeVideoMemory16(void * context, int address, uint16_t value)
817 | {
818 | //printf("WVMEM %05X <= %04X\n", address, value);
819 | if (address >= 0xb0000)
820 | *(uint16_t*)(s_videoMemory + address - 0xb0000) = value;
821 | }
822 |
823 |
824 | uint8_t Machine::readVideoMemory8(void * context, int address)
825 | {
826 | if (address >= 0xb0000)
827 | return s_videoMemory[address - 0xb0000];
828 | else
829 | return 0xff;
830 | }
831 |
832 |
833 | uint16_t Machine::readVideoMemory16(void * context, int address)
834 | {
835 | if (address >= 0xb0000)
836 | return *(uint16_t*)(s_videoMemory + address - 0xb0000);
837 | else
838 | return 0xffff;
839 | }
840 |
841 |
842 | bool Machine::interrupt(void * context, int num)
843 | {
844 | auto m = (Machine*)context;
845 |
846 |
847 | // emu interrupts callable only inside the BIOS segment
848 | if (i8086::CS() == BIOS_SEG) {
849 | switch (num) {
850 |
851 | // Put Char for debug (AL)
852 | case 0xf4:
853 | printf("%c", i8086::AX() & 0xff);
854 | return true;
855 |
856 | // BIOS helpers (AH = select helper function)
857 | case 0xf5:
858 | m->m_BIOS.helpersEntry();
859 | return true;
860 |
861 | // set or reset flag CF before IRET, replacing value in stack with current value
862 | case 0xf6:
863 | {
864 | auto savedFlags = (uint16_t*) (s_memory + i8086::SS() * 16 + (uint16_t)(i8086::SP() + 4));
865 | *savedFlags = (*savedFlags & 0xfffe) | i8086::flagCF();
866 | return true;
867 | }
868 |
869 | // set or reset flag ZF before IRET, replacing value in stack with current value
870 | case 0xf7:
871 | {
872 | auto savedFlags = (uint16_t*) (s_memory + i8086::SS() * 16 + (uint16_t)(i8086::SP() + 4));
873 | *savedFlags = (*savedFlags & 0xffbf) | (i8086::flagZF() << 6);
874 | return true;
875 | }
876 |
877 | // set or reset flag IF before IRET, replacing value in stack with current value
878 | case 0xf8:
879 | {
880 | auto savedFlags = (uint16_t*) (s_memory + i8086::SS() * 16 + (uint16_t)(i8086::SP() + 4));
881 | *savedFlags = (*savedFlags & 0xfdff) | (i8086::flagIF() << 9);
882 | return true;
883 | }
884 |
885 | // test point P0
886 | case 0xf9:
887 | printf("P0 AX=%04X BX=%04X CX=%04X DX=%04X DS=%04X\n", i8086::AX(), i8086::BX(), i8086::CX(), i8086::DX(), i8086::DS());
888 | return true;
889 |
890 | // test point P1
891 | case 0xfa:
892 | printf("P1 AX=%04X BX=%04X CX=%04X DX=%04X DS=%04X\n", i8086::AX(), i8086::BX(), i8086::CX(), i8086::DX(), i8086::DS());
893 | return true;
894 |
895 | // BIOS disk handler (INT 13h)
896 | case 0xfb:
897 | m->m_BIOS.diskHandlerEntry();
898 | return true;
899 |
900 | // BIOS video handler (INT 10h)
901 | case 0xfc:
902 | m->m_BIOS.videoHandlerEntry();
903 | return true;
904 |
905 | }
906 | }
907 |
908 | // not hanlded
909 | return false;
910 | }
911 |
912 |
913 | void Machine::speakerSetFreq()
914 | {
915 | //printf("speakerSetFreq: count = %d\n", m_PIT8253.timerInfo(2).resetCount);
916 | int timerCount = m_PIT8253.timerInfo(2).resetCount;
917 | if (timerCount == 0)
918 | timerCount = 65536;
919 | int freq = PIT_TICK_FREQ / timerCount;
920 | //printf(" freq = %dHz\n", freq);
921 | m_sinWaveGen.setFrequency(freq);
922 | }
923 |
924 |
925 | void Machine::speakerEnableDisable()
926 | {
927 | bool genEnabled = m_PIT8253.getGate(2);
928 | if (genEnabled && m_speakerDataEnable) {
929 | // speaker enabled
930 | m_sinWaveGen.enable(true);
931 | } else {
932 | // speaker disabled
933 | m_sinWaveGen.enable(false);
934 | }
935 | }
936 |
937 |
938 | void Machine::dumpMemory(char const * filename)
939 | {
940 | constexpr int BLOCKLEN = 1024;
941 | auto file = FileBrowser(m_baseDir).openFile(filename, "wb");
942 | if (file) {
943 | for (int i = 0; i < 1048576; i += BLOCKLEN)
944 | fwrite(s_memory + i, 1, BLOCKLEN, file);
945 | fclose(file);
946 | }
947 | }
948 |
949 |
950 | void Machine::dumpInfo(char const * filename)
951 | {
952 | auto file = FileBrowser(m_baseDir).openFile(filename, "wb");
953 | if (file) {
954 | // CPU state
955 | fprintf(file, " CS DS ES SS\n");
956 | fprintf(file, "%04X %04X %04X %04X\n\n", i8086::CS(), i8086::DS(), i8086::ES(), i8086::SS());
957 | fprintf(file, " IP AX BX CX DX SI DI BP SP\n");
958 | fprintf(file, "%04X %04X %04X %04X %04X %04X %04X %04X %04X\n\n", i8086::IP(), i8086::AX(), i8086::BX(), i8086::CX(), i8086::DX(), i8086::SI(), i8086::DI(), i8086::BP(), i8086::SP());
959 | fprintf(file, "O D I T S Z A P C\n");
960 | fprintf(file, "%d %d %d %d %d %d %d %d %d\n\n", i8086::flagOF(), i8086::flagDF(), i8086::flagIF(), i8086::flagTF(), i8086::flagSF(), i8086::flagZF(), i8086::flagAF(), i8086::flagPF(), i8086::flagCF());
961 | fprintf(file, "CS+IP: %05X\n", i8086::CS() * 16 + i8086::IP());
962 | fprintf(file, "SS+SP: %05X\n\n", i8086::SS() * 16 + i8086::SP());
963 | fclose(file);
964 | }
965 | }
966 |
--------------------------------------------------------------------------------
/machine.h:
--------------------------------------------------------------------------------
1 | /*
2 | Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) -
3 | Copyright (c) 2019-2022 Fabrizio Di Vittorio.
4 | All rights reserved.
5 |
6 |
7 | * Please contact fdivitto2013@gmail.com if you need a commercial license.
8 |
9 |
10 | * This library and related software is available under GPL v3.
11 |
12 | FabGL is free software: you can redistribute it and/or modify
13 | it under the terms of the GNU General Public License as published by
14 | the Free Software Foundation, either version 3 of the License, or
15 | (at your option) any later version.
16 |
17 | FabGL is distributed in the hope that it will be useful,
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 | GNU General Public License for more details.
21 |
22 | You should have received a copy of the GNU General Public License
23 | along with FabGL. If not, see .
24 | */
25 |
26 |
27 | #pragma once
28 |
29 |
30 | #include "fabgl.h"
31 | #include "emudevs/graphicsadapter.h"
32 | #include "emudevs/i8086.h"
33 | #include "emudevs/PIC8259.h"
34 | #include "emudevs/PIT8253.h"
35 | #include "emudevs/i8042.h"
36 | #include "emudevs/MC146818.h"
37 | #include "devdrivers/MCP23S17.h"
38 |
39 | #include "bios.h"
40 |
41 |
42 | #define RAM_SIZE 1048576 // must correspond to bios MEMSIZE
43 | #define VIDEOMEMSIZE 65536
44 |
45 |
46 |
47 | using fabgl::GraphicsAdapter;
48 | using fabgl::PIC8259;
49 | using fabgl::PIT8253;
50 | using fabgl::i8042;
51 | using fabgl::MC146818;
52 | using fabgl::MCP23S17;
53 |
54 |
55 | #ifdef FABGL_EMULATED
56 | typedef void (*StepCallback)(void *);
57 | #endif
58 |
59 |
60 | typedef void (*SysReqCallback)();
61 |
62 |
63 |
64 | class Machine {
65 |
66 | public:
67 | Machine();
68 | ~Machine();
69 |
70 | void setBaseDirectory(char const * value) { m_baseDir = value; }
71 |
72 | void setDriveImage(int drive, char const * filename, int cylinders = 0, int heads = 0, int sectors = 0);
73 |
74 | void setBootDrive(int drive) { m_bootDrive = drive; }
75 |
76 | void setSysReqCallback(SysReqCallback value) { m_sysReqCallback = value; }
77 |
78 | void run();
79 |
80 | void trigReset() { m_reset = true; }
81 |
82 | uint32_t ticksCounter() { return m_ticksCounter; }
83 |
84 | i8042 * getI8042() { return &m_i8042; }
85 |
86 | MC146818 * getMC146818() { return &m_MC146818; }
87 |
88 | uint8_t * memory() { return s_memory; }
89 |
90 | uint8_t * videoMemory() { return s_videoMemory; }
91 |
92 | uint8_t * frameBuffer() { return m_frameBuffer; }
93 |
94 | GraphicsAdapter * graphicsAdapter() { return &m_graphicsAdapter; }
95 |
96 | FILE * disk(int index) { return m_disk[index]; }
97 | char const * diskFilename(int index) { return m_diskFilename[index]; }
98 | uint64_t diskSize(int index) { return m_diskSize[index]; }
99 | uint16_t diskCylinders(int index) { return m_diskCylinders[index]; }
100 | uint8_t diskHeads(int index) { return m_diskHeads[index]; }
101 | uint8_t diskSectors(int index) { return m_diskSectors[index]; }
102 |
103 | void dumpMemory(char const * filename);
104 | void dumpInfo(char const * filename);
105 |
106 | #ifdef FABGL_EMULATED
107 | void setStepCallback(StepCallback value) { m_stepCallback = value; }
108 | #endif
109 |
110 | private:
111 |
112 |
113 | static void runTask(void * pvParameters);
114 |
115 | void init();
116 | void reset();
117 |
118 | void tick();
119 |
120 | void setCGAMode();
121 | void setCGA6845Register(uint8_t value);
122 |
123 | void setHGCMode();
124 | void setHGC6845Register(uint8_t value);
125 |
126 | static void writePort(void * context, int address, uint8_t value);
127 | static uint8_t readPort(void * context, int address);
128 |
129 | static void writeVideoMemory8(void * context, int address, uint8_t value);
130 | static void writeVideoMemory16(void * context, int address, uint16_t value);
131 | static uint8_t readVideoMemory8(void * context, int address);
132 | static uint16_t readVideoMemory16(void * context, int address);
133 |
134 | static bool interrupt(void * context, int num);
135 |
136 | static void PITChangeOut(void * context, int timerIndex);
137 |
138 | static bool MC146818Interrupt(void * context);
139 |
140 | static bool keyboardInterrupt(void * context);
141 | static bool mouseInterrupt(void * context);
142 | static bool resetMachine(void * context);
143 | static bool sysReq(void * context);
144 |
145 | void speakerSetFreq();
146 | void speakerEnableDisable();
147 |
148 | void autoDetectDriveGeometry(int drive);
149 |
150 |
151 | #ifdef FABGL_EMULATED
152 | StepCallback m_stepCallback;
153 | #endif
154 |
155 | bool m_reset;
156 |
157 | GraphicsAdapter m_graphicsAdapter;
158 |
159 | BIOS m_BIOS;
160 |
161 | // 0, 1 = floppy
162 | // >= 2 = hard disk
163 | char * m_diskFilename[DISKCOUNT];
164 | FILE * m_disk[DISKCOUNT];
165 | uint64_t m_diskSize[DISKCOUNT];
166 | uint16_t m_diskCylinders[DISKCOUNT];
167 | uint8_t m_diskHeads[DISKCOUNT];
168 | uint8_t m_diskSectors[DISKCOUNT];
169 |
170 | static uint8_t * s_memory;
171 | static uint8_t * s_videoMemory;
172 | uint8_t * m_frameBuffer;
173 |
174 | // 8259 Programmable Interrupt Controllers
175 | PIC8259 m_PIC8259A; // master
176 | PIC8259 m_PIC8259B; // slave
177 |
178 | // 8253 Programmable Interval Timers
179 | // pin connections of PIT8253 on the IBM XT:
180 | // gate-0 = gate-1 = +5V
181 | // gate-2 = TIM2GATESPK
182 | // clk-0 = clk-1 = clk-2 = 1193182 Hz
183 | // out-0 = IRQ0
184 | // out-1 = RAM refresh
185 | // out-2 = speaker
186 | PIT8253 m_PIT8253;
187 |
188 | // 8042 PS/2 Keyboard Controller
189 | i8042 m_i8042;
190 |
191 | TaskHandle_t m_taskHandle;
192 |
193 | uint32_t m_ticksCounter;
194 |
195 | // CGA
196 | uint8_t m_CGA6845SelectRegister;
197 | uint8_t m_CGA6845[18];
198 | uint16_t m_CGAMemoryOffset;
199 | uint8_t m_CGAModeReg;
200 | uint8_t m_CGAColorReg;
201 | uint16_t m_CGAVSyncQuery;
202 |
203 | // Hercules
204 | uint8_t m_HGC6845SelectRegister;
205 | uint8_t m_HGC6845[18];
206 | uint16_t m_HGCMemoryOffset;
207 | uint8_t m_HGCModeReg;
208 | uint8_t m_HGCSwitchReg;
209 | uint16_t m_HGCVSyncQuery;
210 |
211 | // speaker/audio
212 | bool m_speakerDataEnable;
213 | SoundGenerator m_soundGen;
214 | SineWaveformGenerator m_sinWaveGen;
215 |
216 | // CMOS & RTC
217 | MC146818 m_MC146818;
218 |
219 | // extended I/O (MCP23S17)
220 | MCP23S17 m_MCP23S17;
221 | uint8_t m_MCP23S17Sel;
222 |
223 | uint8_t m_bootDrive;
224 |
225 | SysReqCallback m_sysReqCallback;
226 |
227 | char const * m_baseDir;
228 |
229 | };
230 |
--------------------------------------------------------------------------------
/mconf.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) -
3 | Copyright (c) 2019-2022 Fabrizio Di Vittorio.
4 | All rights reserved.
5 |
6 |
7 | * Please contact fdivitto2013@gmail.com if you need a commercial license.
8 |
9 |
10 | * This library and related software is available under GPL v3.
11 |
12 | FabGL is free software: you can redistribute it and/or modify
13 | it under the terms of the GNU General Public License as published by
14 | the Free Software Foundation, either version 3 of the License, or
15 | (at your option) any later version.
16 |
17 | FabGL is distributed in the hope that it will be useful,
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 | GNU General Public License for more details.
21 |
22 | You should have received a copy of the GNU General Public License
23 | along with FabGL. If not, see .
24 | */
25 |
26 |
27 | #include
28 |
29 | #include "fabgl.h"
30 | #include "fabui.h"
31 | #include "fabutils.h"
32 |
33 |
34 | #include "mconf.h"
35 |
36 |
37 | using std::unique_ptr;
38 | using fabgl::imin;
39 |
40 |
41 |
42 |
43 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////
44 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////
45 | // MachineConf
46 |
47 |
48 | #define MAXTAGLENGTH 6
49 | #define MAXVALUELENGTH 256
50 |
51 |
52 | MachineConf::MachineConf()
53 | : m_itemsList(nullptr)
54 | {
55 | }
56 |
57 |
58 | MachineConf::~MachineConf()
59 | {
60 | cleanUp();
61 | }
62 |
63 |
64 | void MachineConf::cleanUp()
65 | {
66 | while (deleteItem(0))
67 | ;
68 | }
69 |
70 |
71 | bool MachineConf::deleteItem(int index)
72 | {
73 | MachineConfItem * item = m_itemsList;
74 | MachineConfItem * prev = nullptr;
75 | while (item && --index > -1) {
76 | prev = item;
77 | item = item->next;
78 | }
79 | if (item) {
80 | if (prev)
81 | prev->next = item->next;
82 | else
83 | m_itemsList = item->next;
84 | delete item;
85 | }
86 | return item != nullptr;
87 | }
88 |
89 |
90 | MachineConfItem * MachineConf::getItem(int index)
91 | {
92 | auto item = m_itemsList;
93 | while (item && --index > -1)
94 | item = item->next;
95 | return item;
96 | }
97 |
98 |
99 | void MachineConf::addItem(MachineConfItem * item)
100 | {
101 | if (item->isValid()) {
102 | if (m_itemsList) {
103 | auto prev = m_itemsList;
104 | while (prev->next)
105 | prev = prev->next;
106 | prev->next = item;
107 | } else
108 | m_itemsList = item;
109 | } else
110 | delete item;
111 | }
112 |
113 |
114 | // insertingPoint = -1, append
115 | // insertingPoint = 0, insert before first
116 | // insertingPoint > 0, insert before insertingPoint
117 | void MachineConf::insertItem(int insertingPoint, MachineConfItem * item)
118 | {
119 | if (insertingPoint == 0) {
120 | item->next = m_itemsList;
121 | m_itemsList = item;
122 | } else if (insertingPoint < 0) {
123 | addItem(item);
124 | } else {
125 | auto prev = getItem(insertingPoint - 1);
126 | item->next = prev->next;
127 | prev->next = item;
128 | }
129 | }
130 |
131 |
132 | void MachineConf::loadFromFile(FILE * file)
133 | {
134 | cleanUp();
135 | auto item = new MachineConfItem;
136 | char * value = (char*) SOC_EXTRAM_DATA_LOW; // use PSRAM as buffer
137 | char * tag = (char*) SOC_EXTRAM_DATA_LOW + MAXVALUELENGTH + 1;
138 | int ipos;
139 | char c = fgetc(file);
140 | while (!feof(file)) {
141 | while (!feof(file) && (isspace(c) || iscntrl(c))) {
142 | if (c == 0x0a) {
143 | // new line
144 | addItem(item);
145 | item = new MachineConfItem;
146 | }
147 | c = fgetc(file);
148 | }
149 | // get tag
150 | ipos = 0;
151 | while (!feof(file) && !isspace(c)) {
152 | if (ipos < MAXTAGLENGTH)
153 | tag[ipos++] = c;
154 | c = fgetc(file);
155 | }
156 | tag[ipos] = 0;
157 | // bypass spaces
158 | while (!feof(file) && isspace(c))
159 | c = fgetc(file);
160 | // get tag value
161 | ipos = 0;
162 | bool quote = false;
163 | while (!feof(file) && (quote || !isspace(c))) {
164 | if (c == '"')
165 | quote = !quote;
166 | else if (ipos < MAXVALUELENGTH)
167 | value[ipos++] = c;
168 | c = fgetc(file);
169 | }
170 | value[ipos] = 0;
171 | // assign tag
172 | if (strcmp("desc", tag) == 0)
173 | item->setDesc(value);
174 | else if (strcmp("dska", tag) == 0 || strcmp("fd0", tag) == 0)
175 | item->setDisk(0, value);
176 | else if (strcmp("dskb", tag) == 0 || strcmp("fd1", tag) == 0)
177 | item->setDisk(1, value);
178 | else if (strcmp("dskc", tag) == 0 || strcmp("hd0", tag) == 0)
179 | item->setDisk(2, value);
180 | else if (strcmp("dskd", tag) == 0 || strcmp("hd1", tag) == 0)
181 | item->setDisk(3, value);
182 | else if (strcmp("chs0", tag) == 0)
183 | item->setCHS(2, value);
184 | else if (strcmp("chs1", tag) == 0)
185 | item->setCHS(3, value);
186 | else if (strcmp("boot", tag) == 0)
187 | item->setBootDrive(value);
188 | }
189 | addItem(item);
190 | }
191 |
192 |
193 | void MachineConf::saveToFile(FILE * file)
194 | {
195 | for (auto item = m_itemsList; item; item = item->next) {
196 | fputs("desc \"", file);
197 | fputs(item->desc, file);
198 | fputs("\" ", file);
199 | for (int d = 0; d < DISKCOUNT; ++d)
200 | if (item->disk[d] && strlen(item->disk[d]) > 0) {
201 | fputs(MachineConfItem::driveIndexToStr(d), file);
202 | fputs(" \"", file);
203 | fputs(item->disk[d], file);
204 | fputs("\" ", file);
205 | }
206 | for (int hd = 2; hd < 4; ++hd)
207 | if (item->cylinders[hd] > 0 && item->heads[hd] > 0 && item->sectors[hd] > 0)
208 | fprintf(file, "chs%d %hu,%hu,%hu ", hd - 2, item->cylinders[hd], item->heads[hd], item->sectors[hd]);
209 | if (item->bootDrive > 0)
210 | fprintf(file, "boot %s ", MachineConfItem::driveIndexToStr(item->bootDrive));
211 | fputs("\r\n", file);
212 | }
213 | }
214 |
215 |
216 |
217 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////
218 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////
219 | // ConfigDialog
220 |
221 |
222 | struct ConfigDialog : public uiApp {
223 |
224 | MachineConf * mconf;
225 | MachineConfItem * item;
226 |
227 | RGB888 backgroundColor;
228 |
229 | uiFrame * mainFrame;
230 | uiButton * saveButton;
231 | uiButton * cancelButton;
232 | uiButton * createImageButton;
233 | uiTextEdit * descEdit;
234 | uiTextEdit * FD0PathEdit;
235 | uiTextEdit * FD1PathEdit;
236 | uiTextEdit * HD0PathEdit, * HD0CylEdit, * HD0HdsEdit, * HD0SecEdit;
237 | uiTextEdit * HD1PathEdit, * HD1CylEdit, * HD1HdsEdit, * HD1SecEdit;
238 | uiButton * browseFD0Button;
239 | uiButton * browseFD1Button;
240 | uiButton * browseHD0Button;
241 | uiButton * browseHD1Button;
242 | uiComboBox * bootDriveComboBox;
243 |
244 |
245 | void init() {
246 | rootWindow()->frameStyle().backgroundColor = backgroundColor;
247 |
248 | rootWindow()->onPaint = [&]() { drawInfo(canvas()); };
249 |
250 | mainFrame = new uiFrame(rootWindow(), "Machine Configuration", UIWINDOW_PARENTCENTER, Size(460, 250));
251 | mainFrame->frameProps().resizeable = false;
252 | mainFrame->frameProps().hasMaximizeButton = false;
253 | mainFrame->frameProps().hasMinimizeButton = false;
254 | mainFrame->frameProps().hasCloseButton = false;
255 | mainFrame->onKeyUp = [&](uiKeyEventInfo const & key) {
256 | if (key.VK == VirtualKey::VK_RETURN || key.VK == VirtualKey::VK_KP_ENTER) {
257 | saveAndQuit();
258 | } else if (key.VK == VirtualKey::VK_ESCAPE) {
259 | justQuit();
260 | }
261 | };
262 |
263 | int x = 6;
264 | int y = 24;
265 | constexpr int oy = 3;
266 | constexpr int hh = 20;
267 | constexpr int dy = hh + 9;
268 |
269 | // description
270 | new uiStaticLabel(mainFrame, "Description", Point(x, y + oy));
271 | descEdit = new uiTextEdit(mainFrame, item->desc, Point(70, y), Size(270, hh));
272 |
273 | y += dy;
274 |
275 | // floppy 0
276 | new uiStaticLabel(mainFrame, "Floppy 0", Point(x, y + oy));
277 | FD0PathEdit = new uiTextEdit(mainFrame, item->disk[0], Point(60, y), Size(260, hh));
278 | browseFD0Button = new uiButton(mainFrame, "...", Point(322, y), Size(20, hh));
279 | browseFD0Button->onClick = [&]() { browseFilename(FD0PathEdit); };
280 |
281 |
282 | y += dy;
283 |
284 | // floppy 1
285 | new uiStaticLabel(mainFrame, "Floppy 1", Point(x, y + oy));
286 | FD1PathEdit = new uiTextEdit(mainFrame, item->disk[1], Point(60, y), Size(260, hh));
287 | browseFD1Button = new uiButton(mainFrame, "...", Point(322, y), Size(20, hh));
288 | browseFD1Button->onClick = [&]() { browseFilename(FD1PathEdit); };
289 |
290 | y += dy;
291 |
292 | // HDD 0
293 | new uiStaticLabel(mainFrame, "HDD 0", Point(x, y + oy));
294 | HD0PathEdit = new uiTextEdit(mainFrame, item->disk[2], Point(60, y), Size(260, hh));
295 | browseHD0Button = new uiButton(mainFrame, "...", Point(322, y), Size(20, hh));
296 | browseHD0Button->onClick = [&]() { browseFilename(HD0PathEdit); };
297 | new uiStaticLabel(mainFrame, "Cyls", Point(350, y - 13));
298 | HD0CylEdit = new uiTextEdit(mainFrame, "", Point(350, y), Size(34, hh));
299 | HD0CylEdit->setTextFmt("%hu", item->cylinders[2]);
300 | new uiStaticLabel(mainFrame, "Head", Point(385, y - 13));
301 | HD0HdsEdit = new uiTextEdit(mainFrame, "", Point(385, y), Size(34, hh));
302 | HD0HdsEdit->setTextFmt("%hu", item->heads[2]);
303 | new uiStaticLabel(mainFrame, "Sect", Point(420, y - 13));
304 | HD0SecEdit = new uiTextEdit(mainFrame, "", Point(420, y), Size(34, hh));
305 | HD0SecEdit->setTextFmt("%hu", item->sectors[2]);
306 |
307 | y += dy;
308 |
309 | // HDD 1
310 | new uiStaticLabel(mainFrame, "HDD 1", Point(x, y + oy));
311 | HD1PathEdit = new uiTextEdit(mainFrame, item->disk[3], Point(60, y), Size(260, hh));
312 | browseHD1Button = new uiButton(mainFrame, "...", Point(322, y), Size(20, hh));
313 | browseHD1Button->onClick = [&]() { browseFilename(HD1PathEdit); };
314 | HD1CylEdit = new uiTextEdit(mainFrame, "", Point(350, y), Size(34, hh));
315 | HD1CylEdit->setTextFmt("%hu", item->cylinders[3]);
316 | HD1HdsEdit = new uiTextEdit(mainFrame, "", Point(385, y), Size(34, hh));
317 | HD1HdsEdit->setTextFmt("%hu", item->heads[3]);
318 | HD1SecEdit = new uiTextEdit(mainFrame, "", Point(420, y), Size(34, hh));
319 | HD1SecEdit->setTextFmt("%hu", item->sectors[3]);
320 |
321 | y += dy;
322 |
323 | // boot drive selection
324 | new uiStaticLabel(mainFrame, "Boot Drive", Point(x, y + oy));
325 | bootDriveComboBox = new uiComboBox(mainFrame, Point(60, y), Size(60, hh), 50);
326 | bootDriveComboBox->items().append("Floppy 0");
327 | bootDriveComboBox->items().append("Floppy 1");
328 | bootDriveComboBox->items().append("HDD 0");
329 | bootDriveComboBox->items().append("HDD 1");
330 | bootDriveComboBox->selectItem(item->bootDrive);
331 |
332 | // bottom buttons
333 |
334 | y = mainFrame->clientSize().height - 8;
335 |
336 | // Save Button
337 | saveButton = new uiButton(mainFrame, "Save", Point(mainFrame->clientSize().width - 75, y), Size(70, hh));
338 | saveButton->onClick = [&]() {
339 | saveAndQuit();
340 | };
341 |
342 | // Cancel Button
343 | cancelButton = new uiButton(mainFrame, "Cancel", Point(mainFrame->clientSize().width - 155, y), Size(70, hh));
344 | cancelButton->onClick = [&]() {
345 | justQuit();
346 | };
347 |
348 | // Create Disk Image Button
349 | createImageButton = new uiButton(mainFrame, "Create Disk", Point(10, y), Size(90, hh));
350 | createImageButton->onClick = [&]() {
351 | createDiskImage();
352 | };
353 |
354 | setActiveWindow(mainFrame);
355 | }
356 |
357 |
358 | void browseFilename(uiTextEdit * edit) {
359 | unique_ptr dir(new char[MAXVALUELENGTH + 1] { '/', 'S', 'D', 0 } );
360 | unique_ptr filename(new char[MAXVALUELENGTH + 1]);
361 | strcpy(filename.get(), edit->text());
362 | // is URL?
363 | if (strncmp("://", filename.get() + 4, 3)) {
364 | // no, does filename contain a path?
365 | auto p = strrchr(filename.get(), '/');
366 | if (p) {
367 | // yes, move it into "dir"
368 | strcat(dir.get(), "/");
369 | strncat(dir.get(), filename.get(), p - filename.get());
370 | memmove(filename.get(), p + 1, strlen(p + 1) + 1);
371 | }
372 | }
373 | if (fileDialog("Select drive image", dir.get(), MAXVALUELENGTH, filename.get(), MAXVALUELENGTH, "OK", "Cancel") == uiMessageBoxResult::ButtonOK) {
374 | if (strlen(dir.get()) > 4)
375 | edit->setTextFmt("%s/%s", dir.get() + 4, filename.get()); // bypass "/SD/"
376 | else
377 | edit->setText(filename.get());
378 | edit->repaint();
379 | }
380 | }
381 |
382 |
383 | void saveAndQuit() {
384 | // get fields
385 | item->setDesc(descEdit->text());
386 | item->setDisk(0, FD0PathEdit->text());
387 | item->setDisk(1, FD1PathEdit->text());
388 | item->setDisk(2, HD0PathEdit->text());
389 | item->setDisk(3, HD1PathEdit->text());
390 | item->cylinders[2] = atoi(HD0CylEdit->text());
391 | item->heads[2] = atoi(HD0HdsEdit->text());
392 | item->sectors[2] = atoi(HD0SecEdit->text());
393 | item->cylinders[3] = atoi(HD1CylEdit->text());
394 | item->heads[3] = atoi(HD1HdsEdit->text());
395 | item->sectors[3] = atoi(HD1SecEdit->text());
396 | item->bootDrive = bootDriveComboBox->selectedItem();
397 |
398 | // save to file
399 | saveMachineConfiguration(mconf);
400 |
401 | justQuit();
402 | }
403 |
404 |
405 | void justQuit() {
406 | rootWindow()->frameProps().fillBackground = false;
407 | quit(0);
408 | }
409 |
410 |
411 | void createDiskImage() {
412 | InputBox ib(this);
413 |
414 | int s = ib.menu("Create Disk Image", "Select Disk Size", "Floppy 320K (FAT12);" // 0
415 | "Floppy 360K (FAT12);" // 1
416 | "Floppy 720K (FAT12);" // 2
417 | "Floppy 1.2M (FAT12);" // 3
418 | "Floppy 1.44M (FAT12);" // 4
419 | "Floppy 2.88M (FAT12);" // 5
420 | "Hard Disk (Unformatted)"); // 6
421 | int hdSize = 0;
422 | if (s == 6) {
423 | // set hard disk size
424 | char str[4] = "10";
425 | if (ib.textInput("Hard Disk Size", "Specify Hard Disk size in Megabytes", str, 3) == InputResult::Enter)
426 | hdSize = atoi(str);
427 | else
428 | return;
429 | if (hdSize <= 0 || hdSize > 512) {
430 | ib.message("Error", "Invalid Hard Disk Size!");
431 | return;
432 | }
433 | } else if (s < 0 || s > 6)
434 | return;
435 |
436 | // set filename
437 | unique_ptr dir(new char[MAXVALUELENGTH + 1] { '/', 'S', 'D', 0 } );
438 | unique_ptr filename(new char[MAXVALUELENGTH + 1] { 'n', 'e', 'w', 'i', 'm', 'a', 'g', 'e', '.', 'i', 'm', 'g', 0 });
439 | if (fileDialog("Image Filename", dir.get(), MAXVALUELENGTH, filename.get(), MAXVALUELENGTH, "OK", "Cancel") != uiMessageBoxResult::ButtonOK)
440 | return;
441 |
442 | // create the image
443 | if (s == 6) {
444 | // Hard Disk
445 | ib.progressBox("", "Abort", true, 380, [&](fabgl::ProgressForm * form) {
446 | auto buf = (uint8_t*) SOC_EXTRAM_DATA_LOW; // use PSRAM as buffer
447 | constexpr int BUFSIZE = 4096;
448 | memset(buf, 0, BUFSIZE);
449 | auto file = FileBrowser(dir.get()).openFile(filename.get(), "wb");
450 | const int totSize = hdSize * 1048576;
451 | for (int sz = totSize; sz > 0; ) {
452 | sz -= fwrite(buf, 1, imin(BUFSIZE, sz), file);
453 | if (!form->update((int)((double)(totSize - sz) / totSize * 100), "Writing %s (%d / %d bytes)", filename.get(), (totSize - sz), totSize))
454 | break;
455 | }
456 | fclose(file);
457 | });
458 | } else {
459 | // FAT Formatted Floppy Disk
460 | createFATFloppyImage(&ib, s, dir.get(), filename.get());
461 | }
462 |
463 | }
464 |
465 | };
466 |
467 |
468 |
469 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////
470 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////
471 | // helpers
472 |
473 |
474 | void loadMachineConfiguration(MachineConf * mconf)
475 | {
476 | FileBrowser fb(SD_MOUNT_PATH);
477 |
478 | // saves a default configuration file if necessary
479 | if (!fb.exists(MACHINE_CONF_FILENAME, false)) {
480 | auto confFile = fb.openFile(MACHINE_CONF_FILENAME, "wb");
481 | fwrite(DefaultConfFile, 1, sizeof(DefaultConfFile), confFile);
482 | fclose(confFile);
483 | }
484 |
485 | // load
486 | auto confFile = fb.openFile(MACHINE_CONF_FILENAME, "rb");
487 | mconf->loadFromFile(confFile);
488 | fclose(confFile);
489 | }
490 |
491 |
492 | void saveMachineConfiguration(MachineConf * mconf)
493 | {
494 | auto confFile = FileBrowser(SD_MOUNT_PATH).openFile(MACHINE_CONF_FILENAME, "wb");
495 | mconf->saveToFile(confFile);
496 | fclose(confFile);
497 | }
498 |
499 |
500 | void editConfigDialog(InputBox * ibox, MachineConf * mconf, int idx)
501 | {
502 | ConfigDialog app;
503 | app.mconf = mconf;
504 | app.item = mconf->getItem(idx);
505 | app.backgroundColor = ibox->backgroundColor();
506 | app.run(ibox->getDisplayController());
507 | }
508 |
509 |
510 | void newConfigDialog(InputBox * ibox, MachineConf * mconf, int idx)
511 | {
512 | auto newItem = new MachineConfItem;
513 | newItem->setDesc("New Configuration");
514 | mconf->insertItem(idx, newItem);
515 |
516 | ConfigDialog app;
517 | app.mconf = mconf;
518 | app.item = newItem;
519 | app.backgroundColor = ibox->backgroundColor();
520 | app.run(ibox->getDisplayController());
521 | }
522 |
523 |
524 | void delConfigDialog(InputBox * ibox, MachineConf * mconf, int idx)
525 | {
526 | if (idx > -1 && ibox->message("Please confirm", "Remove Configuration?", "No", "Yes") == InputResult::Enter) {
527 | mconf->deleteItem(idx);
528 | saveMachineConfiguration(mconf);
529 | }
530 | }
531 |
532 |
533 | void drawInfo(Canvas * canvas)
534 | {
535 | canvas->setPenColor(RGB888(0, 255, 0));
536 | canvas->drawText(120, 5, "E S P 3 2 P C E M U L A T O R");
537 | canvas->drawText(93, 25, "www.fabgl.com - by Fabrizio Di Vittorio");
538 | }
539 |
540 |
541 | // Create FAT formatted floppy image
542 | // diskType:
543 | // 0 = 320
544 | // 1 = 360
545 | // 2 = 720
546 | // 3 = 1200
547 | // 4 = 1440
548 | // 5 = 2880
549 | // directory: absolute path (includes mounting point, ie "/SD/..."
550 | bool createFATFloppyImage(InputBox * ibox, int diskType, char const * directory, char const * filename)
551 | {
552 | // boot sector for: 320K, 360K, 720K, 1440K, 2880K
553 | static const uint8_t BOOTSECTOR_WIN[512] = {
554 | 0xeb, 0x3c, 0x90, 0x4d, 0x53, 0x57, 0x49, 0x4e, 0x34, 0x2e, 0x31, 0x00, 0x02, 0x01, 0x01, 0x00, 0x02, 0xe0, 0x00, 0x40, 0x0b, 0xf0, 0x09, 0x00,
555 | 0x12, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x20, 0x20, 0x20,
556 | 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x46, 0x41, 0x54, 0x31, 0x32, 0x20, 0x20, 0x20, 0x33, 0xc9, 0x8e, 0xd1, 0xbc, 0xfc, 0x7b, 0x16, 0x07, 0xbd,
557 | 0x78, 0x00, 0xc5, 0x76, 0x00, 0x1e, 0x56, 0x16, 0x55, 0xbf, 0x22, 0x05, 0x89, 0x7e, 0x00, 0x89, 0x4e, 0x02, 0xb1, 0x0b, 0xfc, 0xf3, 0xa4, 0x06,
558 | 0x1f, 0xbd, 0x00, 0x7c, 0xc6, 0x45, 0xfe, 0x0f, 0x38, 0x4e, 0x24, 0x7d, 0x20, 0x8b, 0xc1, 0x99, 0xe8, 0x7e, 0x01, 0x83, 0xeb, 0x3a, 0x66, 0xa1,
559 | 0x1c, 0x7c, 0x66, 0x3b, 0x07, 0x8a, 0x57, 0xfc, 0x75, 0x06, 0x80, 0xca, 0x02, 0x88, 0x56, 0x02, 0x80, 0xc3, 0x10, 0x73, 0xed, 0x33, 0xc9, 0xfe,
560 | 0x06, 0xd8, 0x7d, 0x8a, 0x46, 0x10, 0x98, 0xf7, 0x66, 0x16, 0x03, 0x46, 0x1c, 0x13, 0x56, 0x1e, 0x03, 0x46, 0x0e, 0x13, 0xd1, 0x8b, 0x76, 0x11,
561 | 0x60, 0x89, 0x46, 0xfc, 0x89, 0x56, 0xfe, 0xb8, 0x20, 0x00, 0xf7, 0xe6, 0x8b, 0x5e, 0x0b, 0x03, 0xc3, 0x48, 0xf7, 0xf3, 0x01, 0x46, 0xfc, 0x11,
562 | 0x4e, 0xfe, 0x61, 0xbf, 0x00, 0x07, 0xe8, 0x28, 0x01, 0x72, 0x3e, 0x38, 0x2d, 0x74, 0x17, 0x60, 0xb1, 0x0b, 0xbe, 0xd8, 0x7d, 0xf3, 0xa6, 0x61,
563 | 0x74, 0x3d, 0x4e, 0x74, 0x09, 0x83, 0xc7, 0x20, 0x3b, 0xfb, 0x72, 0xe7, 0xeb, 0xdd, 0xfe, 0x0e, 0xd8, 0x7d, 0x7b, 0xa7, 0xbe, 0x7f, 0x7d, 0xac,
564 | 0x98, 0x03, 0xf0, 0xac, 0x98, 0x40, 0x74, 0x0c, 0x48, 0x74, 0x13, 0xb4, 0x0e, 0xbb, 0x07, 0x00, 0xcd, 0x10, 0xeb, 0xef, 0xbe, 0x82, 0x7d, 0xeb,
565 | 0xe6, 0xbe, 0x80, 0x7d, 0xeb, 0xe1, 0xcd, 0x16, 0x5e, 0x1f, 0x66, 0x8f, 0x04, 0xcd, 0x19, 0xbe, 0x81, 0x7d, 0x8b, 0x7d, 0x1a, 0x8d, 0x45, 0xfe,
566 | 0x8a, 0x4e, 0x0d, 0xf7, 0xe1, 0x03, 0x46, 0xfc, 0x13, 0x56, 0xfe, 0xb1, 0x04, 0xe8, 0xc2, 0x00, 0x72, 0xd7, 0xea, 0x00, 0x02, 0x70, 0x00, 0x52,
567 | 0x50, 0x06, 0x53, 0x6a, 0x01, 0x6a, 0x10, 0x91, 0x8b, 0x46, 0x18, 0xa2, 0x26, 0x05, 0x96, 0x92, 0x33, 0xd2, 0xf7, 0xf6, 0x91, 0xf7, 0xf6, 0x42,
568 | 0x87, 0xca, 0xf7, 0x76, 0x1a, 0x8a, 0xf2, 0x8a, 0xe8, 0xc0, 0xcc, 0x02, 0x0a, 0xcc, 0xb8, 0x01, 0x02, 0x80, 0x7e, 0x02, 0x0e, 0x75, 0x04, 0xb4,
569 | 0x42, 0x8b, 0xf4, 0x8a, 0x56, 0x24, 0xcd, 0x13, 0x61, 0x61, 0x72, 0x0a, 0x40, 0x75, 0x01, 0x42, 0x03, 0x5e, 0x0b, 0x49, 0x75, 0x77, 0xc3, 0x03,
570 | 0x18, 0x01, 0x27, 0x0d, 0x0a, 0x49, 0x6e, 0x76, 0x61, 0x6c, 0x69, 0x64, 0x20, 0x73, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x20, 0x64, 0x69, 0x73, 0x6b,
571 | 0xff, 0x0d, 0x0a, 0x44, 0x69, 0x73, 0x6b, 0x20, 0x49, 0x2f, 0x4f, 0x20, 0x65, 0x72, 0x72, 0x6f, 0x72, 0xff, 0x0d, 0x0a, 0x52, 0x65, 0x70, 0x6c,
572 | 0x61, 0x63, 0x65, 0x20, 0x74, 0x68, 0x65, 0x20, 0x64, 0x69, 0x73, 0x6b, 0x2c, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x74, 0x68, 0x65, 0x6e, 0x20, 0x70,
573 | 0x72, 0x65, 0x73, 0x73, 0x20, 0x61, 0x6e, 0x79, 0x20, 0x6b, 0x65, 0x79, 0x0d, 0x0a, 0x00, 0x00, 0x49, 0x4f, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
574 | 0x53, 0x59, 0x53, 0x4d, 0x53, 0x44, 0x4f, 0x53, 0x20, 0x20, 0x20, 0x53, 0x59, 0x53, 0x7f, 0x01, 0x00, 0x41, 0xbb, 0x00, 0x07, 0x60, 0x66, 0x6a,
575 | 0x00, 0xe9, 0x3b, 0xff, 0x00, 0x00, 0x55, 0xaa };
576 |
577 | // boot sector for: 1200K
578 | static const uint8_t BOOTSECTOR_MSDOS5[512] = {
579 | 0xeb, 0x3c, 0x90, 0x4d, 0x53, 0x44, 0x4f, 0x53, 0x35, 0x2e, 0x30, 0x00, 0x02, 0x01, 0x01, 0x00, 0x02, 0xe0, 0x00, 0x60, 0x09, 0xf9, 0x08, 0x00,
580 | 0x0f, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x29, 0xd1, 0x40, 0x38, 0xda, 0x20, 0x20, 0x20, 0x20, 0x20,
581 | 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x46, 0x41, 0x54, 0x31, 0x32, 0x20, 0x20, 0x20, 0xfa, 0x33, 0xc0, 0x8e, 0xd0, 0xbc, 0x00, 0x7c, 0x16, 0x07,
582 | 0xbb, 0x78, 0x00, 0x36, 0xc5, 0x37, 0x1e, 0x56, 0x16, 0x53, 0xbf, 0x3e, 0x7c, 0xb9, 0x0b, 0x00, 0xfc, 0xf3, 0xa4, 0x06, 0x1f, 0xc6, 0x45, 0xfe,
583 | 0x0f, 0x8b, 0x0e, 0x18, 0x7c, 0x88, 0x4d, 0xf9, 0x89, 0x47, 0x02, 0xc7, 0x07, 0x3e, 0x7c, 0xfb, 0xcd, 0x13, 0x72, 0x79, 0x33, 0xc0, 0x39, 0x06,
584 | 0x13, 0x7c, 0x74, 0x08, 0x8b, 0x0e, 0x13, 0x7c, 0x89, 0x0e, 0x20, 0x7c, 0xa0, 0x10, 0x7c, 0xf7, 0x26, 0x16, 0x7c, 0x03, 0x06, 0x1c, 0x7c, 0x13,
585 | 0x16, 0x1e, 0x7c, 0x03, 0x06, 0x0e, 0x7c, 0x83, 0xd2, 0x00, 0xa3, 0x50, 0x7c, 0x89, 0x16, 0x52, 0x7c, 0xa3, 0x49, 0x7c, 0x89, 0x16, 0x4b, 0x7c,
586 | 0xb8, 0x20, 0x00, 0xf7, 0x26, 0x11, 0x7c, 0x8b, 0x1e, 0x0b, 0x7c, 0x03, 0xc3, 0x48, 0xf7, 0xf3, 0x01, 0x06, 0x49, 0x7c, 0x83, 0x16, 0x4b, 0x7c,
587 | 0x00, 0xbb, 0x00, 0x05, 0x8b, 0x16, 0x52, 0x7c, 0xa1, 0x50, 0x7c, 0xe8, 0x92, 0x00, 0x72, 0x1d, 0xb0, 0x01, 0xe8, 0xac, 0x00, 0x72, 0x16, 0x8b,
588 | 0xfb, 0xb9, 0x0b, 0x00, 0xbe, 0xe6, 0x7d, 0xf3, 0xa6, 0x75, 0x0a, 0x8d, 0x7f, 0x20, 0xb9, 0x0b, 0x00, 0xf3, 0xa6, 0x74, 0x18, 0xbe, 0x9e, 0x7d,
589 | 0xe8, 0x5f, 0x00, 0x33, 0xc0, 0xcd, 0x16, 0x5e, 0x1f, 0x8f, 0x04, 0x8f, 0x44, 0x02, 0xcd, 0x19, 0x58, 0x58, 0x58, 0xeb, 0xe8, 0x8b, 0x47, 0x1a,
590 | 0x48, 0x48, 0x8a, 0x1e, 0x0d, 0x7c, 0x32, 0xff, 0xf7, 0xe3, 0x03, 0x06, 0x49, 0x7c, 0x13, 0x16, 0x4b, 0x7c, 0xbb, 0x00, 0x07, 0xb9, 0x03, 0x00,
591 | 0x50, 0x52, 0x51, 0xe8, 0x3a, 0x00, 0x72, 0xd8, 0xb0, 0x01, 0xe8, 0x54, 0x00, 0x59, 0x5a, 0x58, 0x72, 0xbb, 0x05, 0x01, 0x00, 0x83, 0xd2, 0x00,
592 | 0x03, 0x1e, 0x0b, 0x7c, 0xe2, 0xe2, 0x8a, 0x2e, 0x15, 0x7c, 0x8a, 0x16, 0x24, 0x7c, 0x8b, 0x1e, 0x49, 0x7c, 0xa1, 0x4b, 0x7c, 0xea, 0x00, 0x00,
593 | 0x70, 0x00, 0xac, 0x0a, 0xc0, 0x74, 0x29, 0xb4, 0x0e, 0xbb, 0x07, 0x00, 0xcd, 0x10, 0xeb, 0xf2, 0x3b, 0x16, 0x18, 0x7c, 0x73, 0x19, 0xf7, 0x36,
594 | 0x18, 0x7c, 0xfe, 0xc2, 0x88, 0x16, 0x4f, 0x7c, 0x33, 0xd2, 0xf7, 0x36, 0x1a, 0x7c, 0x88, 0x16, 0x25, 0x7c, 0xa3, 0x4d, 0x7c, 0xf8, 0xc3, 0xf9,
595 | 0xc3, 0xb4, 0x02, 0x8b, 0x16, 0x4d, 0x7c, 0xb1, 0x06, 0xd2, 0xe6, 0x0a, 0x36, 0x4f, 0x7c, 0x8b, 0xca, 0x86, 0xe9, 0x8a, 0x16, 0x24, 0x7c, 0x8a,
596 | 0x36, 0x25, 0x7c, 0xcd, 0x13, 0xc3, 0x0d, 0x0a, 0x4e, 0x6f, 0x6e, 0x2d, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x20, 0x64, 0x69, 0x73, 0x6b, 0x20,
597 | 0x6f, 0x72, 0x20, 0x64, 0x69, 0x73, 0x6b, 0x20, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x0d, 0x0a, 0x52, 0x65, 0x70, 0x6c, 0x61, 0x63, 0x65, 0x20, 0x61,
598 | 0x6e, 0x64, 0x20, 0x70, 0x72, 0x65, 0x73, 0x73, 0x20, 0x61, 0x6e, 0x79, 0x20, 0x6b, 0x65, 0x79, 0x20, 0x77, 0x68, 0x65, 0x6e, 0x20, 0x72, 0x65,
599 | 0x61, 0x64, 0x79, 0x0d, 0x0a, 0x00, 0x49, 0x4f, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x53, 0x59, 0x53, 0x4d, 0x53, 0x44, 0x4f, 0x53, 0x20, 0x20,
600 | 0x20, 0x53, 0x59, 0x53, 0x00, 0x00, 0x55, 0xaa };
601 |
602 | static const uint8_t * BOOTSECTOR[6] = {
603 | BOOTSECTOR_WIN, // 320K
604 | BOOTSECTOR_WIN, // 360K
605 | BOOTSECTOR_WIN, // 720K
606 | BOOTSECTOR_MSDOS5, // 1200K
607 | BOOTSECTOR_WIN, // 1440K
608 | BOOTSECTOR_WIN, // 2880K
609 | };
610 |
611 | // offset 0x0c (16 bytes):
612 | static const uint8_t GEOM[6][16] = {
613 | { 0x02, 0x02, 0x01, 0x00, 0x02, 0x70, 0x00, 0x80, 0x02, 0xFF, 0x01, 0x00, 0x08, 0x00, 0x02, 0x00 }, // 320K
614 | { 0x02, 0x02, 0x01, 0x00, 0x02, 0x70, 0x00, 0xD0, 0x02, 0xFD, 0x02, 0x00, 0x09, 0x00, 0x02, 0x00 }, // 360K
615 | { 0x02, 0x02, 0x01, 0x00, 0x02, 0x70, 0x00, 0xA0, 0x05, 0xF9, 0x03, 0x00, 0x09, 0x00, 0x02, 0x00 }, // 720K
616 | { 0x02, 0x01, 0x01, 0x00, 0x02, 0xE0, 0x00, 0x60, 0x09, 0xF9, 0x08, 0x00, 0x0F, 0x00, 0x02, 0x00 }, // 1200K
617 | { 0x02, 0x01, 0x01, 0x00, 0x02, 0xE0, 0x00, 0x40, 0x0B, 0xF0, 0x09, 0x00, 0x12, 0x00, 0x02, 0x00 }, // 1440K
618 | { 0x02, 0x02, 0x01, 0x00, 0x02, 0xF0, 0x00, 0x80, 0x16, 0xF0, 0x09, 0x00, 0x24, 0x00, 0x02, 0x00 }, // 2880K
619 | };
620 |
621 | // sizes in bytes
622 | static const int SIZES[6] = {
623 | 512 * 8 * 40 * 2, // 327680 (320K)
624 | 512 * 9 * 40 * 2, // 368640 (360K)
625 | 512 * 9 * 80 * 2, // 737280 (720K)
626 | 512 * 15 * 80 * 2, // 1228800 (1200K)
627 | 512 * 18 * 80 * 2, // 1474560 (1440K)
628 | 512 * 36 * 80 * 2, // 2949120 (2880K)
629 | };
630 |
631 | // FAT
632 | static const struct {
633 | uint8_t id;
634 | uint8_t fat1size; // in 512 blocks
635 | uint8_t fat2size; // in 512 blocks
636 | } FAT[6] = {
637 | { 0xff, 1, 8 }, // 320K
638 | { 0xfd, 2, 9 }, // 360K
639 | { 0xf9, 3, 10 }, // 720K
640 | { 0xf9, 8, 22 }, // 1200K
641 | { 0xf0, 9, 23 }, // 1440K
642 | { 0xf0, 9, 24 }, // 2880K
643 | };
644 |
645 | auto file = FileBrowser(directory).openFile(filename, "wb");
646 |
647 | if (file) {
648 |
649 | auto sectbuf = (uint8_t*) SOC_EXTRAM_DATA_LOW; // use PSRAM as buffer
650 |
651 | // initial fill with all 0xf6 up to entire size
652 | memset(sectbuf, 0xf6, 512);
653 | fseek(file, 0, SEEK_SET);
654 |
655 | ibox->progressBox("", "Abort", true, 380, [&](fabgl::ProgressForm * form) {
656 | for (int i = 0; i < SIZES[diskType]; i += 512) {
657 | fwrite(sectbuf, 1, 512, file);
658 | if (!form->update((int)((double)i / SIZES[diskType] * 100), "Formatting %s (%d / %d bytes)", filename, i, SIZES[diskType]))
659 | break;
660 | }
661 | });
662 |
663 | // offset 0x00 (512 bytes) : write boot sector
664 | fseek(file, 0, SEEK_SET);
665 | fwrite(BOOTSECTOR[diskType], 1, 512, file);
666 |
667 | // offset 0x0c (16 bytes) : disk geometry
668 | fseek(file, 0x0c, SEEK_SET);
669 | fwrite(GEOM[diskType], 1, 16, file);
670 |
671 | // offset 0x27 (4 bytes) : Volume Serial Number
672 | const uint32_t volSN = rand();
673 | fseek(file, 0x27, SEEK_SET);
674 | fwrite(&volSN, 1, 4, file);
675 |
676 | // FAT
677 | memset(sectbuf, 0x00, 512);
678 | fseek(file, 0x200, SEEK_SET);
679 | for (int i = 0; i < (FAT[diskType].fat1size + FAT[diskType].fat2size); ++i)
680 | fwrite(sectbuf, 1, 512, file);
681 | sectbuf[0] = FAT[diskType].id;
682 | sectbuf[1] = 0xff;
683 | sectbuf[2] = 0xff;
684 | fseek(file, 0x200, SEEK_SET);
685 | fwrite(sectbuf, 1, 3, file);
686 | fseek(file, 0x200 + FAT[diskType].fat1size * 512, SEEK_SET);
687 | fwrite(sectbuf, 1, 3, file);
688 |
689 | fclose(file);
690 | }
691 |
692 | return file != nullptr;
693 | }
694 |
--------------------------------------------------------------------------------
/mconf.h:
--------------------------------------------------------------------------------
1 | /*
2 | Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) -
3 | Copyright (c) 2019-2022 Fabrizio Di Vittorio.
4 | All rights reserved.
5 |
6 |
7 | * Please contact fdivitto2013@gmail.com if you need a commercial license.
8 |
9 |
10 | * This library and related software is available under GPL v3.
11 |
12 | FabGL is free software: you can redistribute it and/or modify
13 | it under the terms of the GNU General Public License as published by
14 | the Free Software Foundation, either version 3 of the License, or
15 | (at your option) any later version.
16 |
17 | FabGL is distributed in the hope that it will be useful,
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 | GNU General Public License for more details.
21 |
22 | You should have received a copy of the GNU General Public License
23 | along with FabGL. If not, see .
24 | */
25 |
26 |
27 |
28 | /*
29 |
30 | ** Disks configuration text format **
31 |
32 | Each line contains a machine configuration, which includes description and disks images location. Location can be a URL or a local path.
33 |
34 | Allowed tags:
35 |
36 | "desc" : Textual description of the configuration
37 | "fd0" : Floppy drive 0 (A) filename or URL
38 | "fd1" : Floppy drive 1 (B) filename or URL
39 | "hd0" : Hard disk drive 0 filename or URL
40 | "hd1" : Hard disk drive 1 filename or URL
41 | "chs0" : Hard disk 0 geometry (Cylinders,Heads,Sectors)
42 | "chs1" : Hard disk 1 geometry (Cylinders,Heads,Sectors)
43 | "boot" : Boot drive. Values: fd0, fd1, hd0, hd1. Default is fd0
44 |
45 | A filename may contain a path, relative to SD_MOUNT_PATH mounting path. For example, if "disks/disk1.img" is specified and SD_MOUNT_PATH="/SD", then it loads "/SD/disks/disk1.img".
46 |
47 |
48 | Examples:
49 |
50 | Download first floppy image from "http://www.fabglib.org/downloads/A_freedos.img" and first hard disk image from "http://www.fabglib.org/downloads/C_dosdev.img". Boot from floppy:
51 | desc "FreeDOS (A:) + DOS Programming Tools (C:)" fd0 "http://www.fabglib.org/downloads/A_freedos.img" hd0 "http://www.fabglib.org/downloads/C_dosdev.img"
52 |
53 | First hard disk is "HDD_10M.IMG", having 306 cylinders, 4 heads and 17 sectors. Boot from hard disk:
54 | desc "My Own MSDOS" hd0 HDD_10M.IMG chs0 306,4,17 boot hd0
55 |
56 | First floppy drive is "TESTBOOT.IMG". Boot from first floppy:
57 | desc "Floppy Only" fd0 TESTBOOT.IMG boot fd0
58 |
59 | Boot from disk1.img located inside "msdos3" folder:
60 | desc "Machine1" fd0 msdos3/disk1.img
61 |
62 | */
63 |
64 |
65 | #pragma once
66 |
67 |
68 | #include
69 | #include
70 | #include
71 | #include
72 |
73 | #include "bios.h"
74 |
75 |
76 | #define SD_MOUNT_PATH "/SD"
77 |
78 | #define MACHINE_CONF_FILENAME "mconfs.txt"
79 |
80 | #define NL "\r\n"
81 |
82 |
83 | static const char DefaultConfFile[] =
84 | "desc \"FreeDOS (floppy 1.44MB)\" fd0 http://www.fabglib.org/downloads/floppy_FREEDOS.img" NL
85 | "desc \"MS-DOS 3.31 (floppy 1.44MB)\" fd0 http://www.fabglib.org/downloads/floppy_MSDOS331.img" NL
86 | "desc \"CP/M 86 + Turbo Pascal 3 (floppy 1.44MB)\" fd0 http://www.fabglib.org/downloads/floppy_CPM86.img" NL
87 | "desc \"Linux ELKS 0.4.0 (floppy 1.44MB)\" fd0 http://www.fabglib.org/downloads/floppy_ELK040.img" NL
88 | "desc \"FreeDOS (HDD 8MB)\" hd0 http://www.fabglib.org/downloads/hd8_FREEDOS.img boot hd0" NL
89 | "desc \"DOS Programs and Games (HDD 20MB)\" hd0 http://www.fabglib.org/downloads/hd20_DOSPROG.img boot hd0" NL
90 | "desc \"DOS Programming Tools (HDD 20MB)\" hd0 http://www.fabglib.org/downloads/hd20_DOSDEV.img boot hd0" NL
91 | "desc \"Windows 3.0 (HDD 20MB)\" hd0 http://www.fabglib.org/downloads/hd20_WINHERC.img boot hd0" NL
92 | "desc \"GEM 3.13 (HDD 8MB)\" hd0 http://www.fabglib.org/downloads/hd8_GEM31.img boot hd0" NL
93 | "desc \"GEOS 2.0 (HDD 20MB)\" hd0 http://www.fabglib.org/downloads/hd20_GEOS20.img boot hd0" NL
94 | "desc \"MS-DOS 5.00 (HDD 8MB)\" hd0 http://www.fabglib.org/downloads/hd8_MSDOS500.img boot hd0" NL
95 | "desc \"MS-DOS 6.22 (HDD 8MB)\" hd0 http://www.fabglib.org/downloads/hd8_MSDOS622.img boot hd0" NL
96 | "desc \"SvarDOS (HDD (hDD 10MB)\" hd0 http://www.fabglib.org/downloads/hd10_SVARDOS.img boot hd0" NL;
97 |
98 |
99 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////
100 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////
101 | // MachineConf
102 |
103 |
104 | struct MachineConfItem {
105 | MachineConfItem * next;
106 | char * desc;
107 | char * disk[DISKCOUNT]; // path relative to mounting path
108 | uint16_t cylinders[DISKCOUNT];
109 | uint16_t heads[DISKCOUNT];
110 | uint16_t sectors[DISKCOUNT];
111 | uint8_t bootDrive;
112 |
113 | MachineConfItem()
114 | : next(nullptr),
115 | desc(nullptr),
116 | disk(),
117 | cylinders(),
118 | heads(),
119 | sectors(),
120 | bootDrive(0)
121 | {
122 | }
123 |
124 | ~MachineConfItem()
125 | {
126 | free(desc);
127 | for (int i = 0; i < DISKCOUNT; ++i)
128 | free(disk[i]);
129 | }
130 |
131 | bool isValid() { return desc != nullptr; }
132 |
133 | void setDesc(char const * value) { free(desc); desc = strdup(value); }
134 | void setDisk(int index, char const * value) { free(disk[index]); disk[index] = strdup(value); }
135 | void setCHS(int index, char const * value) { sscanf(value, "%hu,%hu,%hu", &cylinders[index], &heads[index], §ors[index]); }
136 |
137 | void setBootDrive(char const * value) {
138 | for (int i = 0; i < DISKCOUNT; ++i)
139 | if (strcmp(value, driveIndexToStr(i)) == 0) {
140 | bootDrive = i;
141 | return;
142 | }
143 | }
144 |
145 | static char const * driveIndexToStr(int index) {
146 | static char const * DRVNAME[DISKCOUNT] = { "fd0", "fd1", "hd0", "hd1" };
147 | return DRVNAME[index];
148 | }
149 | };
150 |
151 |
152 | class MachineConf {
153 | public:
154 | MachineConf();
155 | ~MachineConf();
156 |
157 | void loadFromFile(FILE * file);
158 | void saveToFile(FILE * file);
159 |
160 | bool deleteItem(int index);
161 |
162 | void addItem(MachineConfItem * item);
163 | void insertItem(int insertingPoint, MachineConfItem * item);
164 |
165 | MachineConfItem * getFirstItem() { return m_itemsList; }
166 | MachineConfItem * getItem(int index);
167 |
168 | private:
169 |
170 | void cleanUp();
171 |
172 | MachineConfItem * m_itemsList;
173 |
174 | };
175 |
176 |
177 |
178 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////
179 | ////////////////////////////////////////////////////////////////////////////////////////////////////////////
180 | // helpers
181 |
182 |
183 | void loadMachineConfiguration(MachineConf * mconf);
184 |
185 | void saveMachineConfiguration(MachineConf * mconf);
186 |
187 | void editConfigDialog(InputBox * ibox, MachineConf * mconf, int idx);
188 |
189 | void newConfigDialog(InputBox * ibox, MachineConf * mconf, int idx);
190 |
191 | void delConfigDialog(InputBox * ibox, MachineConf * mconf, int idx);
192 |
193 | void drawInfo(Canvas * canvas);
194 |
195 | bool createFATFloppyImage(InputBox * ibox, int diskType, char const * directory, char const * filename);
196 |
--------------------------------------------------------------------------------