다마고치 연구소 한국어판 소스 공개
This commit is contained in:
commit
db23855630
661 files changed
+503597
No files matched your search
@@ -0,0 +1,674 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program does terminal interaction, make it output a short
|
||||
notice like this when it starts in an interactive mode:
|
||||
|
||||
<program> Copyright (C) <year> <name of author>
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, your program's commands
|
||||
might be different; for a GUI interface, you would use an "about box".
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU GPL, see
|
||||
<http://www.gnu.org/licenses/>.
|
||||
|
||||
The GNU General Public License does not permit incorporating your program
|
||||
into proprietary programs. If your program is a subroutine library, you
|
||||
may consider it more useful to permit linking proprietary applications with
|
||||
the library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License. But first, please read
|
||||
<http://www.gnu.org/philosophy/why-not-lgpl.html>.
|
||||
@@ -0,0 +1,232 @@
|
||||
/* Lean core for colour Tamagotchis (S1C33), derived from tamaemu
|
||||
* (github.com/maragotchi/tamaemu, GPL-3.0). For Paradise apps without an
|
||||
* operating system: no stdio, no floating point, fixed memory blocks. */
|
||||
#ifndef TC_H
|
||||
#define TC_H
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
/* Only needed at startup: placed at the end of the image, the translator takes the space afterwards */
|
||||
#if defined(__arm__) && !defined(TC_HOST)
|
||||
#ifdef EMU_OVL
|
||||
/* 1.6.13 test10: start code for size (the program image is full) */
|
||||
#define TC_INIT __attribute__((section(".initcode"), noinline, minsize))
|
||||
#define TC_INIT_LAST __attribute__((section(".initcode_z"), noinline, minsize))
|
||||
#else
|
||||
#define TC_INIT __attribute__((section(".initcode"), noinline))
|
||||
/* at the very end: writes the fixed routines to the start of the area */
|
||||
#define TC_INIT_LAST __attribute__((section(".initcode_z"), noinline))
|
||||
#endif
|
||||
/* 1.6.8: rarely run code, small (0x20000000 is only 4 KB, mirrored; the SRAM is full) */
|
||||
#define TC_SRAM __attribute__((noinline, minsize))
|
||||
#else
|
||||
#define TC_SRAM
|
||||
#define TC_INIT
|
||||
#define TC_INIT_LAST
|
||||
#endif
|
||||
#include <stddef.h>
|
||||
#ifdef TC_HOST
|
||||
#include <string.h>
|
||||
#else
|
||||
void *memset(void *d, int c, size_t n);
|
||||
void *memcpy(void *d, const void *s, size_t n);
|
||||
#endif
|
||||
|
||||
typedef struct DeviceProfile {
|
||||
uint32_t rom_base, rom_size;
|
||||
uint32_t a0ram_base, a0ram_size;
|
||||
uint32_t ivram_base, ivram_size;
|
||||
uint32_t dstram_base, dstram_size;
|
||||
uint32_t io_base, io_end, io_size;
|
||||
uint32_t lcd_cmd_addr, lcd_data_addr;
|
||||
bool flash_top_boot;
|
||||
bool debug_strap_high;
|
||||
bool has_sleep_flag;
|
||||
uint32_t sleep_flag;
|
||||
uint32_t ttbr_reset;
|
||||
uint32_t osc3_hz;
|
||||
uint8_t ir_gpio_rx_bit;
|
||||
bool ir_gpio;
|
||||
bool nfc_pn512; /* 4U/4U+: the PN512 NFC chip answers on the IR UART (1.6.8) */
|
||||
} DeviceProfile;
|
||||
|
||||
#define A0RAM_MAX 0x00008000u
|
||||
#define IVRAM_MAX 0x00003000u
|
||||
#define DSTRAM_MAX 0x00001000u
|
||||
#define IORAM_MAX 0x00002000u
|
||||
#define GRAM_W 128
|
||||
#define GRAM_H 128
|
||||
|
||||
typedef struct Lcd {
|
||||
uint16_t (*gram)[GRAM_W]; /* 128x128, located in fast PRAM */
|
||||
uint8_t cmd;
|
||||
int argc;
|
||||
uint8_t args[16];
|
||||
int xs, xe, ys, ye, cx, cy;
|
||||
uint8_t madctl, colmod;
|
||||
bool sleep_out, disp_on, inverted;
|
||||
uint8_t pix_buf[3];
|
||||
int pix_n;
|
||||
/* changed area since the last fetch */
|
||||
int dx0, dy0, dx1, dy1;
|
||||
} Lcd;
|
||||
|
||||
typedef struct Cmu {
|
||||
uint8_t unlocked;
|
||||
uint32_t sccr, pllc, opt;
|
||||
uint32_t osc3_hz, mclk_hz;
|
||||
} Cmu;
|
||||
|
||||
typedef struct T16 { uint16_t counter; uint8_t a_fired; uint32_t accum; } T16;
|
||||
|
||||
/* Writable flash sectors live in RAM (overlay), the rest is read
|
||||
* directly from the Paradise flash (XIP). */
|
||||
#define OVL_SLOTS 8
|
||||
#define OVL_SIZE 0x2000u /* one 8 KB parameter sector per slot */
|
||||
typedef struct Overlay {
|
||||
uint32_t base[OVL_SLOTS]; /* ROM offset of the sector, 0xFFFFFFFF = free */
|
||||
uint8_t *mem[OVL_SLOTS];
|
||||
int n;
|
||||
} Overlay;
|
||||
|
||||
typedef struct Emu {
|
||||
uint32_t r[16];
|
||||
uint32_t sp, alr, ahr, ttbr, idir, dbbr;
|
||||
uint32_t pc;
|
||||
/* Translator (tc_jit.c): fields near the start so the generated code
|
||||
* reaches them with short offsets. */
|
||||
uint32_t nzcv; /* flags in ARM format, C = not guest C */
|
||||
int32_t jit_left; /* remaining cycles (r10) */
|
||||
uint32_t jit_bias; /* exit request from a helper */
|
||||
uint32_t jit_a0; /* target address of a0ram */
|
||||
uint32_t jit_h[9]; /* target addresses of the helpers */
|
||||
uint32_t jit_flush; /* flush the cache at the next stop */
|
||||
int flash_busy; /* kept up front: the translator reads ROM directly */
|
||||
bool flash_autoselect; /* as long as both are zero */
|
||||
uint32_t jit_rom[128]; /* ROM per 64 KB as target address (0 = helper), see jit_rom_update */
|
||||
uint32_t jit_par[8]; /* parameter block per 8 KB (overlay or original) */
|
||||
uint32_t jit_parb; /* offset of the parameter block */
|
||||
uint8_t jit_map[128]; /* a0ram pages (256 B) with translated code */
|
||||
uint32_t jit_slot[600]; /* jump targets of the exits (chaining without changing code) */
|
||||
uint32_t jit_itab[2 * 256];/* jump targets: (guest pc, code|1), direct mapped */
|
||||
uint8_t f_n, f_z, f_v, f_c, f_ie;
|
||||
uint8_t il;
|
||||
uint32_t psr_rest;
|
||||
int ext_count;
|
||||
uint32_t ext_val[2];
|
||||
bool halted, halt_hard;
|
||||
uint64_t wake_at;
|
||||
uint64_t cycles;
|
||||
uint64_t evt; /* next stop of the fast core */
|
||||
bool stopped;
|
||||
uint32_t stop_pc, stop_code;
|
||||
DeviceProfile dev;
|
||||
const uint8_t *rom; /* original in flash (uncached window) */
|
||||
const uint8_t *rom_fast; /* only for tc_cpu.c (reference on the host) */
|
||||
uint32_t rom_fast_len;
|
||||
/* ROM in 64 KB blocks: rom_src = data (0 = erased, reads 0xFF),
|
||||
* rom_blk = fast read pointer (0 = via mem_read, e.g. parameter block) */
|
||||
const uint8_t *rom_src[128];
|
||||
const uint8_t *rom_blk[128];
|
||||
uint32_t par_base; /* 64 KB with 8 KB parameter sectors */
|
||||
/* Instruction window: host pointer for pc in [fw_lo, fw_lo+fw_len) */
|
||||
const uint8_t *fw_ptr; uint32_t fw_lo, fw_len;
|
||||
Overlay ovl;
|
||||
uint8_t a0ram[A0RAM_MAX];
|
||||
uint8_t ivram[IVRAM_MAX];
|
||||
uint8_t dstram[DSTRAM_MAX];
|
||||
uint8_t ioram[IORAM_MAX];
|
||||
Cmu cmu;
|
||||
Lcd lcd;
|
||||
T16 t16[6];
|
||||
uint32_t pending_irq;
|
||||
uint8_t pending_level;
|
||||
bool wake_req;
|
||||
uint64_t last_tick;
|
||||
uint8_t btn_mask;
|
||||
uint32_t rtc_cyc, rtc_irq_cyc;
|
||||
int flash_state;
|
||||
bool flash_dirty;
|
||||
uint16_t flash_op_data;
|
||||
bool flash_toggle;
|
||||
uint32_t flash_lost_writes; /* write attempts without a free slot */
|
||||
uint64_t lcd_writes;
|
||||
uint32_t ovl_blk[4]; /* 64 KB blocks outside the parameter block with a written copy (bit per block) */
|
||||
uint32_t flash_big_ops; /* erases and writes in 64 KB sectors (for the display) */
|
||||
/* Blocks without their own storage (erased in the original): written 8 KB parts live in
|
||||
* spare pages (device). pg_blk: bit per block, pg_map: page index per 8 KB (0xFF = erased). */
|
||||
const uint8_t *pg_base;
|
||||
uint32_t pg_blk[4];
|
||||
uint8_t pg_map[1024];
|
||||
} Emu;
|
||||
|
||||
/* cpu */
|
||||
void cpu_reset(Emu *e);
|
||||
void cpu_step(Emu *e);
|
||||
/* mem */
|
||||
uint8_t mem_read8 (Emu *e, uint32_t a);
|
||||
uint16_t mem_read16(Emu *e, uint32_t a);
|
||||
uint32_t mem_read32(Emu *e, uint32_t a);
|
||||
void mem_write8 (Emu *e, uint32_t a, uint8_t v);
|
||||
void mem_write16(Emu *e, uint32_t a, uint16_t v);
|
||||
void mem_write32(Emu *e, uint32_t a, uint32_t v);
|
||||
void tc_fetch_window(Emu *e, uint32_t pc);
|
||||
/* periph */
|
||||
uint8_t periph_read8 (Emu *e, uint32_t a);
|
||||
uint32_t periph_read32(Emu *e, uint32_t a);
|
||||
void periph_write8 (Emu *e, uint32_t a, uint8_t v);
|
||||
void periph_write32(Emu *e, uint32_t a, uint32_t v);
|
||||
void periph_tick(Emu *e, uint32_t cycles);
|
||||
uint32_t periph_next(Emu *e);
|
||||
void periph_rtc_advance(Emu *e, uint32_t n);
|
||||
void periph_rtc_ms(Emu *e);
|
||||
extern uint32_t g_tc_rtc_wr; /* writes of the guest to its clock registers (1.6.8) */
|
||||
#ifdef TC_IRLINK
|
||||
/* 1.6.9: infrared to another Paradise (tc_periph.c); the platform passes the bytes */
|
||||
void tc_ir_setup(uint32_t mode_flag, uint32_t ctx_ptr);
|
||||
int tc_ir_avail(void);
|
||||
int tc_ir_in(void);
|
||||
void tc_ir_out(uint8_t b);
|
||||
#endif
|
||||
void periph_rtc_set(Emu *e, uint32_t t); /* local time, seconds since 1970 */
|
||||
uint32_t periph_rtc_get(Emu *e); /* 1.7.2 test16: the guest clock, 0 = stopped or not a date */
|
||||
void periph_buttons(Emu *e, uint8_t mask);
|
||||
void periph_init(Emu *e);
|
||||
/* lcd */
|
||||
void lcd_cmd (Emu *e, uint8_t c);
|
||||
void lcd_data(Emu *e, uint8_t d);
|
||||
/* device */
|
||||
void tc_profile_plus_color(DeviceProfile *d);
|
||||
void tc_profile_idl(DeviceProfile *d);
|
||||
void tc_rom_linear(Emu *e, const uint8_t *rom);
|
||||
void tc_init(Emu *e, const uint8_t *rom_xip, uint16_t (*gram)[GRAM_W], uint8_t *ovl_pool, int slots);
|
||||
/* Runs until cycles >= ziel; returns true if stopped (error). */
|
||||
bool tc_run(Emu *e, uint64_t ziel);
|
||||
void tc_exec(Emu *e, uint64_t ziel);
|
||||
/* translator */
|
||||
void jit_init(Emu *e, uint8_t *buf, uint32_t tgt, uint32_t size);
|
||||
void jit_exec(Emu *e, uint64_t ziel);
|
||||
void jit_flush_all(Emu *e);
|
||||
uint32_t periph_tone_hz(Emu *e); /* guest tone in Hz, 0 = silent */
|
||||
/* All overlay slots in use: write one back and free it (device). */
|
||||
extern int (*tc_ovl_evict)(Emu *e);
|
||||
/* Erase a 64 KB sector of the guest in the backing store (host image or Paradise flash).
|
||||
* Returns 1 if done. Without it, such erases are lost as before. */
|
||||
extern int (*tc_big_erase)(Emu *e, uint32_t base);
|
||||
#ifdef TC_LZ
|
||||
extern const uint8_t *tc_lz_tab; /* 1.7.3 GUEST_LZ: table of the packed cold blocks (0 = none) */
|
||||
extern uint32_t tc_lz_gone[4];
|
||||
#endif
|
||||
void tc_ovl_free(Emu *e, int i); /* release a slot without writing it back */
|
||||
void tc_blk_update(Emu *e, uint32_t blk); /* fast pointers of a 64 KB block after a change */
|
||||
void jit_rom_update(Emu *e, uint32_t off); /* ROM offset: recompute the entry */
|
||||
extern uint32_t (*jit_tgt_of)(const uint8_t *p); /* host pointer -> target address (test bench) */
|
||||
extern int jit_enabled;
|
||||
/* Platform: enter generated code, or sync the instruction cache */
|
||||
uint32_t jit_enter(Emu *e, uint32_t code);
|
||||
extern volatile uint32_t jit_dbg[44];
|
||||
void jit_stage_setup(uint8_t *stage, uint32_t size);
|
||||
extern uint32_t jit_st_copy;
|
||||
extern void (*jit_bad_hook)(Emu *e);
|
||||
void jit_sync(uint32_t lo, uint32_t hi);
|
||||
void tc_exec(Emu *e, uint64_t ziel);
|
||||
#endif
|
||||
@@ -0,0 +1,9 @@
|
||||
/* One translation unit for the whole core, so the compiler can inline. */
|
||||
#include "tc_mem.c"
|
||||
#include "tc_periph.c"
|
||||
#include "tc_lcd.c"
|
||||
#include "tc_core.c"
|
||||
#include "tc_fast.c"
|
||||
#ifdef TC_JIT
|
||||
#include "tc_jit.c"
|
||||
#endif
|
||||
@@ -0,0 +1,100 @@
|
||||
#include "tc.h"
|
||||
|
||||
TC_INIT void tc_profile_idl(DeviceProfile *d)
|
||||
{
|
||||
memset(d, 0, sizeof *d);
|
||||
d->rom_base = 0x02000000u; d->rom_size = 0x00800000u;
|
||||
d->a0ram_base = 0x00000000u; d->a0ram_size = 0x00008000u;
|
||||
d->ivram_base = 0x00080000u; d->ivram_size = 0x00003000u;
|
||||
d->dstram_base = 0x00084000u; d->dstram_size = 0x00001000u;
|
||||
d->io_base = 0x00300000u; d->io_end = 0x00302000u; d->io_size = 0x2000u;
|
||||
d->lcd_cmd_addr = 0x00600000u; d->lcd_data_addr = 0x00600001u;
|
||||
d->flash_top_boot = false;
|
||||
d->debug_strap_high = false;
|
||||
d->has_sleep_flag = true; d->sleep_flag = 0x00000FD8u;
|
||||
d->ttbr_reset = 0x02400000u;
|
||||
d->osc3_hz = 18432000u;
|
||||
}
|
||||
|
||||
TC_INIT void tc_profile_plus_color(DeviceProfile *d)
|
||||
{
|
||||
memset(d, 0, sizeof *d);
|
||||
d->rom_base = 0x00C00000u; d->rom_size = 0x00400000u;
|
||||
d->a0ram_base = 0x00000000u; d->a0ram_size = 0x00008000u;
|
||||
d->ivram_base = 0x00080000u; d->ivram_size = 0x00003000u;
|
||||
d->dstram_base = 0x00084000u; d->dstram_size = 0x00001000u;
|
||||
d->io_base = 0x00300000u; d->io_end = 0x00302000u; d->io_size = 0x2000u;
|
||||
d->lcd_cmd_addr = 0x00600000u; d->lcd_data_addr = 0x00600001u;
|
||||
d->flash_top_boot = true;
|
||||
#ifdef TC_STRAP_TEST
|
||||
d->debug_strap_high = false; /* test only */
|
||||
#else
|
||||
d->debug_strap_high = true;
|
||||
#endif
|
||||
d->has_sleep_flag = true; d->sleep_flag = 0x00000368u;
|
||||
d->ttbr_reset = 0x00C00000u;
|
||||
d->osc3_hz = 20000000u;
|
||||
d->ir_gpio = true; d->ir_gpio_rx_bit = 5;
|
||||
}
|
||||
|
||||
TC_INIT void tc_init(Emu *e, const uint8_t *rom_xip, uint16_t (*gram)[GRAM_W], uint8_t *ovl_pool, int slots)
|
||||
{
|
||||
DeviceProfile d = e->dev;
|
||||
memset(e, 0, sizeof *e);
|
||||
e->dev = d;
|
||||
e->rom = rom_xip;
|
||||
e->rom_fast = rom_xip; e->rom_fast_len = 0;
|
||||
e->par_base = d.flash_top_boot ? d.rom_size - 0x10000u : 0;
|
||||
if (rom_xip) tc_rom_linear(e, rom_xip);
|
||||
e->lcd.gram = gram;
|
||||
for (int i = 0; i < OVL_SLOTS; i++) { e->ovl.base[i] = 0xFFFFFFFFu; e->ovl.mem[i] = 0; }
|
||||
for (int i = 0; i < slots && i < OVL_SLOTS; i++) e->ovl.mem[i] = ovl_pool + (uint32_t)i * OVL_SIZE;
|
||||
e->ovl.n = 0;
|
||||
e->lcd.xe = 131; e->lcd.ye = 161;
|
||||
e->lcd.dx0 = 0; e->lcd.dy0 = 0; e->lcd.dx1 = 127; e->lcd.dy1 = 127;
|
||||
e->fw_len = 0; e->fw_lo = 0xFFFFFFFFu;
|
||||
periph_init(e);
|
||||
cpu_reset(e);
|
||||
}
|
||||
|
||||
uint32_t tc_halt_steps;
|
||||
/* ROM is contiguous from rom (fill the block tables). */
|
||||
TC_INIT __attribute__((minsize)) void tc_rom_linear(Emu *e, const uint8_t *rom)
|
||||
{
|
||||
for (uint32_t i = 0; i < 128; i++) {
|
||||
e->rom_src[i] = (i << 16) < e->dev.rom_size ? rom + (i << 16) : 0;
|
||||
e->rom_blk[i] = e->rom_src[i];
|
||||
}
|
||||
e->rom_blk[e->par_base >> 16] = 0;
|
||||
}
|
||||
|
||||
bool tc_run(Emu *e, uint64_t ziel)
|
||||
{
|
||||
while (e->cycles < ziel) {
|
||||
if (e->stopped) return true;
|
||||
if (e->halted) {
|
||||
if (e->pending_irq || e->wake_req || (!e->halt_hard && e->cycles >= e->wake_at)) {
|
||||
e->halted = false;
|
||||
} else {
|
||||
/* Sleeping CPU: fast forward time in steps instead of cycle by cycle. */
|
||||
uint64_t step = ziel - e->cycles;
|
||||
#ifdef TC_JIT
|
||||
{ uint32_t nx = periph_next(e); if (step > nx) step = nx; tc_halt_steps++; }
|
||||
#else
|
||||
if (step > 1024) step = 1024;
|
||||
#endif
|
||||
if (!e->halt_hard && e->wake_at > e->cycles && e->wake_at - e->cycles < step) step = e->wake_at - e->cycles;
|
||||
if (!step) step = 1;
|
||||
e->cycles += step;
|
||||
periph_tick(e, (uint32_t)(e->cycles - e->last_tick));
|
||||
e->last_tick = e->cycles;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
#ifdef TC_JIT
|
||||
if (jit_enabled) { jit_exec(e, ziel); continue; }
|
||||
#endif
|
||||
tc_exec(e, ziel);
|
||||
}
|
||||
return e->stopped;
|
||||
}
|
||||
@@ -0,0 +1,629 @@
|
||||
/* S1C33 PE interpreter implemented with reference to GMMan's SLEIGH definition:
|
||||
* https://github.com/GMMan/s1c33_sleigh
|
||||
*/
|
||||
|
||||
#include "tc.h"
|
||||
|
||||
|
||||
/* Fast paths for the most common accesses: A0RAM at 0, IVRAM at 0x80000
|
||||
* (stack), DSTRAM at 0x84000, cached ROM. */
|
||||
#ifdef TC_STATS
|
||||
void stat_acc(uint32_t a, int w);
|
||||
#define ST(a, w) stat_acc((a), (w))
|
||||
#else
|
||||
#define ST(a, w) ((void)0)
|
||||
#endif
|
||||
static inline uint8_t *ramp(Emu *e, uint32_t a, uint32_t n)
|
||||
{
|
||||
if (a <= A0RAM_MAX - n) return e->a0ram + a;
|
||||
uint32_t o = a - 0x80000u;
|
||||
if (o <= IVRAM_MAX - n) return e->ivram + o;
|
||||
o = a - 0x84000u;
|
||||
if (o <= DSTRAM_MAX - n) return e->dstram + o;
|
||||
return 0;
|
||||
}
|
||||
static inline uint8_t rd8(Emu *e, uint32_t a)
|
||||
{
|
||||
ST(a, 0);
|
||||
uint8_t *p = ramp(e, a, 1); if (p) return *p;
|
||||
uint32_t o = a - e->dev.rom_base;
|
||||
if (o < e->rom_fast_len && !e->flash_busy && !e->flash_autoselect) return e->rom_fast[o];
|
||||
return mem_read8(e, a);
|
||||
}
|
||||
static inline uint16_t rd16(Emu *e, uint32_t a)
|
||||
{
|
||||
ST(a, 0);
|
||||
uint8_t *p = ramp(e, a, 2); if (p) return (uint16_t)(p[0] | (p[1] << 8));
|
||||
uint32_t o = a - e->dev.rom_base;
|
||||
if (o < e->rom_fast_len - 1 && e->rom_fast_len && !e->flash_busy && !e->flash_autoselect) { const uint8_t *q = e->rom_fast + o; return (uint16_t)(q[0] | (q[1] << 8)); }
|
||||
return mem_read16(e, a);
|
||||
}
|
||||
static inline uint32_t rd32(Emu *e, uint32_t a)
|
||||
{
|
||||
ST(a, 0);
|
||||
uint8_t *p = ramp(e, a, 4); if (p) return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
|
||||
uint32_t o = a - e->dev.rom_base;
|
||||
if (o < e->rom_fast_len - 3 && e->rom_fast_len && !e->flash_busy && !e->flash_autoselect) { const uint8_t *q = e->rom_fast + o; return (uint32_t)q[0] | ((uint32_t)q[1] << 8) | ((uint32_t)q[2] << 16) | ((uint32_t)q[3] << 24); }
|
||||
return mem_read32(e, a);
|
||||
}
|
||||
static inline void wr8(Emu *e, uint32_t a, uint8_t v) { ST(a, 1); uint8_t *p = ramp(e, a, 1); if (p) *p = v; else mem_write8(e, a, v); }
|
||||
static inline void wr16(Emu *e, uint32_t a, uint16_t v) { ST(a, 1); uint8_t *p = ramp(e, a, 2); if (p) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); } else mem_write16(e, a, v); }
|
||||
static inline void wr32(Emu *e, uint32_t a, uint32_t v) { ST(a, 1); uint8_t *p = ramp(e, a, 4); if (p) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); p[2] = (uint8_t)(v >> 16); p[3] = (uint8_t)(v >> 24); } else mem_write32(e, a, v); }
|
||||
|
||||
static uint32_t sext(uint32_t v, int bits)
|
||||
{
|
||||
uint32_t m = 1u << (bits - 1);
|
||||
return (v ^ m) - m;
|
||||
}
|
||||
|
||||
/* ---- flag helpers (s1c33_macros.sinc) ---- */
|
||||
static void result_flags(Emu *e, uint32_t r)
|
||||
{
|
||||
e->f_n = (r >> 31) & 1;
|
||||
e->f_z = (r == 0);
|
||||
}
|
||||
|
||||
static uint8_t scarry(uint32_t a, uint32_t b)
|
||||
{
|
||||
uint32_t s = a + b;
|
||||
return ((~(a ^ b)) & (a ^ s)) >> 31;
|
||||
}
|
||||
|
||||
static uint8_t sborrow(uint32_t a, uint32_t b)
|
||||
{
|
||||
uint32_t d = a - b;
|
||||
return ((a ^ b) & (a ^ d)) >> 31;
|
||||
}
|
||||
|
||||
static uint32_t add_op(Emu *e, uint32_t a, uint32_t b)
|
||||
{
|
||||
e->f_v = scarry(a, b);
|
||||
e->f_c = (a + b) < a;
|
||||
uint32_t r = a + b;
|
||||
result_flags(e, r);
|
||||
return r;
|
||||
}
|
||||
|
||||
static uint32_t adc_op(Emu *e, uint32_t a, uint32_t b)
|
||||
{
|
||||
uint32_t cz = e->f_c, s = a + b;
|
||||
e->f_v = scarry(a, b) ^ scarry(s, cz);
|
||||
e->f_c = ((a + b) < a) || ((s + cz) < s);
|
||||
uint32_t r = s + cz;
|
||||
result_flags(e, r);
|
||||
return r;
|
||||
}
|
||||
|
||||
static uint32_t sub_op(Emu *e, uint32_t a, uint32_t b)
|
||||
{
|
||||
e->f_v = sborrow(a, b);
|
||||
e->f_c = a < b;
|
||||
uint32_t r = a - b;
|
||||
result_flags(e, r);
|
||||
return r;
|
||||
}
|
||||
|
||||
static uint32_t sbc_op(Emu *e, uint32_t a, uint32_t b)
|
||||
{
|
||||
uint32_t cz = e->f_c, d = a - b;
|
||||
e->f_v = sborrow(a, b) || sborrow(d, cz);
|
||||
e->f_c = (a < b) || (d < cz);
|
||||
uint32_t r = d - cz;
|
||||
result_flags(e, r);
|
||||
return r;
|
||||
}
|
||||
|
||||
static void cmp_op(Emu *e, uint32_t a, uint32_t b)
|
||||
{
|
||||
e->f_v = sborrow(a, b);
|
||||
e->f_c = a < b;
|
||||
result_flags(e, a - b);
|
||||
}
|
||||
|
||||
static uint32_t log_flags(Emu *e, uint32_t r)
|
||||
{
|
||||
result_flags(e, r);
|
||||
e->f_v = 0;
|
||||
return r;
|
||||
}
|
||||
|
||||
static uint32_t sl_op(Emu *e, uint32_t a, uint32_t n)
|
||||
{
|
||||
n &= 31;
|
||||
uint32_t r = a << n;
|
||||
result_flags(e, r);
|
||||
return r;
|
||||
}
|
||||
|
||||
static uint32_t srl_op(Emu *e, uint32_t a, uint32_t n)
|
||||
{
|
||||
n &= 31;
|
||||
uint32_t r = a >> n;
|
||||
result_flags(e, r);
|
||||
return r;
|
||||
}
|
||||
|
||||
static uint32_t sra_op(Emu *e, uint32_t a, uint32_t n)
|
||||
{
|
||||
n &= 31;
|
||||
uint32_t r = (uint32_t)((int32_t)a >> n);
|
||||
result_flags(e, r);
|
||||
return r;
|
||||
}
|
||||
|
||||
static uint32_t rr_op(Emu *e, uint32_t a, uint32_t n)
|
||||
{
|
||||
n &= 31;
|
||||
uint32_t r = n ? ((a >> n) | (a << (32 - n))) : a;
|
||||
result_flags(e, r);
|
||||
return r;
|
||||
}
|
||||
|
||||
static uint32_t rl_op(Emu *e, uint32_t a, uint32_t n)
|
||||
{
|
||||
n &= 31;
|
||||
uint32_t r = n ? ((a << n) | (a >> (32 - n))) : a;
|
||||
result_flags(e, r);
|
||||
return r;
|
||||
}
|
||||
|
||||
/* ---- PSR pack/unpack (bit0 N,1 Z,2 V,3 C,4 IE, 8-11 IL) ---- */
|
||||
static uint32_t psr_pack(Emu *e)
|
||||
{
|
||||
return (e->psr_rest & ~0xF1Fu) | e->f_n | (e->f_z << 1) | (e->f_v << 2) |
|
||||
(e->f_c << 3) | (e->f_ie << 4) | ((uint32_t)(e->il & 0xF) << 8);
|
||||
}
|
||||
|
||||
static void psr_unpack(Emu *e, uint32_t v)
|
||||
{
|
||||
e->f_n = v & 1;
|
||||
e->f_z = (v >> 1) & 1;
|
||||
e->f_v = (v >> 2) & 1;
|
||||
e->f_c = (v >> 3) & 1;
|
||||
e->f_ie = (v >> 4) & 1;
|
||||
e->il = (v >> 8) & 0xF;
|
||||
e->psr_rest = v;
|
||||
}
|
||||
|
||||
static void push32(Emu *e, uint32_t v)
|
||||
{
|
||||
e->sp -= 4;
|
||||
wr32(e, e->sp, v);
|
||||
}
|
||||
|
||||
static uint32_t pop32(Emu *e)
|
||||
{
|
||||
uint32_t v = rd32(e, e->sp);
|
||||
e->sp += 4;
|
||||
return v;
|
||||
}
|
||||
|
||||
static void stop(Emu *e, uint32_t iaddr, const char *why, uint32_t detail)
|
||||
{
|
||||
(void)why;
|
||||
e->stopped = true; e->stop_pc = iaddr; e->stop_code = detail;
|
||||
}
|
||||
|
||||
/* Trap instruction fetches outside ROM and RAM. Unmapped reads return zero,
|
||||
* which otherwise turns a wild jump into an endless stream of valid NOPs. */
|
||||
static int pc_mapped(Emu *e, uint32_t a)
|
||||
{
|
||||
const DeviceProfile *d = &e->dev;
|
||||
return a - d->rom_base < d->rom_size || a - d->a0ram_base < d->a0ram_size ||
|
||||
a - d->ivram_base < d->ivram_size || a - d->dstram_base < d->dstram_size;
|
||||
}
|
||||
|
||||
#define trace_call(e,t,f) ((void)0)
|
||||
#define trace_jump(e,t,f) ((void)0)
|
||||
#define trace_ret(e,t) ((void)0)
|
||||
|
||||
static void bad_pc(Emu *e)
|
||||
{
|
||||
stop(e, e->pc, "fetch from unmapped memory", e->pc);
|
||||
}
|
||||
|
||||
/* special register file for class 5 moves: sd=(0,3) / ss=(4,7) index */
|
||||
static uint32_t sreg_read(Emu *e, int idx, uint32_t iaddr)
|
||||
{
|
||||
switch (idx) {
|
||||
case 0: return psr_pack(e);
|
||||
case 1: return e->sp;
|
||||
case 2: return e->alr;
|
||||
case 3: return e->ahr;
|
||||
case 8: return e->ttbr;
|
||||
case 10: return e->idir;
|
||||
case 11: return e->dbbr;
|
||||
case 15: return iaddr;
|
||||
default: stop(e, iaddr, "read of invalid special reg", idx); return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void sreg_write(Emu *e, int idx, uint32_t v, uint32_t iaddr)
|
||||
{
|
||||
switch (idx) {
|
||||
case 0: psr_unpack(e, v); break;
|
||||
case 1: e->sp = v; break;
|
||||
case 2: e->alr = v; break;
|
||||
case 3: e->ahr = v; break;
|
||||
case 8: e->ttbr = v; break;
|
||||
case 10: e->idir = v; break;
|
||||
case 11: e->dbbr = v; break;
|
||||
case 15: e->pc = v; break; /* ld.w %pc,%rs acts as a jump */
|
||||
default: stop(e, iaddr, "write of invalid special reg", idx); break;
|
||||
}
|
||||
}
|
||||
|
||||
/* conditional branch predicates, c0_op1 4..13 */
|
||||
static int cond_true(Emu *e, int op)
|
||||
{
|
||||
switch (op) {
|
||||
case 4: return !e->f_z && e->f_n == e->f_v; /* jrgt */
|
||||
case 5: return e->f_n == e->f_v; /* jrge */
|
||||
case 6: return e->f_n != e->f_v; /* jrlt */
|
||||
case 7: return e->f_z || e->f_n != e->f_v; /* jrle */
|
||||
case 8: return !e->f_z && !e->f_c; /* jrugt */
|
||||
case 9: return !e->f_c; /* jruge */
|
||||
case 10: return e->f_c; /* jrult */
|
||||
case 11: return e->f_z || e->f_c; /* jrule */
|
||||
case 12: return e->f_z; /* jreq */
|
||||
case 13: return !e->f_z; /* jrne */
|
||||
default: return 1; /* call/jp */
|
||||
}
|
||||
}
|
||||
|
||||
static void exec_insn(Emu *e, uint16_t hw, uint32_t iaddr, int in_slot);
|
||||
|
||||
/* execute exactly one instruction (ext prefixes folded) at e->pc; no irq check */
|
||||
static void step_one(Emu *e, int in_slot)
|
||||
{
|
||||
e->pc &= 0x0FFFFFFFu; /* 28-bit address bus */
|
||||
if (e->pc - e->fw_lo >= e->fw_len) {
|
||||
if (!pc_mapped(e, e->pc)) { bad_pc(e); return; }
|
||||
tc_fetch_window(e, e->pc);
|
||||
}
|
||||
uint16_t hw = e->fw_len ? *(const uint16_t *)(e->fw_ptr + (e->pc - e->fw_lo)) : rd16(e, e->pc);
|
||||
int guard = 0;
|
||||
while ((hw >> 13) == 6) { /* class 6 = ext prefix */
|
||||
if (in_slot && guard == 0)
|
||||
((void)0);
|
||||
if (e->ext_count < 2) e->ext_val[e->ext_count] = hw & 0x1FFF;
|
||||
e->ext_count++;
|
||||
e->pc += 2; e->cycles++;
|
||||
hw = (e->pc - e->fw_lo < e->fw_len) ? *(const uint16_t *)(e->fw_ptr + (e->pc - e->fw_lo)) : rd16(e, e->pc);
|
||||
if (++guard > 2) { stop(e, e->pc, "more than two ext prefixes", hw); return; }
|
||||
}
|
||||
uint32_t iaddr = e->pc;
|
||||
e->pc += 2; e->cycles++;
|
||||
exec_insn(e, hw, iaddr, in_slot);
|
||||
}
|
||||
|
||||
/* run the delay slot of a .d instruction */
|
||||
static void run_slot(Emu *e)
|
||||
{
|
||||
if (e->stopped) return;
|
||||
step_one(e, 1);
|
||||
}
|
||||
|
||||
static void exec_insn(Emu *e, uint16_t hw, uint32_t iaddr, int in_slot)
|
||||
{
|
||||
int cls = hw >> 13;
|
||||
int rd = hw & 0xF, rs = (hw >> 4) & 0xF;
|
||||
int ext_n = e->ext_count > 2 ? 2 : e->ext_count;
|
||||
uint32_t ext0 = e->ext_val[0], ext1 = e->ext_val[1];
|
||||
e->ext_count = 0;
|
||||
|
||||
switch (cls) {
|
||||
|
||||
case 0: {
|
||||
int op1 = (hw >> 9) & 0xF, d = (hw >> 8) & 1;
|
||||
if (op1 <= 3) {
|
||||
int cnst = (hw >> 4) & 3;
|
||||
if (cnst == 0) {
|
||||
int op2 = (hw >> 6) & 3;
|
||||
switch (op1 * 8 + d * 4 + op2) {
|
||||
case 0: /* nop */ break;
|
||||
case 1: /* slp */ {
|
||||
const int force_wake = 0;
|
||||
e->halted = true;
|
||||
/* OSC-stabilization auto-wake only when the CMU Clock Option
|
||||
* register is armed for it (boot/PLL flow writes 0x3E00 before
|
||||
* slp). A normal idle slp sleeps until a real interrupt. */
|
||||
e->halt_hard = !(e->cmu.opt & 0x3E00) && !force_wake;
|
||||
e->wake_at = e->cycles + 20000;
|
||||
break; }
|
||||
case 2: /* halt */ e->halted = true; e->halt_hard = true; break;
|
||||
case 8: { /* pushn rd */ for (int i = rd; i >= 0; i--) push32(e, e->r[i]); break; }
|
||||
case 9: { /* popn rd */ for (int i = 0; i <= rd; i++) e->r[i] = pop32(e); break; }
|
||||
case 11: /* jpr rd */ e->pc = iaddr + e->r[rd];
|
||||
trace_jump(e, e->pc, iaddr); break;
|
||||
case 15: { /* jpr.d */ uint32_t t = iaddr + e->r[rd]; run_slot(e); e->pc = t;
|
||||
trace_jump(e, t, iaddr); break; }
|
||||
case 16: /* brk */ stop(e, iaddr, "brk", 0); break;
|
||||
case 17: /* retd */ stop(e, iaddr, "retd (debug) unsupported", 0); break;
|
||||
case 18: { /* int imm2 */
|
||||
uint32_t vec = 12 + (hw & 3); /* software exceptions int0-3 */
|
||||
((void)0);
|
||||
push32(e, e->pc);
|
||||
push32(e, psr_pack(e));
|
||||
e->f_ie = 0;
|
||||
e->pc = rd32(e, e->ttbr + vec * 4);
|
||||
break; }
|
||||
case 19: { /* reti */ psr_unpack(e, pop32(e)); e->pc = pop32(e);
|
||||
trace_ret(e, e->pc); break; }
|
||||
case 24: /* call rd */ push32(e, e->pc); trace_call(e, e->r[rd], iaddr);
|
||||
e->pc = e->r[rd]; break;
|
||||
case 25: /* ret */ e->pc = pop32(e); trace_ret(e, e->pc); break;
|
||||
case 26: /* jp rd */ e->pc = e->r[rd]; trace_jump(e, e->pc, iaddr); break;
|
||||
case 28: { /* call.d rd */ uint32_t t = e->r[rd]; run_slot(e); push32(e, e->pc);
|
||||
trace_call(e, t, iaddr); e->pc = t; break; }
|
||||
case 29: { /* ret.d */ run_slot(e); e->pc = pop32(e); trace_ret(e, e->pc); break; }
|
||||
case 30: { /* jp.d rd */ uint32_t t = e->r[rd]; run_slot(e); e->pc = t;
|
||||
trace_jump(e, t, iaddr); break; }
|
||||
default: stop(e, iaddr, "bad class0 group", hw); break;
|
||||
}
|
||||
} else if (cnst == 1 && op1 == 0) {
|
||||
int op2 = (hw >> 6) & 7;
|
||||
switch (op2) {
|
||||
case 0: push32(e, e->r[rd]); break;
|
||||
case 1: e->r[rd] = pop32(e); break;
|
||||
case 2: /* pushs */
|
||||
if ((hw & 0xF) == 2) push32(e, e->alr);
|
||||
else if ((hw & 0xF) == 3) { push32(e, e->ahr); push32(e, e->alr); }
|
||||
else stop(e, iaddr, "pushs bad sx", hw);
|
||||
break;
|
||||
case 3: /* pops */
|
||||
if ((hw & 0xF) == 2) e->alr = pop32(e);
|
||||
else if ((hw & 0xF) == 3) { e->alr = pop32(e); e->ahr = pop32(e); }
|
||||
else stop(e, iaddr, "pops bad sx", hw);
|
||||
break;
|
||||
case 7: /* ld.cf: coprocessor flags - none present */
|
||||
e->f_n = e->f_z = e->f_v = e->f_c = 0; break;
|
||||
default: stop(e, iaddr, "bad class0 const1", hw); break;
|
||||
}
|
||||
} else stop(e, iaddr, "bad class0 encoding", hw);
|
||||
} else {
|
||||
/* rel8 branches, c0_op1 4..15 */
|
||||
uint32_t u8 = hw & 0xFF;
|
||||
uint32_t target;
|
||||
if (ext_n == 0) target = iaddr + sext(u8, 8) * 2;
|
||||
else if (ext_n == 1) target = iaddr + ((sext(ext0, 13) << 9) | (u8 << 1));
|
||||
else target = iaddr + (((ext0 & 0x1ff8) << 19) | (ext1 << 9) | (u8 << 1));
|
||||
int taken = cond_true(e, op1);
|
||||
if (in_slot) { ((void)0); return; }
|
||||
if (op1 == 14) { /* call / call.d */
|
||||
if (d) { run_slot(e); push32(e, e->pc); trace_call(e, target, iaddr); e->pc = target; }
|
||||
else { push32(e, e->pc); trace_call(e, target, iaddr); e->pc = target; }
|
||||
} else if (op1 == 15) { /* jp / jp.d */
|
||||
if (d) { run_slot(e); }
|
||||
e->pc = target;
|
||||
} else { /* conditional */
|
||||
if (d) { run_slot(e); }
|
||||
if (taken) e->pc = target;
|
||||
}
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 1: {
|
||||
int op1 = (hw >> 10) & 7, op2 = (hw >> 8) & 3;
|
||||
if (op2 == 3) { /* shifts by DoubleImm5 = imm4+16 */
|
||||
uint32_t n = ((hw >> 4) & 0xF) + 16;
|
||||
switch (op1) {
|
||||
case 0: e->r[rd] = srl_op(e, e->r[rd], n); break;
|
||||
case 1: e->r[rd] = sl_op (e, e->r[rd], n); break;
|
||||
case 2: e->r[rd] = sra_op(e, e->r[rd], n); break;
|
||||
case 3: e->r[rd] = sl_op (e, e->r[rd], n); break; /* sla */
|
||||
case 4: e->r[rd] = rr_op (e, e->r[rd], n); break;
|
||||
case 5: e->r[rd] = rl_op (e, e->r[rd], n); break;
|
||||
default: stop(e, iaddr, "bad class1 shift", hw); break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (op2 == 2) { /* reg-reg ALU; with ext -> rd = rs OP imm */
|
||||
uint32_t imm = (ext_n == 1) ? ext0 : ((ext0 << 13) | ext1);
|
||||
switch (op1) {
|
||||
case 0: e->r[rd] = ext_n ? add_op(e, e->r[rs], imm) : add_op(e, e->r[rd], e->r[rs]); break;
|
||||
case 1: e->r[rd] = ext_n ? sub_op(e, e->r[rs], imm) : sub_op(e, e->r[rd], e->r[rs]); break;
|
||||
case 2: if (ext_n) cmp_op(e, e->r[rs], imm); else cmp_op(e, e->r[rd], e->r[rs]); break;
|
||||
case 3: if (ext_n) { stop(e, iaddr, "ext on ld.w rd,rs", hw); break; }
|
||||
e->r[rd] = e->r[rs]; break;
|
||||
case 4: e->r[rd] = ext_n ? log_flags(e, e->r[rs] & imm) : log_flags(e, e->r[rd] & e->r[rs]); break;
|
||||
case 5: e->r[rd] = ext_n ? log_flags(e, e->r[rs] | imm) : log_flags(e, e->r[rd] | e->r[rs]); break;
|
||||
case 6: e->r[rd] = ext_n ? log_flags(e, e->r[rs] ^ imm) : log_flags(e, e->r[rd] ^ e->r[rs]); break;
|
||||
case 7: if (ext_n) { stop(e, iaddr, "ext on not rd,rs", hw); break; }
|
||||
e->r[rd] = log_flags(e, ~e->r[rs]); break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* op2 0/1: memory ops. ext (op2==0 only) -> [rs+imm], no post-inc */
|
||||
{
|
||||
uint32_t addr = e->r[rs];
|
||||
int postinc = (op2 == 1);
|
||||
if (ext_n) {
|
||||
if (postinc) { stop(e, iaddr, "ext on post-inc load", hw); break; }
|
||||
addr += (ext_n == 1) ? ext0 : ((ext0 << 13) | ext1);
|
||||
}
|
||||
switch (op1) {
|
||||
case 0: e->r[rd] = sext(rd8(e, addr), 8); if (postinc) e->r[rs] += 1; break;
|
||||
case 1: e->r[rd] = rd8(e, addr); if (postinc) e->r[rs] += 1; break;
|
||||
case 2: e->r[rd] = sext(rd16(e, addr), 16); if (postinc) e->r[rs] += 2; break;
|
||||
case 3: e->r[rd] = rd16(e, addr); if (postinc) e->r[rs] += 2; break;
|
||||
case 4: e->r[rd] = rd32(e, addr); if (postinc) e->r[rs] += 4; break;
|
||||
case 5: mem_write8 (e, addr, (uint8_t)e->r[rd]); if (postinc) e->r[rs] += 1; break;
|
||||
case 6: wr16(e, addr, (uint16_t)e->r[rd]); if (postinc) e->r[rs] += 2; break;
|
||||
case 7: wr32(e, addr, e->r[rd]); if (postinc) e->r[rs] += 4; break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
case 2: { /* SP-relative; base form scales by size, ext form does not */
|
||||
int op1 = (hw >> 10) & 7;
|
||||
uint32_t imm6 = (hw >> 4) & 0x3F;
|
||||
uint32_t off;
|
||||
if (ext_n == 0) {
|
||||
static const uint8_t scale[8] = {1,1,2,2,4,1,2,4};
|
||||
off = imm6 * scale[op1];
|
||||
} else if (ext_n == 1) off = (ext0 << 6) | imm6;
|
||||
else off = (ext0 << 19) | (ext1 << 6) | imm6;
|
||||
uint32_t addr = e->sp + off;
|
||||
switch (op1) {
|
||||
case 0: e->r[rd] = sext(rd8(e, addr), 8); break;
|
||||
case 1: e->r[rd] = rd8(e, addr); break;
|
||||
case 2: e->r[rd] = sext(rd16(e, addr), 16); break;
|
||||
case 3: e->r[rd] = rd16(e, addr); break;
|
||||
case 4: e->r[rd] = rd32(e, addr); break;
|
||||
case 5: mem_write8 (e, addr, (uint8_t)e->r[rd]); break;
|
||||
case 6: wr16(e, addr, (uint16_t)e->r[rd]); break;
|
||||
case 7: wr32(e, addr, e->r[rd]); break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 3: { /* imm6 ALU */
|
||||
int op1 = (hw >> 10) & 7;
|
||||
uint32_t imm6 = (hw >> 4) & 0x3F;
|
||||
uint32_t imm;
|
||||
if (ext_n == 0)
|
||||
imm = (op1 == 0 || op1 == 1) ? imm6 : sext(imm6, 6);
|
||||
else if (ext_n == 1) {
|
||||
imm = (ext0 << 6) | imm6;
|
||||
if (!(op1 == 0 || op1 == 1)) imm = sext(imm, 19);
|
||||
} else
|
||||
imm = (ext0 << 19) | (ext1 << 6) | imm6;
|
||||
switch (op1) {
|
||||
case 0: e->r[rd] = add_op(e, e->r[rd], imm); break;
|
||||
case 1: e->r[rd] = sub_op(e, e->r[rd], imm); break;
|
||||
case 2: cmp_op(e, e->r[rd], imm); break;
|
||||
case 3: e->r[rd] = imm; break; /* ld.w / xld.w imm */
|
||||
case 4: e->r[rd] = log_flags(e, e->r[rd] & imm); break;
|
||||
case 5: e->r[rd] = log_flags(e, e->r[rd] | imm); break;
|
||||
case 6: e->r[rd] = log_flags(e, e->r[rd] ^ imm); break;
|
||||
case 7: e->r[rd] = log_flags(e, ~imm); break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 4: {
|
||||
int op1 = (hw >> 10) & 7, op2 = (hw >> 8) & 3;
|
||||
uint32_t imm4 = (hw >> 4) & 0xF;
|
||||
if (op1 == 0) { e->sp += (uint32_t)(hw & 0x3FF) * 4; break; }
|
||||
if (op1 == 1) { e->sp -= (uint32_t)(hw & 0x3FF) * 4; break; }
|
||||
uint32_t amt = (op2 == 0) ? imm4 : e->r[rs];
|
||||
switch (op1) {
|
||||
case 2: e->r[rd] = srl_op(e, e->r[rd], amt); break;
|
||||
case 3: e->r[rd] = sl_op (e, e->r[rd], amt); break;
|
||||
case 4:
|
||||
if (op2 == 2) { uint32_t v = e->r[rs];
|
||||
e->r[rd] = (v << 24) | ((v & 0xFF00) << 8) | ((v >> 8) & 0xFF00) | (v >> 24); }
|
||||
else e->r[rd] = sra_op(e, e->r[rd], amt);
|
||||
break;
|
||||
case 5: e->r[rd] = sl_op(e, e->r[rd], amt); break; /* sla */
|
||||
case 6:
|
||||
if (op2 == 2) { uint32_t v = e->r[rs]; /* swaph */
|
||||
e->r[rd] = ((v & 0x00FF0000) << 8) | ((v >> 8) & 0x00FF0000) |
|
||||
((v & 0x000000FF) << 8) | ((v >> 8) & 0x000000FF); }
|
||||
else e->r[rd] = rr_op(e, e->r[rd], amt);
|
||||
break;
|
||||
case 7: e->r[rd] = rl_op(e, e->r[rd], amt); break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case 5: {
|
||||
int op1 = (hw >> 10) & 7, op2 = (hw >> 8) & 3;
|
||||
switch (op1 * 4 + op2) {
|
||||
case 0*4+0: sreg_write(e, hw & 0xF, e->r[rs], iaddr); break; /* ld.w %sd,%rs */
|
||||
case 0*4+1: e->r[rd] = sext(e->r[rs] & 0xFF, 8); break; /* ld.b rd,rs */
|
||||
case 0*4+2: e->alr = (uint32_t)((int32_t)sext(e->r[rd] & 0xFFFF, 16) *
|
||||
(int32_t)sext(e->r[rs] & 0xFFFF, 16)); break; /* mlt.h */
|
||||
case 1*4+0: e->r[rd] = sreg_read(e, rs, iaddr); break; /* ld.w rd,%ss */
|
||||
case 1*4+1: e->r[rd] = e->r[rs] & 0xFF; break; /* ld.ub */
|
||||
case 1*4+2: e->alr = (e->r[rd] & 0xFFFF) * (e->r[rs] & 0xFFFF); break; /* mltu.h */
|
||||
case 2*4+0: case 3*4+0: case 4*4+0: case 5*4+0: { /* bit ops */
|
||||
uint32_t addr = e->r[rs];
|
||||
if (ext_n) addr += (ext_n == 1) ? ext0 : ((ext0 << 13) | ext1);
|
||||
int pos = hw & 7;
|
||||
uint8_t b = rd8(e, addr);
|
||||
switch (op1) {
|
||||
case 2: e->f_z = ((b >> pos) & 1) == 0; break; /* btst */
|
||||
case 3: wr8(e, addr, b & ~(1u << pos)); break; /* bclr */
|
||||
case 4: wr8(e, addr, b | (1u << pos)); break; /* bset */
|
||||
case 5: wr8(e, addr, b ^ (1u << pos)); break; /* bnot */
|
||||
}
|
||||
break; }
|
||||
case 2*4+1: e->r[rd] = sext(e->r[rs] & 0xFFFF, 16); break; /* ld.h */
|
||||
case 2*4+2: { int64_t p = (int64_t)(int32_t)e->r[rd] * (int32_t)e->r[rs];
|
||||
e->alr = (uint32_t)p; e->ahr = (uint32_t)((uint64_t)p >> 32); break; } /* mlt.w */
|
||||
case 3*4+1: e->r[rd] = e->r[rs] & 0xFFFF; break; /* ld.uh */
|
||||
case 3*4+2: { uint64_t p = (uint64_t)e->r[rd] * e->r[rs];
|
||||
e->alr = (uint32_t)p; e->ahr = (uint32_t)(p >> 32); break; } /* mltu.w */
|
||||
case 4*4+1: case 5*4+1: /* ld.c: coprocessor */
|
||||
((void)0); break;
|
||||
case 6*4+0: e->r[rd] = adc_op(e, e->r[rd], e->r[rs]); break;
|
||||
case 7*4+0: e->r[rd] = sbc_op(e, e->r[rd], e->r[rs]); break;
|
||||
case 7*4+3: { /* class 5 (2): do.c / psrset / psrclr */
|
||||
int op3 = (hw >> 6) & 3, imm5 = hw & 0x1F;
|
||||
if (op3 == 0) { ((void)0); break; }
|
||||
if (op3 == 1 || op3 == 2) {
|
||||
int val = (op3 == 1);
|
||||
switch (imm5) {
|
||||
case 0: e->f_n = val; break;
|
||||
case 1: e->f_z = val; break;
|
||||
case 2: e->f_v = val; break;
|
||||
case 3: e->f_c = val; break;
|
||||
case 4: e->f_ie = val; break;
|
||||
default: ((void)0); break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
stop(e, iaddr, "bad class5(2)", hw); break; }
|
||||
default: stop(e, iaddr, "bad class5 encoding", hw); break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
stop(e, iaddr, "unreachable class", hw);
|
||||
break;
|
||||
}
|
||||
(void)in_slot;
|
||||
}
|
||||
|
||||
void cpu_reset(Emu *e)
|
||||
{
|
||||
memset(e->r, 0, sizeof e->r);
|
||||
e->sp = 0; e->alr = e->ahr = 0;
|
||||
e->ttbr = e->dev.ttbr_reset;
|
||||
e->idir = e->dbbr = 0;
|
||||
e->f_n = e->f_z = e->f_v = e->f_c = e->f_ie = 0;
|
||||
e->il = 0; e->psr_rest = 0;
|
||||
e->ext_count = 0;
|
||||
e->halted = false; e->halt_hard = false;
|
||||
e->stopped = false;
|
||||
e->pc = rd32(e, e->ttbr); /* reset vector */
|
||||
}
|
||||
|
||||
void cpu_step(Emu *e)
|
||||
{
|
||||
if (e->stopped) return;
|
||||
if (e->halted) {
|
||||
e->cycles += 16;
|
||||
if (e->pending_irq || e->wake_req || (!e->halt_hard && e->cycles >= e->wake_at)) {
|
||||
e->halted = false;
|
||||
} else
|
||||
return;
|
||||
}
|
||||
/* interrupt accept point (never between ext and target: ext consumed inside step_one) */
|
||||
if (e->pending_irq && e->f_ie && e->pending_level > e->il) {
|
||||
uint32_t vec = e->pending_irq;
|
||||
push32(e, e->pc);
|
||||
push32(e, psr_pack(e));
|
||||
e->f_ie = 0;
|
||||
e->il = e->pending_level;
|
||||
e->pc = rd32(e, e->ttbr + vec * 4);
|
||||
e->pending_irq = 0;
|
||||
}
|
||||
step_one(e, 0);
|
||||
}
|
||||
@@ -0,0 +1,500 @@
|
||||
/* Fast interpreter for S1C33 (same semantics as tc_cpu.c, which is derived
|
||||
* from tamaemu cpu.c). Hot state lives in local variables so the compiler
|
||||
* keeps it in registers. Verified by lockstep runs against tc_cpu.c. */
|
||||
#include "tc.h"
|
||||
#ifdef ROMSTAT
|
||||
__attribute__((section(".stats"), used)) volatile uint32_t tc_rom_hits[128]; /* stats build: reads per 64 KB guest block */
|
||||
#define ROMHIT(o) (tc_rom_hits[((o) >> 16) & 127]++)
|
||||
#else
|
||||
#define ROMHIT(o) ((void)0)
|
||||
#endif
|
||||
|
||||
#define SEXT(v, bits) ((uint32_t)(((uint32_t)(v) ^ (1u << ((bits) - 1))) - (1u << ((bits) - 1))))
|
||||
|
||||
static inline uint8_t *f_ramp(Emu *e, uint32_t a, uint32_t n)
|
||||
{
|
||||
if (a <= A0RAM_MAX - n) return e->a0ram + a;
|
||||
uint32_t o = a - 0x80000u;
|
||||
if (o <= IVRAM_MAX - n) return e->ivram + o;
|
||||
o = a - 0x84000u;
|
||||
if (o <= DSTRAM_MAX - n) return e->dstram + o;
|
||||
return 0;
|
||||
}
|
||||
#ifdef TC_JIT
|
||||
/* Write to RAM that has already been translated: discard all translations */
|
||||
#ifdef JIT_WHOT
|
||||
/* v0.9.6: a0ram pages whose code keeps getting overwritten (iD L, Connect to Plus Color: code at 0xC00
|
||||
* next to its data) are no longer translated. Before, every write threw away all translations, thousands
|
||||
* of times per second, and the emulator stood still on the device. */
|
||||
uint8_t jit_wcnt[128], jit_nojit[128];
|
||||
uint64_t jit_wwin, jit_nojit_at;
|
||||
#define JIT_WWIN 2000000u /* window for counting (about 0.1 s of guest time) */
|
||||
#define JIT_NOJIT 100000000u /* after about 5 s the pages get another chance */
|
||||
static void jit_cw_note(Emu *e, uint32_t a, uint32_t n)
|
||||
{
|
||||
if (e->cycles - jit_wwin > JIT_WWIN) { memset(jit_wcnt, 0, sizeof jit_wcnt); jit_wwin = e->cycles; }
|
||||
for (uint32_t p = a >> 8; p <= ((a + n - 1) >> 8) && p < 128u; p++)
|
||||
if (e->jit_map[p] && ++jit_wcnt[p] >= 8u) { jit_nojit[p] = 1; jit_wcnt[p] = 0; jit_nojit_at = e->cycles; }
|
||||
}
|
||||
static int jit_nojit_pc(Emu *e, uint32_t pc)
|
||||
{
|
||||
if (!jit_nojit[pc >> 8]) return 0;
|
||||
if (e->cycles - jit_nojit_at > JIT_NOJIT) { memset(jit_nojit, 0, sizeof jit_nojit); return 0; }
|
||||
return 1;
|
||||
}
|
||||
#else
|
||||
#define jit_cw_note(e, a, n) ((void)0)
|
||||
#endif
|
||||
__attribute__((noinline)) static void jit_cw_chk(Emu *e, uint32_t a, uint32_t n)
|
||||
{
|
||||
if (e->jit_map[a >> 8] | e->jit_map[(a + n - 1) >> 8]) { jit_cw_note(e, a, n); e->jit_flush = 1; memset(e->jit_map, 0, sizeof e->jit_map); }
|
||||
}
|
||||
#endif
|
||||
#ifdef TC_STATS
|
||||
void stat_acc(uint32_t a, int w);
|
||||
#define FST(a, w) stat_acc((a), (w))
|
||||
#else
|
||||
#define FST(a, w) ((void)0)
|
||||
#endif
|
||||
static inline uint32_t f_rd8(Emu *e, uint32_t a)
|
||||
{
|
||||
FST(a, 0);
|
||||
uint8_t *p = f_ramp(e, a, 1); if (p) return *p;
|
||||
uint32_t o = a - e->dev.rom_base;
|
||||
if (o < e->dev.rom_size && !e->flash_busy && !e->flash_autoselect) { ROMHIT(o); const uint8_t *b = e->rom_blk[o >> 16]; if (b) return b[o & 0xFFFFu]; }
|
||||
return mem_read8(e, a);
|
||||
}
|
||||
static inline uint32_t f_rd16(Emu *e, uint32_t a)
|
||||
{
|
||||
FST(a, 0);
|
||||
uint8_t *p = f_ramp(e, a, 2); if (p) return (uint32_t)(p[0] | (p[1] << 8));
|
||||
uint32_t o = a - e->dev.rom_base;
|
||||
if (o < e->dev.rom_size && (o & 0xFFFFu) <= 0xFFFEu && !e->flash_busy && !e->flash_autoselect) { ROMHIT(o); const uint8_t *b = e->rom_blk[o >> 16]; if (b) { const uint8_t *q = b + (o & 0xFFFFu); return (uint32_t)(q[0] | (q[1] << 8)); } }
|
||||
return mem_read16(e, a);
|
||||
}
|
||||
static inline uint32_t f_rd32(Emu *e, uint32_t a)
|
||||
{
|
||||
FST(a, 0);
|
||||
uint8_t *p = f_ramp(e, a, 4); if (p) return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
|
||||
uint32_t o = a - e->dev.rom_base;
|
||||
if (o < e->dev.rom_size && (o & 0xFFFFu) <= 0xFFFCu && !e->flash_busy && !e->flash_autoselect) { ROMHIT(o); const uint8_t *b = e->rom_blk[o >> 16]; if (b) { const uint8_t *q = b + (o & 0xFFFFu); return (uint32_t)q[0] | ((uint32_t)q[1] << 8) | ((uint32_t)q[2] << 16) | ((uint32_t)q[3] << 24); } }
|
||||
return mem_read32(e, a);
|
||||
}
|
||||
static inline void f_wr8(Emu *e, uint32_t a, uint32_t v) { uint8_t *p = f_ramp(e, a, 1); if (p) *p = (uint8_t)v; else mem_write8(e, a, (uint8_t)v); }
|
||||
static inline void f_wr16(Emu *e, uint32_t a, uint32_t v) { uint8_t *p = f_ramp(e, a, 2); if (p) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); } else mem_write16(e, a, (uint16_t)v); }
|
||||
static inline void f_wr32(Emu *e, uint32_t a, uint32_t v) { uint8_t *p = f_ramp(e, a, 4); if (p) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); p[2] = (uint8_t)(v >> 16); p[3] = (uint8_t)(v >> 24); } else mem_write32(e, a, v); }
|
||||
|
||||
/* Flags in one word: bit3 N, bit2 Z, bit1 V, bit0 C */
|
||||
#define FN (fl >> 3 & 1)
|
||||
#define FZ (fl >> 2 & 1)
|
||||
#define FV (fl >> 1 & 1)
|
||||
#define FC (fl & 1)
|
||||
#define NZ(r) do { uint32_t _r = (r); fl = (fl & 3u) | ((_r >> 31) << 3) | ((uint32_t)(_r == 0) << 2); } while (0)
|
||||
#define SCARRY(a, b) ((((~((a) ^ (b))) & ((a) ^ ((a) + (b)))) >> 31))
|
||||
#define SBORROW(a, b) (((((a) ^ (b)) & ((a) ^ ((a) - (b)))) >> 31))
|
||||
#define PUSH(v) do { sp -= 4; WR32(sp, (v)); } while (0)
|
||||
#define POP() (sp += 4, f_rd32(e, sp - 4))
|
||||
#define PSR_PACK() ((e->psr_rest & ~0xF1Fu) | FN | (FZ << 1) | (FV << 2) | (FC << 3) | ((uint32_t)e->f_ie << 4) | ((uint32_t)(e->il & 0xF) << 8))
|
||||
#define PSR_UNPACK(v) do { uint32_t _v = (v); fl = ((_v & 1) << 3) | (((_v >> 1) & 1) << 2) | (((_v >> 2) & 1) << 1) | ((_v >> 3) & 1); \
|
||||
e->f_ie = (_v >> 4) & 1; e->il = (_v >> 8) & 0xF; e->psr_rest = _v; chk = 1; } while (0)
|
||||
|
||||
static inline uint32_t op_add(uint32_t a, uint32_t b, uint32_t *fl)
|
||||
{ uint32_t r = a + b; *fl = ((r >> 31) << 3) | ((uint32_t)(r == 0) << 2) | (SCARRY(a, b) << 1) | (uint32_t)(r < a); return r; }
|
||||
static inline uint32_t op_sub(uint32_t a, uint32_t b, uint32_t *fl)
|
||||
{ uint32_t r = a - b; *fl = ((r >> 31) << 3) | ((uint32_t)(r == 0) << 2) | (SBORROW(a, b) << 1) | (uint32_t)(a < b); return r; }
|
||||
#define ADD(a, b) op_add((a), (b), &fl)
|
||||
#define SUB(a, b) op_sub((a), (b), &fl)
|
||||
#define CMP(a, b) ((void)op_sub((a), (b), &fl))
|
||||
#define LOG(r) ({ uint32_t _q = (r); fl = (fl & 1u) | ((_q >> 31) << 3) | ((uint32_t)(_q == 0) << 2); _q; })
|
||||
|
||||
/* Deferred jump effect after the delay slot */
|
||||
enum { D_NONE, D_JP, D_CALL, D_RET };
|
||||
|
||||
static inline int cond_ok(int op, uint32_t fl)
|
||||
{
|
||||
uint32_t fn = fl >> 3 & 1, fz = fl >> 2 & 1, fv = fl >> 1 & 1, fc = fl & 1;
|
||||
switch (op) {
|
||||
case 4: return !fz && fn == fv;
|
||||
case 5: return fn == fv;
|
||||
case 6: return fn != fv;
|
||||
case 7: return fz || fn != fv;
|
||||
case 8: return !fz && !fc;
|
||||
case 9: return !fc;
|
||||
case 10: return fc;
|
||||
case 11: return fz || fc;
|
||||
case 12: return fz;
|
||||
case 13: return !fz;
|
||||
default: return 1;
|
||||
}
|
||||
}
|
||||
|
||||
static int pc_ok(Emu *e, uint32_t a)
|
||||
{
|
||||
const DeviceProfile *d = &e->dev;
|
||||
return a - d->rom_base < d->rom_size || a - d->a0ram_base < d->a0ram_size ||
|
||||
a - d->ivram_base < d->ivram_size || a - d->dstram_base < d->dstram_size;
|
||||
}
|
||||
|
||||
/* Runs until cycles >= ziel (target), or the CPU sleeps or stops. Peripherals
|
||||
* are ticked every TICK cycles. */
|
||||
#define TICK 256
|
||||
__attribute__((minsize)) void tc_exec(Emu *e, uint64_t ziel)
|
||||
{
|
||||
uint32_t pc = e->pc, sp = e->sp;
|
||||
uint32_t fl = ((uint32_t)e->f_n << 3) | ((uint32_t)e->f_z << 2) | ((uint32_t)e->f_v << 1) | e->f_c;
|
||||
int32_t left;
|
||||
#define CYC() (e->evt - (uint64_t)(int64_t)left)
|
||||
#define r (e->r)
|
||||
const uint8_t *fwp = e->fw_ptr; uint32_t fwlo = e->fw_lo, fwlen = e->fw_len;
|
||||
int slot = 0;
|
||||
#define dkind (e->ext_count)
|
||||
#define dtarget (e->ext_val[0])
|
||||
dkind = D_NONE;
|
||||
|
||||
#define SYNC_OUT() do { e->pc = pc; e->sp = sp; e->f_n = FN; e->f_z = FZ; e->f_v = FV; e->f_c = FC; e->cycles = CYC(); } while (0)
|
||||
#define SYNC_IN() do { pc = e->pc; sp = e->sp; fl = ((uint32_t)e->f_n << 3) | ((uint32_t)e->f_z << 2) | ((uint32_t)e->f_v << 1) | e->f_c; left = (int32_t)(e->evt - e->cycles); \
|
||||
fwp = e->fw_ptr; fwlo = e->fw_lo; fwlen = e->fw_len; } while (0)
|
||||
|
||||
/* Write to RAM that has already been translated: discard all translations */
|
||||
#ifdef TC_JIT
|
||||
#define JCW(a, n) do { if ((a) <= A0RAM_MAX - (n)) jit_cw_chk(e, (a), (n)); } while (0)
|
||||
#else
|
||||
#define JCW(a, n) ((void)0)
|
||||
#endif
|
||||
#define WR8(a, v) do { uint32_t _a = (a), _v = (v); uint8_t *_p = f_ramp(e, _a, 1); if (_p) { *_p = (uint8_t)_v; JCW(_a, 1); } \
|
||||
else { SYNC_OUT(); mem_write8(e, _a, (uint8_t)_v); SYNC_IN(); chk = 1; } } while (0)
|
||||
#define WR16(a, v) do { uint32_t _a = (a), _v = (v); uint8_t *_p = f_ramp(e, _a, 2); if (_p) { _p[0] = (uint8_t)_v; _p[1] = (uint8_t)(_v >> 8); JCW(_a, 2); } \
|
||||
else { SYNC_OUT(); mem_write16(e, _a, (uint16_t)_v); SYNC_IN(); chk = 1; } } while (0)
|
||||
#define WR32(a, v) do { uint32_t _a = (a), _v = (v); uint8_t *_p = f_ramp(e, _a, 4); if (_p) { _p[0] = (uint8_t)_v; _p[1] = (uint8_t)(_v >> 8); _p[2] = (uint8_t)(_v >> 16); _p[3] = (uint8_t)(_v >> 24); JCW(_a, 4); } \
|
||||
else { SYNC_OUT(); mem_write32(e, _a, _v); SYNC_IN(); chk = 1; } } while (0)
|
||||
|
||||
#define NEXT_EVT(c) do { uint64_t _n = (e->last_tick + TICK < ziel) ? e->last_tick + TICK : ziel; e->evt = _n; left = (int32_t)(_n - (c)); } while (0)
|
||||
#define FORCE_EVT() do { uint64_t _c = CYC(); e->evt = _c; left = 0; } while (0)
|
||||
NEXT_EVT(e->cycles);
|
||||
int chk = 1; /* check for interrupts (after peripherals, I/O, PSR) */
|
||||
if (e->halted || e->stopped) goto out;
|
||||
for (;;) {
|
||||
int in_slot = slot; slot = 0;
|
||||
if (!in_slot && chk) {
|
||||
chk = 0;
|
||||
if (e->pending_irq && e->f_ie && e->pending_level > e->il) {
|
||||
uint32_t vec = e->pending_irq;
|
||||
PUSH(pc);
|
||||
PUSH(PSR_PACK());
|
||||
e->f_ie = 0;
|
||||
e->il = e->pending_level;
|
||||
pc = f_rd32(e, e->ttbr + vec * 4);
|
||||
e->pending_irq = 0;
|
||||
}
|
||||
}
|
||||
/* Fetch */
|
||||
if (pc - fwlo >= fwlen) {
|
||||
pc &= 0x0FFFFFFFu;
|
||||
if (!pc_ok(e, pc)) { e->stopped = true; FORCE_EVT(); e->stop_pc = pc; e->stop_code = pc; break; }
|
||||
tc_fetch_window(e, pc); fwp = e->fw_ptr; fwlo = e->fw_lo; fwlen = e->fw_len;
|
||||
}
|
||||
#define FETCH(a) ((a) - fwlo < fwlen ? *(const uint16_t *)(fwp + ((a) - fwlo)) : (uint16_t)f_rd16(e, (a)))
|
||||
uint32_t hw = FETCH(pc);
|
||||
int ext_n = 0; uint32_t ext0 = 0, ext1 = 0;
|
||||
while ((hw >> 13) == 6) {
|
||||
if (ext_n == 0) ext0 = hw & 0x1FFF; else if (ext_n == 1) ext1 = hw & 0x1FFF;
|
||||
ext_n++;
|
||||
pc += 2; left--;
|
||||
hw = FETCH(pc);
|
||||
if (ext_n > 2) { e->stopped = true; FORCE_EVT(); e->stop_pc = pc; e->stop_code = hw; goto out; }
|
||||
}
|
||||
if (ext_n > 2) ext_n = 2;
|
||||
uint32_t iaddr = pc;
|
||||
pc += 2; left--;
|
||||
int rd = hw & 0xF, rs = (hw >> 4) & 0xF;
|
||||
|
||||
switch (hw >> 13) {
|
||||
case 0: {
|
||||
int op1 = (hw >> 9) & 0xF, d = (hw >> 8) & 1;
|
||||
if (op1 <= 3) {
|
||||
int cnst = (hw >> 4) & 3;
|
||||
if (cnst == 0) {
|
||||
int op2 = (hw >> 6) & 3;
|
||||
switch (op1 * 8 + d * 4 + op2) {
|
||||
case 0: break;
|
||||
case 1: e->halted = true; e->halt_hard = !(e->cmu.opt & 0x3E00); e->wake_at = CYC() + 20000; FORCE_EVT(); break;
|
||||
case 2: e->halted = true; e->halt_hard = true; FORCE_EVT(); break;
|
||||
case 8: for (int i = rd; i >= 0; i--) PUSH(r[i]); break;
|
||||
case 9: for (int i = 0; i <= rd; i++) r[i] = POP(); break;
|
||||
case 11: pc = iaddr + r[rd]; break;
|
||||
case 15: dkind = D_JP; dtarget = iaddr + r[rd]; slot = 1; break;
|
||||
case 16: e->stopped = true; FORCE_EVT(); e->stop_pc = iaddr; break;
|
||||
case 17: e->stopped = true; FORCE_EVT(); e->stop_pc = iaddr; break;
|
||||
case 18: { uint32_t vec = 12 + (hw & 3);
|
||||
PUSH(pc); PUSH(PSR_PACK()); e->f_ie = 0;
|
||||
pc = f_rd32(e, e->ttbr + vec * 4); break; }
|
||||
case 19: { PSR_UNPACK(POP()); pc = POP(); break; }
|
||||
case 24: { uint32_t t = r[rd]; PUSH(pc); pc = t; break; }
|
||||
case 25: pc = POP(); break;
|
||||
case 26: pc = r[rd]; break;
|
||||
case 28: dkind = D_CALL; dtarget = r[rd]; slot = 1; break;
|
||||
case 29: dkind = D_RET; slot = 1; break;
|
||||
case 30: dkind = D_JP; dtarget = r[rd]; slot = 1; break;
|
||||
default: e->stopped = true; FORCE_EVT(); e->stop_pc = iaddr; e->stop_code = hw; break;
|
||||
}
|
||||
} else if (cnst == 1 && op1 == 0) {
|
||||
switch ((hw >> 6) & 7) {
|
||||
case 0: PUSH(r[rd]); break;
|
||||
case 1: r[rd] = POP(); break;
|
||||
case 2: if ((hw & 0xF) == 2) PUSH(e->alr);
|
||||
else if ((hw & 0xF) == 3) { PUSH(e->ahr); PUSH(e->alr); }
|
||||
else { e->stopped = true; FORCE_EVT(); e->stop_pc = iaddr; } break;
|
||||
case 3: if ((hw & 0xF) == 2) e->alr = POP();
|
||||
else if ((hw & 0xF) == 3) { e->alr = POP(); e->ahr = POP(); }
|
||||
else { e->stopped = true; FORCE_EVT(); e->stop_pc = iaddr; } break;
|
||||
case 7: fl = 0; break;
|
||||
default: e->stopped = true; FORCE_EVT(); e->stop_pc = iaddr; e->stop_code = hw; break;
|
||||
}
|
||||
} else { e->stopped = true; FORCE_EVT(); e->stop_pc = iaddr; e->stop_code = hw; }
|
||||
} else {
|
||||
uint32_t u8 = hw & 0xFF, target;
|
||||
if (ext_n == 0) target = iaddr + SEXT(u8, 8) * 2;
|
||||
else if (ext_n == 1) target = iaddr + ((SEXT(ext0, 13) << 9) | (u8 << 1));
|
||||
else target = iaddr + (((ext0 & 0x1ff8) << 19) | (ext1 << 9) | (u8 << 1));
|
||||
if (in_slot) break; /* branch in a delay slot: ignored, like the original */
|
||||
if (op1 == 14) {
|
||||
if (d) { dkind = D_CALL; dtarget = target; slot = 1; }
|
||||
else { PUSH(pc); pc = target; }
|
||||
} else if (op1 == 15) {
|
||||
if (d) { dkind = D_JP; dtarget = target; slot = 1; } else pc = target;
|
||||
} else {
|
||||
int taken = cond_ok(op1, fl);
|
||||
if (d) { dkind = taken ? D_JP : D_NONE; dtarget = target; slot = 1; }
|
||||
else if (taken) pc = target;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 1: {
|
||||
int op1 = (hw >> 10) & 7, op2 = (hw >> 8) & 3;
|
||||
if (op2 == 3) {
|
||||
uint32_t n = ((hw >> 4) & 0xF) + 16, a = r[rd], q;
|
||||
switch (op1) {
|
||||
case 0: q = a >> n; break;
|
||||
case 1: case 3: q = a << n; break;
|
||||
case 2: q = (uint32_t)((int32_t)a >> n); break;
|
||||
case 4: q = (a >> n) | (a << (32 - n)); break;
|
||||
case 5: q = (a << n) | (a >> (32 - n)); break;
|
||||
default: e->stopped = true; FORCE_EVT(); e->stop_pc = iaddr; q = a; break;
|
||||
}
|
||||
r[rd] = q; NZ(q);
|
||||
break;
|
||||
}
|
||||
if (op2 == 2) {
|
||||
uint32_t imm = (ext_n == 1) ? ext0 : ((ext0 << 13) | ext1);
|
||||
switch (op1) {
|
||||
case 0: r[rd] = ext_n ? ADD(r[rs], imm) : ADD(r[rd], r[rs]); break;
|
||||
case 1: r[rd] = ext_n ? SUB(r[rs], imm) : SUB(r[rd], r[rs]); break;
|
||||
case 2: if (ext_n) CMP(r[rs], imm); else CMP(r[rd], r[rs]); break;
|
||||
case 3: if (ext_n) { e->stopped = true; FORCE_EVT(); e->stop_pc = iaddr; break; } r[rd] = r[rs]; break;
|
||||
case 4: r[rd] = ext_n ? LOG(r[rs] & imm) : LOG(r[rd] & r[rs]); break;
|
||||
case 5: r[rd] = ext_n ? LOG(r[rs] | imm) : LOG(r[rd] | r[rs]); break;
|
||||
case 6: r[rd] = ext_n ? LOG(r[rs] ^ imm) : LOG(r[rd] ^ r[rs]); break;
|
||||
case 7: if (ext_n) { e->stopped = true; FORCE_EVT(); e->stop_pc = iaddr; break; } r[rd] = LOG(~r[rs]); break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
{
|
||||
uint32_t addr = r[rs];
|
||||
int postinc = (op2 == 1);
|
||||
if (ext_n) {
|
||||
if (postinc) { e->stopped = true; FORCE_EVT(); e->stop_pc = iaddr; break; }
|
||||
addr += (ext_n == 1) ? ext0 : ((ext0 << 13) | ext1);
|
||||
}
|
||||
switch (op1) {
|
||||
case 0: r[rd] = SEXT(f_rd8(e, addr), 8); if (postinc) r[rs] += 1; break;
|
||||
case 1: r[rd] = f_rd8(e, addr); if (postinc) r[rs] += 1; break;
|
||||
case 2: r[rd] = SEXT(f_rd16(e, addr), 16); if (postinc) r[rs] += 2; break;
|
||||
case 3: r[rd] = f_rd16(e, addr); if (postinc) r[rs] += 2; break;
|
||||
case 4: r[rd] = f_rd32(e, addr); if (postinc) r[rs] += 4; break;
|
||||
case 5: WR8(addr, r[rd]); if (postinc) r[rs] += 1; break;
|
||||
case 6: WR16(addr, r[rd]); if (postinc) r[rs] += 2; break;
|
||||
case 7: WR32(addr, r[rd]); if (postinc) r[rs] += 4; break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
case 2: {
|
||||
int op1 = (hw >> 10) & 7;
|
||||
uint32_t imm6 = (hw >> 4) & 0x3F, off;
|
||||
if (ext_n == 0) { static const uint8_t scale[8] = {1,1,2,2,4,1,2,4}; off = imm6 * scale[op1]; }
|
||||
else if (ext_n == 1) off = (ext0 << 6) | imm6;
|
||||
else off = (ext0 << 19) | (ext1 << 6) | imm6;
|
||||
uint32_t addr = sp + off;
|
||||
switch (op1) {
|
||||
case 0: r[rd] = SEXT(f_rd8(e, addr), 8); break;
|
||||
case 1: r[rd] = f_rd8(e, addr); break;
|
||||
case 2: r[rd] = SEXT(f_rd16(e, addr), 16); break;
|
||||
case 3: r[rd] = f_rd16(e, addr); break;
|
||||
case 4: r[rd] = f_rd32(e, addr); break;
|
||||
case 5: WR8(addr, r[rd]); break;
|
||||
case 6: WR16(addr, r[rd]); break;
|
||||
case 7: WR32(addr, r[rd]); break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 3: {
|
||||
int op1 = (hw >> 10) & 7;
|
||||
uint32_t imm6 = (hw >> 4) & 0x3F, imm;
|
||||
if (ext_n == 0) imm = (op1 == 0 || op1 == 1) ? imm6 : SEXT(imm6, 6);
|
||||
else if (ext_n == 1) { imm = (ext0 << 6) | imm6; if (!(op1 == 0 || op1 == 1)) imm = SEXT(imm, 19); }
|
||||
else imm = (ext0 << 19) | (ext1 << 6) | imm6;
|
||||
switch (op1) {
|
||||
case 0: r[rd] = ADD(r[rd], imm); break;
|
||||
case 1: r[rd] = SUB(r[rd], imm); break;
|
||||
case 2: CMP(r[rd], imm); break;
|
||||
case 3: r[rd] = imm; break;
|
||||
case 4: r[rd] = LOG(r[rd] & imm); break;
|
||||
case 5: r[rd] = LOG(r[rd] | imm); break;
|
||||
case 6: r[rd] = LOG(r[rd] ^ imm); break;
|
||||
case 7: r[rd] = LOG(~imm); break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 4: {
|
||||
int op1 = (hw >> 10) & 7, op2 = (hw >> 8) & 3;
|
||||
if (op1 == 0) { sp += (uint32_t)(hw & 0x3FF) * 4; break; }
|
||||
if (op1 == 1) { sp -= (uint32_t)(hw & 0x3FF) * 4; break; }
|
||||
uint32_t amt = ((op2 == 0) ? ((hw >> 4) & 0xF) : r[rs]) & 31, a = r[rd], q;
|
||||
switch (op1) {
|
||||
case 2: q = a >> amt; r[rd] = q; NZ(q); break;
|
||||
case 3: case 5: q = a << amt; r[rd] = q; NZ(q); break;
|
||||
case 4:
|
||||
if (op2 == 2) { uint32_t v = r[rs]; r[rd] = (v << 24) | ((v & 0xFF00) << 8) | ((v >> 8) & 0xFF00) | (v >> 24); }
|
||||
else { q = (uint32_t)((int32_t)a >> amt); r[rd] = q; NZ(q); }
|
||||
break;
|
||||
case 6:
|
||||
if (op2 == 2) { uint32_t v = r[rs];
|
||||
r[rd] = ((v & 0x00FF0000) << 8) | ((v >> 8) & 0x00FF0000) | ((v & 0x000000FF) << 8) | ((v >> 8) & 0x000000FF); }
|
||||
else { q = amt ? ((a >> amt) | (a << (32 - amt))) : a; r[rd] = q; NZ(q); }
|
||||
break;
|
||||
case 7: q = amt ? ((a << amt) | (a >> (32 - amt))) : a; r[rd] = q; NZ(q); break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 5: {
|
||||
int op1 = (hw >> 10) & 7, op2 = (hw >> 8) & 3;
|
||||
switch (op1 * 4 + op2) {
|
||||
case 0: { uint32_t v = r[rs];
|
||||
switch (hw & 0xF) {
|
||||
case 0: PSR_UNPACK(v); break;
|
||||
case 1: sp = v; break;
|
||||
case 2: e->alr = v; break;
|
||||
case 3: e->ahr = v; break;
|
||||
case 8: e->ttbr = v; break;
|
||||
case 10: e->idir = v; break;
|
||||
case 11: e->dbbr = v; break;
|
||||
case 15: pc = v; break;
|
||||
default: e->stopped = true; FORCE_EVT(); e->stop_pc = iaddr; break;
|
||||
} break; }
|
||||
case 1: r[rd] = SEXT(r[rs] & 0xFF, 8); break;
|
||||
case 2: e->alr = (uint32_t)((int32_t)SEXT(r[rd] & 0xFFFF, 16) * (int32_t)SEXT(r[rs] & 0xFFFF, 16)); break;
|
||||
case 4: {
|
||||
uint32_t v;
|
||||
switch (rs) {
|
||||
case 0: v = PSR_PACK(); break;
|
||||
case 1: v = sp; break;
|
||||
case 2: v = e->alr; break;
|
||||
case 3: v = e->ahr; break;
|
||||
case 8: v = e->ttbr; break;
|
||||
case 10: v = e->idir; break;
|
||||
case 11: v = e->dbbr; break;
|
||||
case 15: v = iaddr; break;
|
||||
default: e->stopped = true; FORCE_EVT(); e->stop_pc = iaddr; v = 0; break;
|
||||
}
|
||||
r[rd] = v; break; }
|
||||
case 5: r[rd] = r[rs] & 0xFF; break;
|
||||
case 6: e->alr = (r[rd] & 0xFFFF) * (r[rs] & 0xFFFF); break;
|
||||
case 8: case 12: case 16: case 20: {
|
||||
uint32_t addr = r[rs];
|
||||
if (ext_n) addr += (ext_n == 1) ? ext0 : ((ext0 << 13) | ext1);
|
||||
int pos = hw & 7;
|
||||
uint32_t b = f_rd8(e, addr);
|
||||
switch (op1) {
|
||||
case 2: fl = (fl & ~4u) | ((uint32_t)(((b >> pos) & 1) == 0) << 2); break;
|
||||
case 3: WR8(addr, b & ~(1u << pos)); break;
|
||||
case 4: WR8(addr, b | (1u << pos)); break;
|
||||
case 5: WR8(addr, b ^ (1u << pos)); break;
|
||||
}
|
||||
break; }
|
||||
case 9: r[rd] = SEXT(r[rs] & 0xFFFF, 16); break;
|
||||
case 10: { int64_t p = (int64_t)(int32_t)r[rd] * (int32_t)r[rs];
|
||||
e->alr = (uint32_t)p; e->ahr = (uint32_t)((uint64_t)p >> 32); break; }
|
||||
case 13: r[rd] = r[rs] & 0xFFFF; break;
|
||||
case 14: { uint64_t p = (uint64_t)r[rd] * r[rs]; e->alr = (uint32_t)p; e->ahr = (uint32_t)(p >> 32); break; }
|
||||
case 17: case 21: break;
|
||||
case 24: { uint32_t a = r[rd], b = r[rs], cz = FC, s = a + b;
|
||||
uint32_t v = SCARRY(a, b) ^ SCARRY(s, cz), c = (s < a) || ((s + cz) < s);
|
||||
uint32_t q = s + cz; fl = ((q >> 31) << 3) | ((uint32_t)(q == 0) << 2) | (v << 1) | c; r[rd] = q; break; }
|
||||
case 28: { uint32_t a = r[rd], b = r[rs], cz = FC, dd = a - b;
|
||||
uint32_t v = SBORROW(a, b) || SBORROW(dd, cz), c = (a < b) || (dd < cz);
|
||||
uint32_t q = dd - cz; fl = ((q >> 31) << 3) | ((uint32_t)(q == 0) << 2) | (v << 1) | c; r[rd] = q; break; }
|
||||
case 31: {
|
||||
int op3 = (hw >> 6) & 3, imm5 = hw & 0x1F;
|
||||
if (op3 == 0) break;
|
||||
if (op3 == 1 || op3 == 2) {
|
||||
int val = (op3 == 1);
|
||||
switch (imm5) {
|
||||
case 0: fl = (fl & ~8u) | ((uint32_t)val << 3); break;
|
||||
case 1: fl = (fl & ~4u) | ((uint32_t)val << 2); break;
|
||||
case 2: fl = (fl & ~2u) | ((uint32_t)val << 1); break;
|
||||
case 3: fl = (fl & ~1u) | (uint32_t)val; break;
|
||||
case 4: e->f_ie = val; chk = 1; break;
|
||||
default: break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
e->stopped = true; FORCE_EVT(); e->stop_pc = iaddr; break; }
|
||||
default: e->stopped = true; FORCE_EVT(); e->stop_pc = iaddr; e->stop_code = hw; break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
e->stopped = true; FORCE_EVT(); e->stop_pc = iaddr; e->stop_code = hw; break;
|
||||
}
|
||||
if (in_slot) {
|
||||
int k = dkind; dkind = D_NONE;
|
||||
if (k == D_JP) pc = dtarget;
|
||||
else if (k == D_CALL) { PUSH(pc); pc = dtarget; }
|
||||
else if (k == D_RET) pc = POP();
|
||||
}
|
||||
if (left <= 0 && !slot) {
|
||||
uint64_t cyc = CYC();
|
||||
if (e->halted || e->stopped) break;
|
||||
if (cyc - e->last_tick >= TICK) {
|
||||
SYNC_OUT();
|
||||
periph_tick(e, (uint32_t)(cyc - e->last_tick));
|
||||
e->last_tick = cyc;
|
||||
SYNC_IN();
|
||||
chk = 1;
|
||||
}
|
||||
if (cyc >= ziel) break;
|
||||
NEXT_EVT(cyc);
|
||||
}
|
||||
}
|
||||
out:
|
||||
#undef r
|
||||
SYNC_OUT();
|
||||
e->fw_ptr = fwp; e->fw_lo = fwlo; e->fw_len = fwlen;
|
||||
}
|
||||
|
||||
#ifndef TC_WITH_REF
|
||||
void cpu_reset(Emu *e)
|
||||
{
|
||||
memset(e->r, 0, sizeof e->r);
|
||||
e->sp = 0; e->alr = e->ahr = 0;
|
||||
e->ttbr = e->dev.ttbr_reset;
|
||||
e->idir = e->dbbr = 0;
|
||||
e->f_n = e->f_z = e->f_v = e->f_c = e->f_ie = 0;
|
||||
e->il = 0; e->psr_rest = 0;
|
||||
e->ext_count = 0;
|
||||
e->halted = false; e->halt_hard = false;
|
||||
e->stopped = false;
|
||||
e->pc = mem_read32(e, e->ttbr);
|
||||
}
|
||||
#endif
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,90 @@
|
||||
/* LCD controller of the colour Tamagotchi, based on tamaemu lcd.c. Tracks the
|
||||
* changed area so only that part is sent to the Paradise panel. */
|
||||
#include "tc.h"
|
||||
|
||||
static void finish_cmd(Emu *e)
|
||||
{
|
||||
Lcd *l = &e->lcd;
|
||||
switch (l->cmd) {
|
||||
case 0x2A: if (l->argc >= 4) { l->xs = (l->args[0] << 8) | l->args[1];
|
||||
l->xe = (l->args[2] << 8) | l->args[3];
|
||||
l->cx = l->xs; } break;
|
||||
case 0x2B: if (l->argc >= 4) { l->ys = (l->args[0] << 8) | l->args[1];
|
||||
l->ye = (l->args[2] << 8) | l->args[3];
|
||||
l->cy = l->ys; } break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef LCD_ROWS
|
||||
/* v0.8.19: additionally the changed range per row (outside of Emu, so the save format stays the same).
|
||||
* lcd_rall: redraw the whole frame (commands like invert, sleep). */
|
||||
uint8_t lcd_rx0[GRAM_H], lcd_rx1[GRAM_H], lcd_rall = 1;
|
||||
#endif
|
||||
static void mark(Lcd *l, int x, int y)
|
||||
{
|
||||
if (x < l->dx0) l->dx0 = x;
|
||||
if (x > l->dx1) l->dx1 = x;
|
||||
if (y < l->dy0) l->dy0 = y;
|
||||
if (y > l->dy1) l->dy1 = y;
|
||||
#ifdef LCD_ROWS
|
||||
if ((unsigned)y < GRAM_H) { if (x < lcd_rx0[y]) lcd_rx0[y] = (uint8_t)x; if (x > lcd_rx1[y]) lcd_rx1[y] = (uint8_t)x; }
|
||||
#endif
|
||||
}
|
||||
static void mark_all(Lcd *l)
|
||||
{
|
||||
mark(l, 0, 0); mark(l, 127, 127);
|
||||
#ifdef LCD_ROWS
|
||||
lcd_rall = 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
void lcd_cmd(Emu *e, uint8_t c)
|
||||
{
|
||||
Lcd *l = &e->lcd;
|
||||
#ifdef LCDCMD_DEBUG
|
||||
if(c==1||c==0x10||c==0x11||c==0x28||c==0x29) printf("LCDCMD %02x at %.3f\n", c, (double)e->cycles/e->dev.osc3_hz);
|
||||
#endif
|
||||
finish_cmd(e);
|
||||
l->cmd = c; l->argc = 0; l->pix_n = 0;
|
||||
switch (c) {
|
||||
case 0x01: l->madctl = 0; l->sleep_out = false; l->disp_on = false; l->inverted = false;
|
||||
l->xs = 0; l->xe = 131; l->ys = 0; l->ye = 161; break;
|
||||
case 0x10: l->sleep_out = false; mark_all(l); break;
|
||||
case 0x11: l->sleep_out = true; mark_all(l); break;
|
||||
case 0x20: l->inverted = false; mark_all(l); break;
|
||||
case 0x21: l->inverted = true; mark_all(l); break;
|
||||
case 0x28: l->disp_on = false; mark_all(l); break;
|
||||
case 0x29: l->disp_on = true; mark_all(l); break;
|
||||
case 0x2C: l->cx = l->xs; l->cy = l->ys; break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
void lcd_data(Emu *e, uint8_t d)
|
||||
{
|
||||
Lcd *l = &e->lcd;
|
||||
if (l->cmd == 0x2C) {
|
||||
int bpp = ((l->colmod & 0x0F) == 0x05) ? 2 : 3;
|
||||
l->pix_buf[l->pix_n++] = d;
|
||||
if (l->pix_n >= bpp) {
|
||||
uint16_t p;
|
||||
if (bpp == 2) p = (uint16_t)((l->pix_buf[0] << 8) | l->pix_buf[1]);
|
||||
else {
|
||||
uint32_t r6 = l->pix_buf[0] >> 2, g6 = l->pix_buf[1] >> 2, b6 = l->pix_buf[2] >> 2;
|
||||
p = (uint16_t)(((r6 >> 1) << 11) | (g6 << 5) | (b6 >> 1));
|
||||
}
|
||||
l->pix_n = 0;
|
||||
int x = l->cx, y = l->cy;
|
||||
if (x >= 0 && x < GRAM_W && y >= 0 && y < GRAM_H) {
|
||||
if (l->gram[y][x] != p) { l->gram[y][x] = p; mark(l, x, y); }
|
||||
}
|
||||
if (++l->cx > l->xe) { l->cx = l->xs; if (++l->cy > l->ye) l->cy = l->ys; }
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (l->argc < 16) l->args[l->argc] = d;
|
||||
l->argc++;
|
||||
if (l->cmd == 0x36 && l->argc == 1) { l->madctl = d; mark_all(l); }
|
||||
if (l->cmd == 0x3A && l->argc == 1) l->colmod = d;
|
||||
}
|
||||
@@ -0,0 +1,372 @@
|
||||
/* Memory map, based on tamaemu mem.c. ROM reads directly from the Paradise flash,
|
||||
* written sectors are kept as a copy in RAM. */
|
||||
#include "tc.h"
|
||||
#define DEV const DeviceProfile *dev = &e->dev
|
||||
#define FLASH_BUSY_READS 3
|
||||
|
||||
/* Sector for a ROM offset (top 64 KB: 8 KB sectors, otherwise 64 KB). */
|
||||
static void sector_of(Emu *e, uint32_t off, uint32_t *base, uint32_t *size)
|
||||
{
|
||||
DEV;
|
||||
if (dev->flash_top_boot && off >= dev->rom_size - 0x10000) { *base = off & ~0x1FFFu; *size = 0x2000; }
|
||||
else if (!dev->flash_top_boot && off < 0x10000) { *base = off & ~0x1FFFu; *size = 0x2000; }
|
||||
else { *base = off & ~0xFFFFu; *size = 0x10000; }
|
||||
}
|
||||
|
||||
/* Pointer to the current byte (overlay or original). Erased
|
||||
* blocks read 0xFF. */
|
||||
static const uint8_t rom_ff[4] = { 0xFF, 0xFF, 0xFF, 0xFF };
|
||||
/* Start of the stored 8 KB part (0 = erased). */
|
||||
static inline const uint8_t *rom_chunk(Emu *e, uint32_t base)
|
||||
{
|
||||
uint32_t b = (base >> 16) & 127u;
|
||||
if ((e->pg_blk[b >> 5] >> (b & 31u)) & 1u) {
|
||||
uint8_t k = e->pg_map[(base >> 13) & 1023u];
|
||||
return k == 0xFFu ? 0 : e->pg_base + (uint32_t)k * 0x2000u;
|
||||
}
|
||||
const uint8_t *p = e->rom_src[b];
|
||||
return p ? p + (base & 0xE000u) : 0;
|
||||
}
|
||||
#ifdef TC_LZ
|
||||
/* 1.7.3 GUEST_LZ: cold blocks of the firmware packed in 1 KB parts (LZ4 blocks, written by the Studio). A part the guest
|
||||
* reads is unpacked into one of two 1 KB buffers in the RAM at 0x20000400 (free there: 0x20000000..0x400 belongs to the
|
||||
* boot, the mailboxes are at 0x20000FC0). Table: 128 u32 offsets of the block's part list (0 = not packed), per block
|
||||
* 65 u32 starts (same start as the next one = erased part, 1024 bytes = stored as is). Written parts (spare pages) win. */
|
||||
const uint8_t *tc_lz_tab;
|
||||
uint32_t tc_lz_gone[4]; /* packed blocks the guest erased: they read empty */
|
||||
#define LZ_RAM ((uint8_t *)0x20000400u)
|
||||
static uint32_t lz_tag[2], lz_rr; /* part number + 1 in each buffer (0 = empty) */
|
||||
static uint32_t lz_n(const uint8_t **ps, uint32_t n)
|
||||
{
|
||||
if (n == 15u) { uint32_t x; do { x = *(*ps)++; n += x; } while (x == 255u); }
|
||||
return n;
|
||||
}
|
||||
__attribute__((noinline, minsize)) static void lz_dec(uint8_t *d, const uint8_t *s)
|
||||
{
|
||||
uint8_t *end = d + 0x400u;
|
||||
for (;;) {
|
||||
uint32_t t = *s++, n = lz_n(&s, t >> 4);
|
||||
while (n-- && d < end) *d++ = *s++;
|
||||
if (d >= end) return;
|
||||
const uint8_t *m = d - ((uint32_t)s[0] | ((uint32_t)s[1] << 8)); s += 2;
|
||||
n = lz_n(&s, t & 15u) + 4u;
|
||||
while (n-- && d < end) *d++ = *m++;
|
||||
}
|
||||
}
|
||||
/* the 1 KB part at ROM offset off, unpacked (0 = the block is not packed) */
|
||||
__attribute__((noinline, minsize)) static const uint8_t *lz_part(uint32_t off)
|
||||
{
|
||||
uint32_t b = (off >> 16) & 127u, tag = (off >> 10) + 1u;
|
||||
const uint32_t *t = (const uint32_t *)tc_lz_tab;
|
||||
if (!t || ((tc_lz_gone[b >> 5] >> (b & 31u)) & 1u) || !t[b]) return 0;
|
||||
for (uint32_t k = 0; k < 2u; k++) if (lz_tag[k] == tag) return LZ_RAM + (k << 10);
|
||||
const uint32_t *l = (const uint32_t *)(tc_lz_tab + t[b]) + ((off >> 10) & 63u);
|
||||
uint32_t a = l[0], z = l[1], i = lz_rr; lz_rr ^= 1u;
|
||||
uint8_t *d = LZ_RAM + (i << 10);
|
||||
if (z == a) memset(d, 0xFF, 0x400u);
|
||||
else if (z - a >= 0x400u) memcpy(d, tc_lz_tab + a, 0x400u);
|
||||
else lz_dec(d, tc_lz_tab + a);
|
||||
lz_tag[i] = tag;
|
||||
return d;
|
||||
}
|
||||
/* an 8 KB copy for a written sector (0 = the block is not packed) */
|
||||
__attribute__((noinline, minsize)) static int lz_fill(uint8_t *d, uint32_t base)
|
||||
{
|
||||
if (!lz_part(base)) return 0;
|
||||
for (uint32_t k = 0; k < OVL_SIZE; k += 0x400u) memcpy(d + k, lz_part(base + k), 0x400u);
|
||||
return 1;
|
||||
}
|
||||
#endif
|
||||
static inline const uint8_t *rom_src_ptr(Emu *e, uint32_t off)
|
||||
{
|
||||
uint32_t b = (off >> 16) & 127u;
|
||||
if ((e->pg_blk[b >> 5] >> (b & 31u)) & 1u) {
|
||||
const uint8_t *c = rom_chunk(e, off & ~0x1FFFu);
|
||||
#ifdef TC_LZ
|
||||
if (!c) { const uint8_t *z = lz_part(off); if (z) return z + (off & 0x3FFu); }
|
||||
#endif
|
||||
return c ? c + (off & 0x1FFFu) : rom_ff;
|
||||
}
|
||||
const uint8_t *p = e->rom_src[b];
|
||||
return p ? p + (off & 0xFFFFu) : rom_ff;
|
||||
}
|
||||
/* Slow path: parameter block, written copies, spare pages, erased blocks. */
|
||||
__attribute__((noinline)) static const uint8_t *rom_slow(Emu *e, uint32_t off)
|
||||
{
|
||||
for (int i = 0; i < e->ovl.n; i++)
|
||||
if (off - e->ovl.base[i] < OVL_SIZE) return e->ovl.mem[i] + (off - e->ovl.base[i]);
|
||||
return rom_src_ptr(e, off);
|
||||
}
|
||||
/* rom_blk is set only for blocks without written copies (see tc_blk_update). */
|
||||
static inline const uint8_t *rom_ptr(Emu *e, uint32_t off)
|
||||
{
|
||||
const uint8_t *b = e->rom_blk[(off >> 16) & 127u];
|
||||
return b ? b + (off & 0xFFFFu) : rom_slow(e, off);
|
||||
}
|
||||
|
||||
/* Get a writable copy of the sector (create it if needed). Only
|
||||
* 8 KB sectors fit in a slot; writes to larger sectors are lost. */
|
||||
int (*tc_ovl_evict)(Emu *e);
|
||||
#ifdef TC_LZ
|
||||
__attribute__((noinline, minsize)) /* 1.7.3: room for the unpacking (only when the guest writes its flash) */
|
||||
#endif
|
||||
static uint8_t *rom_wptr(Emu *e, uint32_t off)
|
||||
{
|
||||
for (int i = 0; i < e->ovl.n; i++)
|
||||
if (off - e->ovl.base[i] < OVL_SIZE) return e->ovl.mem[i] + (off - e->ovl.base[i]);
|
||||
uint32_t base, size; sector_of(e, off, &base, &size);
|
||||
if (size > OVL_SIZE) {
|
||||
/* 64 KB sector: kept in 8 KB parts like the parameter sectors (v0.9.2). Before, these
|
||||
* writes were lost and the guest could hang or crash (for example when buying an item). */
|
||||
if (!tc_big_erase) { e->flash_lost_writes++; return 0; }
|
||||
base = off & ~(OVL_SIZE - 1u);
|
||||
}
|
||||
for (int pass = 0; pass < 2; pass++) {
|
||||
if (pass && !(tc_ovl_evict && tc_ovl_evict(e))) break;
|
||||
for (int i = 0; i < OVL_SLOTS; i++)
|
||||
if (e->ovl.mem[i] && e->ovl.base[i] == 0xFFFFFFFFu) {
|
||||
e->ovl.base[i] = base;
|
||||
{ const uint8_t *c = rom_chunk(e, base);
|
||||
if (c) memcpy(e->ovl.mem[i], c, OVL_SIZE);
|
||||
#ifdef TC_LZ
|
||||
else if (lz_fill(e->ovl.mem[i], base)) {}
|
||||
#endif
|
||||
else memset(e->ovl.mem[i], 0xFF, OVL_SIZE); }
|
||||
if (i >= e->ovl.n) e->ovl.n = i + 1;
|
||||
if (base - e->par_base >= 0x10000u) {
|
||||
uint32_t b = (base >> 16) & 127u;
|
||||
e->ovl_blk[b >> 5] |= 1u << (b & 31u);
|
||||
tc_blk_update(e, b);
|
||||
}
|
||||
#ifdef TC_JIT
|
||||
else jit_rom_update(e, base);
|
||||
#endif
|
||||
return e->ovl.mem[i] + (off - base);
|
||||
}
|
||||
}
|
||||
e->flash_lost_writes++;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int (*tc_big_erase)(Emu *e, uint32_t base);
|
||||
/* Fast read pointers of a 64 KB block: direct only while no written copy exists. */
|
||||
__attribute__((noinline)) void tc_blk_update(Emu *e, uint32_t b)
|
||||
{
|
||||
b &= 127u;
|
||||
if ((b << 16) - e->par_base < 0x10000u) return;
|
||||
int busy = (int)(((e->ovl_blk[b >> 5] | e->pg_blk[b >> 5]) >> (b & 31u)) & 1u);
|
||||
e->rom_blk[b] = busy ? 0 : e->rom_src[b];
|
||||
e->fw_len = 0; e->fw_lo = 0xFFFFFFFFu;
|
||||
#ifdef TC_JIT
|
||||
jit_rom_update(e, b << 16);
|
||||
e->jit_flush = 1; /* translated code may have read this block */
|
||||
#endif
|
||||
}
|
||||
__attribute__((noinline)) void tc_ovl_free(Emu *e, int i)
|
||||
{
|
||||
uint32_t base = e->ovl.base[i];
|
||||
if (base == 0xFFFFFFFFu) return;
|
||||
e->ovl.base[i] = 0xFFFFFFFFu;
|
||||
e->fw_len = 0; e->fw_lo = 0xFFFFFFFFu;
|
||||
if (base - e->par_base < 0x10000u) {
|
||||
#ifdef TC_JIT
|
||||
jit_rom_update(e, base);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
uint32_t b = (base >> 16) & 127u;
|
||||
for (int k = 0; k < e->ovl.n; k++) if (e->ovl.base[k] != 0xFFFFFFFFu && ((e->ovl.base[k] >> 16) & 127u) == b) return;
|
||||
e->ovl_blk[b >> 5] &= ~(1u << (b & 31u));
|
||||
tc_blk_update(e, b);
|
||||
}
|
||||
static void flash_erase_sector(Emu *e, uint32_t off)
|
||||
{
|
||||
uint32_t base, size; sector_of(e, off, &base, &size);
|
||||
uint8_t *p = 0;
|
||||
if (size > OVL_SIZE) {
|
||||
/* drop the written copies of this sector, then erase the backing store */
|
||||
for (int i = 0; i < e->ovl.n; i++) if (e->ovl.base[i] - base < size) tc_ovl_free(e, i);
|
||||
e->flash_big_ops++;
|
||||
if (!(tc_big_erase && tc_big_erase(e, base))) e->flash_lost_writes++;
|
||||
tc_blk_update(e, base >> 16);
|
||||
} else p = rom_wptr(e, base);
|
||||
if (p) memset(p, 0xFF, size);
|
||||
e->flash_dirty = true;
|
||||
e->flash_busy = FLASH_BUSY_READS; e->fw_len = 0; e->fw_lo = 0xFFFFFFFFu;
|
||||
e->flash_op_data = 0xFFFF;
|
||||
e->flash_toggle = false;
|
||||
}
|
||||
|
||||
static uint16_t flash_status(Emu *e)
|
||||
{
|
||||
e->flash_toggle = !e->flash_toggle;
|
||||
uint16_t s = 0;
|
||||
if (!((e->flash_op_data >> 7) & 1)) s |= 0x80;
|
||||
if (e->flash_toggle) s |= 0x40;
|
||||
if (--e->flash_busy <= 0) e->flash_busy = 0;
|
||||
return s;
|
||||
}
|
||||
|
||||
static void flash_write(Emu *e, uint32_t a, uint32_t v, int size)
|
||||
{
|
||||
DEV;
|
||||
uint32_t off = a - dev->rom_base;
|
||||
uint8_t d = (uint8_t)v;
|
||||
if (e->flash_state == 3) {
|
||||
uint16_t final = 0;
|
||||
for (int i = 0; i < size; i++) {
|
||||
uint32_t o = (off + (uint32_t)i) & (dev->rom_size - 1);
|
||||
uint8_t *p = rom_wptr(e, o);
|
||||
uint8_t nb = p ? (*p &= (uint8_t)(v >> (8 * i))) : rom_ptr(e, o)[0];
|
||||
final |= (uint16_t)nb << (8 * i);
|
||||
}
|
||||
e->flash_dirty = true;
|
||||
e->flash_state = 0;
|
||||
e->flash_busy = FLASH_BUSY_READS; e->fw_len = 0; e->fw_lo = 0xFFFFFFFFu;
|
||||
e->flash_op_data = size >= 2 ? final : (uint16_t)(0xFF00 | final);
|
||||
e->flash_toggle = false;
|
||||
return;
|
||||
}
|
||||
if (d == 0xF0) { e->flash_state = 0; e->flash_autoselect = false; return; }
|
||||
switch (e->flash_state) {
|
||||
case 0: if (d == 0xAA) e->flash_state = 1; break;
|
||||
case 1: e->flash_state = (d == 0x55) ? 2 : 0; break;
|
||||
case 2:
|
||||
if (d == 0xA0) e->flash_state = 3;
|
||||
else if (d == 0x80) e->flash_state = 4;
|
||||
else if (d == 0x90) { e->flash_autoselect = true; e->flash_state = 0; e->fw_len = 0; e->fw_lo = 0xFFFFFFFFu; }
|
||||
else e->flash_state = 0;
|
||||
break;
|
||||
case 4: e->flash_state = (d == 0xAA) ? 5 : 0; break;
|
||||
case 5: e->flash_state = (d == 0x55) ? 6 : 0; break;
|
||||
case 6:
|
||||
if (d == 0x30) flash_erase_sector(e, off);
|
||||
e->flash_state = 0; /* chip erase (0x10) is ignored */
|
||||
break;
|
||||
default: e->flash_state = 0; break;
|
||||
}
|
||||
}
|
||||
|
||||
static uint8_t flash_read8(Emu *e, uint32_t a)
|
||||
{
|
||||
DEV;
|
||||
if (e->flash_busy) return (uint8_t)flash_status(e);
|
||||
if (e->flash_autoselect) {
|
||||
uint32_t w = (a - dev->rom_base) >> 1;
|
||||
if ((w & 0xFF) == 0) return (a & 1) ? 0x00 : 0x01;
|
||||
return (a & 1) ? 0x22 : 0xF6;
|
||||
}
|
||||
return *rom_ptr(e, a - dev->rom_base);
|
||||
}
|
||||
|
||||
#ifdef LCDRD_DEBUG
|
||||
extern uint32_t g_lcdrd, g_lcdrd_pc[16]; extern int g_np;
|
||||
#endif
|
||||
uint8_t mem_read8(Emu *e, uint32_t a)
|
||||
{
|
||||
DEV;
|
||||
#ifdef LCDRD_DEBUG
|
||||
if (a == dev->lcd_cmd_addr || a == dev->lcd_data_addr) { g_lcdrd++; int k; for (k = 0; k < g_np && g_lcdrd_pc[k] != e->pc; k++); if (k == g_np && g_np < 16) g_lcdrd_pc[g_np++] = e->pc; }
|
||||
#endif
|
||||
if (a - dev->rom_base < dev->rom_size) {
|
||||
if (e->flash_busy || e->flash_autoselect) return flash_read8(e, a);
|
||||
return *rom_ptr(e, a - dev->rom_base);
|
||||
}
|
||||
if (a - dev->a0ram_base < dev->a0ram_size) return e->a0ram[a - dev->a0ram_base];
|
||||
if (a - dev->io_base < dev->io_size) return periph_read8(e, a);
|
||||
if (a - dev->ivram_base < dev->ivram_size) return e->ivram[a - dev->ivram_base];
|
||||
if (a - dev->dstram_base < dev->dstram_size) return e->dstram[a - dev->dstram_base];
|
||||
return 0;
|
||||
}
|
||||
|
||||
void mem_write8(Emu *e, uint32_t a, uint8_t v)
|
||||
{
|
||||
DEV;
|
||||
if (a - dev->a0ram_base < dev->a0ram_size) { e->a0ram[a - dev->a0ram_base] = v; return; }
|
||||
if (a == dev->lcd_cmd_addr) { e->lcd_writes++; lcd_cmd(e, v); return; }
|
||||
if (a == dev->lcd_data_addr) { e->lcd_writes++; lcd_data(e, v); return; }
|
||||
if (a - dev->io_base < dev->io_size) { periph_write8(e, a, v); return; }
|
||||
if (a - dev->ivram_base < dev->ivram_size) { e->ivram[a - dev->ivram_base] = v; return; }
|
||||
if (a - dev->dstram_base < dev->dstram_size) { e->dstram[a - dev->dstram_base] = v; return; }
|
||||
if (a - dev->rom_base < dev->rom_size) { flash_write(e, a, v, 1); return; }
|
||||
}
|
||||
|
||||
uint16_t mem_read16(Emu *e, uint32_t a)
|
||||
{
|
||||
DEV;
|
||||
if (a - dev->rom_base < dev->rom_size - 1) {
|
||||
if (e->flash_busy) return flash_status(e);
|
||||
if (e->flash_autoselect)
|
||||
return (uint16_t)(flash_read8(e, a) | (flash_read8(e, a + 1) << 8));
|
||||
const uint8_t *p = rom_ptr(e, a - dev->rom_base);
|
||||
return (uint16_t)(p[0] | (p[1] << 8));
|
||||
}
|
||||
if (a - dev->a0ram_base < dev->a0ram_size - 1) {
|
||||
const uint8_t *p = e->a0ram + (a - dev->a0ram_base);
|
||||
return (uint16_t)(p[0] | (p[1] << 8));
|
||||
}
|
||||
return (uint16_t)(mem_read8(e, a) | (mem_read8(e, a + 1) << 8));
|
||||
}
|
||||
|
||||
uint32_t mem_read32(Emu *e, uint32_t a)
|
||||
{
|
||||
DEV;
|
||||
if (a - dev->rom_base < dev->rom_size - 3) {
|
||||
if (e->flash_busy || e->flash_autoselect)
|
||||
return (uint32_t)mem_read16(e, a) | ((uint32_t)mem_read16(e, a + 2) << 16);
|
||||
const uint8_t *p = rom_ptr(e, a - dev->rom_base);
|
||||
return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
|
||||
}
|
||||
if (a - dev->a0ram_base < dev->a0ram_size - 3) {
|
||||
const uint8_t *p = e->a0ram + (a - dev->a0ram_base);
|
||||
return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
|
||||
}
|
||||
if (a - dev->io_base < dev->io_size - 3) return periph_read32(e, a);
|
||||
return (uint32_t)mem_read16(e, a) | ((uint32_t)mem_read16(e, a + 2) << 16);
|
||||
}
|
||||
|
||||
void mem_write16(Emu *e, uint32_t a, uint16_t v)
|
||||
{
|
||||
DEV;
|
||||
if (a - dev->a0ram_base < dev->a0ram_size - 1) {
|
||||
e->a0ram[a - dev->a0ram_base] = (uint8_t)v;
|
||||
e->a0ram[a - dev->a0ram_base + 1] = (uint8_t)(v >> 8);
|
||||
return;
|
||||
}
|
||||
if (a - dev->rom_base < dev->rom_size - 1) { flash_write(e, a, v, 2); return; }
|
||||
mem_write8(e, a, (uint8_t)v);
|
||||
mem_write8(e, a + 1, (uint8_t)(v >> 8));
|
||||
}
|
||||
|
||||
void mem_write32(Emu *e, uint32_t a, uint32_t v)
|
||||
{
|
||||
DEV;
|
||||
if (a - dev->a0ram_base < dev->a0ram_size - 3) {
|
||||
uint8_t *p = e->a0ram + (a - dev->a0ram_base);
|
||||
p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); p[2] = (uint8_t)(v >> 16); p[3] = (uint8_t)(v >> 24);
|
||||
return;
|
||||
}
|
||||
if (a - dev->io_base < dev->io_size - 3) { periph_write32(e, a, v); return; }
|
||||
if (a - dev->rom_base < dev->rom_size - 3) {
|
||||
flash_write(e, a, v & 0xFFFF, 2);
|
||||
flash_write(e, a + 2, v >> 16, 2);
|
||||
return;
|
||||
}
|
||||
mem_write16(e, a, (uint16_t)v);
|
||||
mem_write16(e, a + 2, (uint16_t)(v >> 16));
|
||||
}
|
||||
|
||||
/* Set the instruction window for fast fetch (or clear it if none fits). */
|
||||
void tc_fetch_window(Emu *e, uint32_t pc)
|
||||
{
|
||||
DEV;
|
||||
e->fw_len = 0; e->fw_lo = 0xFFFFFFFFu;
|
||||
if (pc - dev->rom_base < dev->rom_size && !e->flash_busy && !e->flash_autoselect) {
|
||||
/* 64 KB window of the block */
|
||||
const uint8_t *b = e->rom_blk[(pc - dev->rom_base) >> 16];
|
||||
if (!b) return;
|
||||
e->fw_ptr = b; e->fw_lo = pc & ~0xFFFFu; e->fw_len = 0x10000u; return; }
|
||||
if (pc - dev->a0ram_base < dev->a0ram_size) { e->fw_ptr = e->a0ram; e->fw_lo = dev->a0ram_base; e->fw_len = dev->a0ram_size; return; }
|
||||
if (pc - dev->ivram_base < dev->ivram_size) { e->fw_ptr = e->ivram; e->fw_lo = dev->ivram_base; e->fw_len = dev->ivram_size; return; }
|
||||
if (pc - dev->dstram_base < dev->dstram_size) { e->fw_ptr = e->dstram; e->fw_lo = dev->dstram_base; e->fw_len = dev->dstram_size; return; }
|
||||
}
|
||||
@@ -0,0 +1,583 @@
|
||||
/* Peripherals based on tamaemu periph.c, without infrared/NFC/sound protocols and
|
||||
* without floating point. Time comes from counted cycles. */
|
||||
#include "tc.h"
|
||||
#define CMU_BASE 0x301B00u
|
||||
#define IOB(a) e->ioram[(a) - e->dev.io_base]
|
||||
#define IR_BASE 0x300B10u
|
||||
#define IR_RXDATA (IR_BASE + 1u)
|
||||
#define IR_STAT (IR_BASE + 2u)
|
||||
#define IR_LAST (IR_BASE + 7u)
|
||||
#define SIO_RX 0x301700u
|
||||
#define SIO_STAT 0x301714u
|
||||
|
||||
static uint32_t ioram_r32(Emu *e, uint32_t a)
|
||||
{
|
||||
uint8_t *p = e->ioram + (a - e->dev.io_base);
|
||||
return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24);
|
||||
}
|
||||
static void ioram_w32(Emu *e, uint32_t a, uint32_t v)
|
||||
{
|
||||
uint8_t *p = e->ioram + (a - e->dev.io_base);
|
||||
p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); p[2] = (uint8_t)(v >> 16); p[3] = (uint8_t)(v >> 24);
|
||||
}
|
||||
|
||||
TC_SRAM static void cmu_recalc(Emu *e)
|
||||
{
|
||||
Cmu *c = &e->cmu;
|
||||
uint32_t oscsel = (c->sccr >> 2) & 3;
|
||||
uint32_t osc3div = (c->sccr >> 8) & 7;
|
||||
uint32_t mclkdiv = (c->sccr >> 12) & 1;
|
||||
uint32_t pllindiv = (c->sccr >> 20) & 0xF;
|
||||
uint32_t pllpowr = c->pllc & 1;
|
||||
uint32_t plln = (c->pllc >> 4) & 0xF;
|
||||
uint32_t f;
|
||||
if (oscsel == 1) f = 32768u;
|
||||
else if (oscsel == 3 && pllpowr) f = c->osc3_hz / (pllindiv + 1) * (plln + 1);
|
||||
else f = c->osc3_hz >> osc3div;
|
||||
if (mclkdiv) f >>= 1;
|
||||
c->mclk_hz = f ? f : 1;
|
||||
}
|
||||
|
||||
static uint32_t mclk(Emu *e) { return e->cmu.mclk_hz ? e->cmu.mclk_hz : e->dev.osc3_hz; }
|
||||
|
||||
TC_SRAM static void cmu_write32(Emu *e, uint32_t a, uint32_t v)
|
||||
{
|
||||
Cmu *c = &e->cmu;
|
||||
uint32_t off = a - CMU_BASE;
|
||||
if (off == 0x24) { c->unlocked = ((v & 0xFF) == 0x96); ioram_w32(e, a, v); return; }
|
||||
if (off <= 0x14 && !c->unlocked) return;
|
||||
ioram_w32(e, a, v);
|
||||
switch (off) {
|
||||
case 0x08: c->sccr = v; cmu_recalc(e); break;
|
||||
case 0x0C: c->pllc = v; cmu_recalc(e); break;
|
||||
case 0x14: c->opt = v; break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
static void itc_scan(Emu *e);
|
||||
|
||||
/* ---- PN512 NFC chip of the 4U/4U+ (1.6.8) --------------------------------------------------
|
||||
* The 4U firmware talks to its PN512 over the IR UART (0x300B10 TX, 0x300B11 RX) all the time: it
|
||||
* wakes the chip, arms field detection and waits for answers. Without a chip every read ran into
|
||||
* a long spin timeout, the firmware hardly ever went idle and buttons felt slow (two presses).
|
||||
* Like tamaemu's pn512.c, without radio: a register file, commands finish at once, every write is
|
||||
* acknowledged with its address, a read returns the register. Not saved with the session (the chip
|
||||
* powers up again). */
|
||||
#define IR_TRIG (IR_BASE + 5u)
|
||||
static struct { uint8_t reg[0x40], q[16], fifo_n, q_n, wr, tx; } g_pn;
|
||||
static const uint8_t pn_init[] = { 0x01, 0x20, 0x11, 0x3F, 0x14, 0x80, 0x15, 0x40, 0x18, 0x84, 0x19, 0x4D, 0x1F, 0xEB, 0x26, 0x48, 0x27, 0x88, 0x37, 0x82 };
|
||||
TC_SRAM static void pn_host_byte(Emu *e, uint8_t b)
|
||||
{
|
||||
(void)e;
|
||||
uint8_t *r = g_pn.reg, a = g_pn.wr & 0x3F, v;
|
||||
if (!r[0x37]) for (unsigned i = 0; i < sizeof pn_init; i += 2) r[pn_init[i]] = pn_init[i + 1]; /* power up */
|
||||
if (g_pn.wr) { /* register write, acknowledged with the address */
|
||||
g_pn.wr = 0; v = b;
|
||||
if (a == 0x01) { /* Command: everything finishes at once */
|
||||
uint8_t c = v & 0x0F;
|
||||
if (c == 0x0F) { r[0x37] = 0; g_pn.fifo_n = 0; } /* SoftReset: power-up values again */
|
||||
else r[1] = (uint8_t)((v & 0x30) | ((c == 0x0C || c == 0x08 || c == 0x0D) ? c : 0));
|
||||
if (c == 0x03) r[5] |= 0x04; /* CalcCRC done */
|
||||
if (c == 0x04) r[4] |= 0x50; /* Transmit: Tx, Idle */
|
||||
} else if (a == 0x04 || a == 0x05) { if (v & 0x80) r[a] |= v & 0x7F; else r[a] &= (uint8_t)~v; }
|
||||
else if (a == 0x09) { if (g_pn.fifo_n < 64) g_pn.fifo_n++; }
|
||||
else if (a == 0x0A) { if (v & 0x80) g_pn.fifo_n = 0; }
|
||||
else if (a == 0x0C) { if (v & 0x40) r[4] |= 0x01; if (v & 0x20) g_pn.fifo_n = 10; r[a] = v & 0x1F; }
|
||||
else if (a < 0x06 || a > 0x08) r[a] = v;
|
||||
} else if (b & 0x80) { /* register read */
|
||||
a = b & 0x3F; v = r[a];
|
||||
if (a == 0x09) { v = (uint8_t)(0xF0 ^ g_pn.fifo_n); if (g_pn.fifo_n) g_pn.fifo_n--; } /* FIFO: some ID bytes */
|
||||
else if (a == 0x0A) v = g_pn.fifo_n;
|
||||
else if (a == 0x07) v = 0x60;
|
||||
else if (a == 0x08) v = ((r[1] & 0x0F) == 0x0C || (r[1] & 0x0F) == 0x08) ? 0x05 : 0x00;
|
||||
a = v;
|
||||
} else { g_pn.wr = (uint8_t)(0x40 | b); return; }
|
||||
if (g_pn.q_n < sizeof g_pn.q) g_pn.q[g_pn.q_n++] = a;
|
||||
g_pn.tx = 1;
|
||||
}
|
||||
TC_SRAM static void pn_tick(Emu *e)
|
||||
{
|
||||
uint8_t f = 0;
|
||||
if (g_pn.tx) { g_pn.tx = 0; f = 0x20; } /* TX done */
|
||||
if (g_pn.q_n && IOB(IR_TRIG) && !(IOB(IR_STAT) & 0x01u)) { /* next answer byte, RX */
|
||||
IOB(IR_RXDATA) = g_pn.q[0]; g_pn.q_n--;
|
||||
for (unsigned i = 0; i < g_pn.q_n; i++) g_pn.q[i] = g_pn.q[i + 1];
|
||||
IOB(IR_STAT) |= 0x01u; f |= 0x10;
|
||||
}
|
||||
if (f) { IOB(0x300286) |= f; itc_scan(e); }
|
||||
}
|
||||
|
||||
#ifdef TC_IRLINK
|
||||
/* ---- 1.6.9: infrared to another Paradise ---------------------------------------------------------
|
||||
* As tamaemu's periph.c (ir_tx_byte, ir_rx_poll, the session gating of the P's and the peer-detect flag),
|
||||
* but the bytes go to the platform (tc_ir_out) and come from it (tc_ir_avail, tc_ir_in) instead of
|
||||
* tamaemu's link.c (no time stamps or collisions: the two devices pass the bytes on at once). State outside
|
||||
* Emu, so saved sessions stay valid. Off until tc_ir_setup (then also the TX-ready bit follows the byte time). */
|
||||
#define IR_BAUD_LO (IR_BASE + 6u)
|
||||
#define IR_BAUD_HI (IR_BASE + 7u)
|
||||
#define SIO_TX 0x301704u
|
||||
#define IR_CTX_STATE 0x4E4u
|
||||
int tc_ir_avail(void);
|
||||
int tc_ir_in(void);
|
||||
void tc_ir_out(uint8_t b);
|
||||
static struct { uint64_t tx_ready_at, rx_next_at; uint32_t mode_flag, ctx_ptr; uint8_t on; } g_ir;
|
||||
void tc_ir_setup(uint32_t mode_flag, uint32_t ctx_ptr) { g_ir.mode_flag = mode_flag; g_ir.ctx_ptr = ctx_ptr; g_ir.on = 1; }
|
||||
/* 8N1 byte time: the IR divisor clock is 576 kHz (tamaemu: base / (div + 1), 300..1000000 baud, else 115200) */
|
||||
TC_SRAM static uint64_t ir_byte_cycles(Emu *e)
|
||||
{
|
||||
uint32_t div = (uint32_t)IOB(IR_BAUD_LO) | ((uint32_t)(IOB(IR_BAUD_HI) & 0xF) << 16);
|
||||
uint32_t c = div + 1u > 1920u ? mclk(e) / 11520u : mclk(e) / 1000u * 10u * (div + 1u) / 576u; /* 32 bit: no 64-bit division on the device */
|
||||
return c ? c : 1u;
|
||||
}
|
||||
TC_SRAM static void ir_raise(Emu *e, int bit) { IOB(0x300286) |= (uint8_t)(1u << bit); itc_scan(e); }
|
||||
TC_SRAM static void ir_tx_done_raise(Emu *e)
|
||||
{
|
||||
uint8_t en = IOB(0x300276);
|
||||
if (en & 0x20) ir_raise(e, 5); else if (en & 0x08) ir_raise(e, 3); else if (en & 0x10) ir_raise(e, 4);
|
||||
}
|
||||
TC_SRAM static void ir_tx_byte(Emu *e, uint8_t b)
|
||||
{
|
||||
tc_ir_out(b);
|
||||
IOB(IR_STAT) &= (uint8_t)~0x20u; /* TX busy off, TX ready after one byte time */
|
||||
g_ir.tx_ready_at = e->cycles + ir_byte_cycles(e);
|
||||
}
|
||||
TC_SRAM static uint8_t ram_peek8(Emu *e, uint32_t a)
|
||||
{
|
||||
const DeviceProfile *d = &e->dev;
|
||||
if (a - d->a0ram_base < d->a0ram_size) return e->a0ram[a - d->a0ram_base];
|
||||
if (a - d->ivram_base < d->ivram_size) return e->ivram[a - d->ivram_base];
|
||||
if (a - d->dstram_base < d->dstram_size) return e->dstram[a - d->dstram_base];
|
||||
return 0;
|
||||
}
|
||||
/* session state: a byte (P's: 2 visit/connect, 1 item exchange) or one reached through a context pointer (iD L) */
|
||||
TC_SRAM static uint8_t ir_session(Emu *e)
|
||||
{
|
||||
if (g_ir.mode_flag) return ram_peek8(e, g_ir.mode_flag);
|
||||
if (!g_ir.ctx_ptr) return 0;
|
||||
uint32_t p = g_ir.ctx_ptr, c = (uint32_t)ram_peek8(e, p) | (uint32_t)ram_peek8(e, p + 1) << 8 | (uint32_t)ram_peek8(e, p + 2) << 16 | (uint32_t)ram_peek8(e, p + 3) << 24;
|
||||
const DeviceProfile *d = &e->dev;
|
||||
if (!c || (c - d->a0ram_base >= d->a0ram_size && c - d->ivram_base >= d->ivram_size && c - d->dstram_base >= d->dstram_size)) return 0;
|
||||
return ram_peek8(e, c + IR_CTX_STATE);
|
||||
}
|
||||
TC_SRAM static void ir_rx_poll(Emu *e)
|
||||
{
|
||||
/* receiver not armed (or the P's not in a session): the byte is lost, but the peer-detect flag rises */
|
||||
if ((g_ir.mode_flag && !ir_session(e)) || !IOB(IR_TRIG)) {
|
||||
while (tc_ir_avail()) {
|
||||
(void)tc_ir_in();
|
||||
if (ir_session(e) != 1) { IOB(0x300287) |= 0x08u; itc_scan(e); }
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (e->cycles < g_ir.rx_next_at) return;
|
||||
if ((IOB(IR_STAT) & 0x01u) && e->cycles < g_ir.rx_next_at + 64u * ir_byte_cycles(e)) return; /* unread: wait, then overrun */
|
||||
if (!tc_ir_avail()) return;
|
||||
uint8_t b = (uint8_t)tc_ir_in();
|
||||
IOB(SIO_RX) = b; IOB(IR_RXDATA) = b;
|
||||
IOB(IR_STAT) |= 0x01u;
|
||||
g_ir.rx_next_at = e->cycles + ir_byte_cycles(e); /* one byte per byte time */
|
||||
ir_raise(e, 4);
|
||||
}
|
||||
TC_SRAM static void ir_tick(Emu *e)
|
||||
{
|
||||
if (g_ir.tx_ready_at && e->cycles >= g_ir.tx_ready_at) { g_ir.tx_ready_at = 0; ir_tx_done_raise(e); }
|
||||
if (!e->dev.ir_gpio && !e->dev.nfc_pn512) ir_rx_poll(e);
|
||||
}
|
||||
TC_SRAM static void ir_write8(Emu *e, uint32_t a, uint8_t v)
|
||||
{
|
||||
uint8_t old_trig = IOB(IR_TRIG);
|
||||
e->ioram[a - e->dev.io_base] = v;
|
||||
if (a == IR_BASE) ir_tx_byte(e, v);
|
||||
if (a == IR_TRIG && v && !old_trig) g_ir.rx_next_at = e->cycles + ir_byte_cycles(e); /* receiver start: one byte time */
|
||||
}
|
||||
#endif
|
||||
|
||||
uint32_t g_tc_rtc_wr;
|
||||
void periph_write32(Emu *e, uint32_t a, uint32_t v)
|
||||
{
|
||||
#ifdef TC_IRLINK
|
||||
if (g_ir.on && a == SIO_TX) { ioram_w32(e, a, v); ir_tx_byte(e, (uint8_t)v); return; }
|
||||
#endif
|
||||
if (a >= 0x301910 && a <= 0x30192B) g_tc_rtc_wr++;
|
||||
if (a - CMU_BASE <= 0x24) { cmu_write32(e, a, v); return; }
|
||||
if (a == 0x301900) { if (v & 1) IOB(0x301900) &= (uint8_t)~1u; return; }
|
||||
ioram_w32(e, a, v);
|
||||
}
|
||||
|
||||
uint32_t periph_read32(Emu *e, uint32_t a)
|
||||
{
|
||||
if (a == SIO_STAT) return ioram_r32(e, a) & ~0x40u;
|
||||
if (a == SIO_RX) { uint32_t b = ioram_r32(e, a); IOB(IR_STAT) &= (uint8_t)~0x01u; return b; }
|
||||
return ioram_r32(e, a);
|
||||
}
|
||||
|
||||
#ifdef IOLOG
|
||||
extern int g_iolog;
|
||||
#endif
|
||||
void periph_write8(Emu *e, uint32_t a, uint8_t v)
|
||||
{
|
||||
#ifdef IOLOG
|
||||
if (g_iolog) printf("IO %.4f %06x <- %02x pc=%08x\n", (double)e->cycles / e->dev.osc3_hz, a, v, e->pc);
|
||||
#endif
|
||||
#ifndef NO_PN
|
||||
if (e->dev.nfc_pn512 && a == IR_BASE) { e->ioram[a - e->dev.io_base] = v; pn_host_byte(e, v); return; }
|
||||
#endif
|
||||
#ifdef TC_IRLINK
|
||||
if (g_ir.on && a >= IR_BASE && a <= IR_LAST && !e->dev.nfc_pn512) { ir_write8(e, a, v); return; }
|
||||
#endif
|
||||
if (a - CMU_BASE <= 0x27) {
|
||||
uint32_t word = a & ~3u;
|
||||
uint32_t cur = ioram_r32(e, word);
|
||||
int sh = (a & 3) * 8;
|
||||
cmu_write32(e, word, (cur & ~(0xFFu << sh)) | ((uint32_t)v << sh));
|
||||
return;
|
||||
}
|
||||
if (a >= 0x300280 && a <= 0x30028F) { e->ioram[a - e->dev.io_base] &= (uint8_t)~v; itc_scan(e); return; }
|
||||
if (a == 0x3002A9) { e->ioram[a - e->dev.io_base] &= (uint8_t)~v; itc_scan(e); return; }
|
||||
if (a == 0x301900) { if (v & 1) e->ioram[a - e->dev.io_base] &= (uint8_t)~1u; return; }
|
||||
if (a >= 0x301910 && a <= 0x30192B) g_tc_rtc_wr++; /* 1.6.8: the game sets its clock (main.c puts the Paradise's time back) */
|
||||
if (a == 0x300544 && (v & 0x06)) { /* battery measurement: immediate, healthy */
|
||||
e->ioram[a - e->dev.io_base] = v;
|
||||
IOB(0x300540) = 0x50; IOB(0x300541) = 0x03;
|
||||
IOB(0x300548) = 0x50; IOB(0x300549) = 0x03;
|
||||
IOB(0x300287) |= 2;
|
||||
itc_scan(e);
|
||||
return;
|
||||
}
|
||||
if (a >= 0x300780 && a <= 0x3007AF) {
|
||||
int x = (int)((a - 0x300780) / 8);
|
||||
int off = (int)((a - 0x300780) % 8);
|
||||
if (off == 6) {
|
||||
if (v & 2) { e->t16[x].counter = 0; e->t16[x].accum = 0; }
|
||||
e->ioram[a - e->dev.io_base] = v & (uint8_t)~2u;
|
||||
return;
|
||||
}
|
||||
if (off == 4) {
|
||||
e->t16[x].counter = (uint16_t)((e->t16[x].counter & 0xFF00) | v);
|
||||
e->t16[x].a_fired = 0;
|
||||
e->ioram[a - e->dev.io_base] = v;
|
||||
return;
|
||||
}
|
||||
if (off == 5) {
|
||||
e->t16[x].counter = (uint16_t)((e->t16[x].counter & 0x00FF) | ((uint16_t)v << 8));
|
||||
e->ioram[a - e->dev.io_base] = v;
|
||||
return;
|
||||
}
|
||||
}
|
||||
e->ioram[a - e->dev.io_base] = v;
|
||||
if ((a >= 0x300270 && a <= 0x30027F) || a == 0x3002A6) itc_scan(e);
|
||||
}
|
||||
|
||||
uint8_t periph_read8(Emu *e, uint32_t a)
|
||||
{
|
||||
if (a == 0x300380) {
|
||||
uint8_t p06 = e->dev.debug_strap_high ? 0x00 : 0x40; /* charger, no debug */
|
||||
uint8_t v = (uint8_t)((0x07u | p06) & ~(e->btn_mask & 7));
|
||||
if (e->dev.ir_gpio) v |= (uint8_t)(1u << e->dev.ir_gpio_rx_bit); /* IR idles high */
|
||||
return v;
|
||||
}
|
||||
if (a == IR_RXDATA) { uint8_t v = e->ioram[a - e->dev.io_base]; IOB(IR_STAT) &= (uint8_t)~0x01u; return v; }
|
||||
#ifdef TC_IRLINK
|
||||
if (a == IR_STAT && g_ir.on) { uint8_t st = e->ioram[a - e->dev.io_base]; return (uint8_t)(e->cycles >= g_ir.tx_ready_at ? st | 0x02u : st & ~0x02u); }
|
||||
#endif
|
||||
if (a == IR_STAT) return (uint8_t)(e->ioram[a - e->dev.io_base] | 0x02u);
|
||||
return e->ioram[a - e->dev.io_base];
|
||||
}
|
||||
|
||||
void periph_buttons(Emu *e, uint8_t mask)
|
||||
{
|
||||
uint8_t newly = mask & (uint8_t)~e->btn_mask & 7;
|
||||
e->btn_mask = mask & 7;
|
||||
if (newly) { IOB(0x300280) |= newly; itc_scan(e); }
|
||||
}
|
||||
|
||||
static const uint32_t presc_div[8] = {1, 2, 4, 16, 64, 256, 1024, 4096};
|
||||
|
||||
static void t16_set_flag(Emu *e, int x, int is_a)
|
||||
{
|
||||
uint32_t freg = 0x300282 + (uint32_t)(x / 2);
|
||||
int bit = (x & 1) ? (is_a ? 7 : 6) : (is_a ? 3 : 2);
|
||||
IOB(freg) |= (uint8_t)(1u << bit);
|
||||
}
|
||||
|
||||
/* Like the counting loop in the original, but in jumps instead of cycle by cycle. */
|
||||
static void t16_run(Emu *e, int x, uint32_t clocks)
|
||||
{
|
||||
uint32_t base = 0x300780 + (uint32_t)x * 8;
|
||||
uint16_t cra = (uint16_t)(IOB(base) | (IOB(base + 1) << 8));
|
||||
uint16_t crb = (uint16_t)(IOB(base + 2) | (IOB(base + 3) << 8));
|
||||
T16 *t = &e->t16[x];
|
||||
uint32_t c = t->counter;
|
||||
while (clocks) {
|
||||
uint32_t to_b = (crb > c) ? (uint32_t)crb - c : 1u;
|
||||
if (clocks < to_b) {
|
||||
c += clocks; clocks = 0;
|
||||
if (cra && !t->a_fired && c >= cra) { t16_set_flag(e, x, 1); t->a_fired = 1; }
|
||||
break;
|
||||
}
|
||||
c += to_b; clocks -= to_b;
|
||||
if (cra && !t->a_fired && c >= cra) t16_set_flag(e, x, 1);
|
||||
t16_set_flag(e, x, 0);
|
||||
c = 0; t->a_fired = 0;
|
||||
if (crb && clocks >= crb) { /* whole periods: flags are already set */
|
||||
if (cra && cra <= crb) t16_set_flag(e, x, 1);
|
||||
clocks %= crb;
|
||||
}
|
||||
}
|
||||
t->counter = (uint16_t)c;
|
||||
IOB(base + 4) = (uint8_t)c;
|
||||
IOB(base + 5) = (uint8_t)(c >> 8);
|
||||
}
|
||||
|
||||
static void itc_consider(Emu *e, uint32_t vec, uint8_t lvl)
|
||||
{
|
||||
e->wake_req = true;
|
||||
if (lvl > e->pending_level || !e->pending_irq) { e->pending_irq = vec; e->pending_level = lvl; }
|
||||
}
|
||||
|
||||
static void itc_scan(Emu *e)
|
||||
{
|
||||
static const uint8_t vec_b[6] = {30, 34, 38, 42, 46, 50};
|
||||
e->pending_irq = 0; e->pending_level = 0; e->wake_req = false;
|
||||
{
|
||||
uint8_t act = IOB(0x300270) & IOB(0x300280) & 0x0F;
|
||||
for (int p = 0; p < 4; p++) {
|
||||
if (!(act & (1u << p))) continue;
|
||||
uint8_t lr = IOB(0x300260 + (uint32_t)(p / 2));
|
||||
uint8_t lvl = (p & 1) ? (lr >> 4) & 7 : lr & 7;
|
||||
itc_consider(e, 16u + (uint32_t)p, lvl);
|
||||
}
|
||||
}
|
||||
for (int x = 0; x < 6; x++) {
|
||||
uint32_t en = 0x300272 + (uint32_t)(x / 2), fl = 0x300282 + (uint32_t)(x / 2);
|
||||
uint8_t act = IOB(en) & IOB(fl);
|
||||
int b_bit = (x & 1) ? 6 : 2, a_bit = (x & 1) ? 7 : 3;
|
||||
uint8_t lvl_reg = IOB(0x300266 + (uint32_t)(x / 2));
|
||||
uint8_t lvl = (x & 1) ? (lvl_reg >> 4) & 7 : lvl_reg & 7;
|
||||
if (act & (1u << b_bit)) itc_consider(e, vec_b[x], lvl);
|
||||
if (act & (1u << a_bit)) itc_consider(e, vec_b[x] + 1u, lvl);
|
||||
}
|
||||
if (IOB(0x300277) & IOB(0x300287) & 4) { uint8_t lvl = (IOB(0x30026A) >> 4) & 7; itc_consider(e, 65, lvl ? lvl : 1); }
|
||||
if (IOB(0x300277) & IOB(0x300287) & 2) { uint8_t lvl = IOB(0x30026A) & 7; itc_consider(e, 64, lvl ? lvl : 1); }
|
||||
if (IOB(0x300277) & IOB(0x300287) & 0x08u) itc_consider(e, 68, 4);
|
||||
{
|
||||
uint8_t act = IOB(0x300276) & IOB(0x300286);
|
||||
for (int b = 3; b <= 5; b++)
|
||||
if (act & (uint8_t)(1u << b)) itc_consider(e, (uint32_t)(57 + b), 4);
|
||||
}
|
||||
}
|
||||
|
||||
static uint8_t bcd_inc(uint8_t v, uint8_t wrap_after)
|
||||
{
|
||||
v++;
|
||||
if ((v & 0xF) > 9) v = (uint8_t)((v & 0xF0) + 0x10);
|
||||
if (v > wrap_after) return 0xFF;
|
||||
return v;
|
||||
}
|
||||
static const uint8_t month_days[13] = {0,0x31,0x28,0x31,0x30,0x31,0x30,0x31,0x31,0x30,0x31,0x30,0x31};
|
||||
|
||||
#ifdef HOST
|
||||
uint32_t g_host_rtc_secs;
|
||||
#endif
|
||||
TC_SRAM static void rtc_count_second(Emu *e)
|
||||
{
|
||||
#ifdef HOST
|
||||
g_host_rtc_secs++;
|
||||
#endif
|
||||
uint8_t v = bcd_inc(IOB(0x301910), 0x59);
|
||||
IOB(0x301910) = (v == 0xFF) ? 0 : v; if (v != 0xFF) return;
|
||||
v = bcd_inc(IOB(0x301914), 0x59);
|
||||
IOB(0x301914) = (v == 0xFF) ? 0 : v; if (v != 0xFF) return;
|
||||
v = bcd_inc(IOB(0x301918), 0x23);
|
||||
IOB(0x301918) = (v == 0xFF) ? 0 : v; if (v != 0xFF) return;
|
||||
IOB(0x301928) = (uint8_t)((IOB(0x301928) + 1) % 7);
|
||||
uint8_t mon = IOB(0x301920);
|
||||
int mi = ((mon >> 4) & 1) * 10 + (mon & 0xF);
|
||||
uint8_t dim = (mi >= 1 && mi <= 12) ? month_days[mi] : 0x31;
|
||||
if (mi == 2 && ((IOB(0x301924) & 0xF) % 4 == 0)) dim = 0x29;
|
||||
v = bcd_inc(IOB(0x30191C), dim);
|
||||
IOB(0x30191C) = (v == 0xFF) ? 1 : v; if (v != 0xFF) return;
|
||||
v = bcd_inc(IOB(0x301920), 0x12);
|
||||
IOB(0x301920) = (v == 0xFF) ? 1 : v; if (v != 0xFF) return;
|
||||
v = bcd_inc(IOB(0x301924), 0x99);
|
||||
IOB(0x301924) = (v == 0xFF) ? 0 : v;
|
||||
}
|
||||
|
||||
/* Set the guest clock to local time t (seconds since 1970), e.g. the Paradise's time at the start.
|
||||
* The weekday moves by the days between the old date and the new one. */
|
||||
static uint8_t to_bcd(uint32_t v) { return (uint8_t)(((v / 10u) << 4) | (v % 10u)); }
|
||||
static uint32_t from_bcd(uint8_t v) { return (uint32_t)(v >> 4) * 10u + (v & 15u); }
|
||||
TC_SRAM static int32_t days_from(int32_t y, int32_t m, int32_t d)
|
||||
{
|
||||
y -= m <= 2; int32_t era = (y >= 0 ? y : y - 399) / 400, yoe = y - era * 400;
|
||||
int32_t doy = (153 * (m + (m > 2 ? -3 : 9)) + 2) / 5 + d - 1, doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
|
||||
return era * 146097 + doe - 719468;
|
||||
}
|
||||
TC_SRAM void periph_rtc_set(Emu *e, uint32_t t)
|
||||
{
|
||||
if ((IOB(0x301908) & 2) || (IOB(0x30190C) & 1)) return; /* stopped or being set by the game */
|
||||
uint32_t days = t / 86400u, sod = t % 86400u;
|
||||
uint32_t z = days + 719468u, era = z / 146097u, doe = z - era * 146097u;
|
||||
uint32_t yoe = (doe - doe / 1460u + doe / 36524u - doe / 146096u) / 365u, doy = doe - (365u * yoe + yoe / 4u - yoe / 100u);
|
||||
uint32_t mp = (5u * doy + 2u) / 153u, d = doy - (153u * mp + 2u) / 5u + 1u, m = mp < 10u ? mp + 3u : mp - 9u, y = yoe + era * 400u + (m <= 2u);
|
||||
uint32_t oy = 2000u + from_bcd(IOB(0x301924)), om = from_bcd(IOB(0x301920)), od = from_bcd(IOB(0x30191C)), wd;
|
||||
if (om >= 1u && om <= 12u && od >= 1u && od <= 31u && IOB(0x301928) < 7u) {
|
||||
int32_t diff = (int32_t)days - days_from((int32_t)oy, (int32_t)om, (int32_t)od);
|
||||
wd = (uint32_t)(((int32_t)IOB(0x301928) + diff % 7 + 7) % 7);
|
||||
} else wd = (days + 4u) % 7u; /* 1970-01-01 was a Thursday (0 = Sunday) */
|
||||
IOB(0x301910) = to_bcd(sod % 60u); IOB(0x301914) = to_bcd((sod / 60u) % 60u); IOB(0x301918) = to_bcd(sod / 3600u);
|
||||
IOB(0x30191C) = to_bcd(d); IOB(0x301920) = to_bcd(m); IOB(0x301924) = to_bcd(y % 100u); IOB(0x301928) = (uint8_t)wd;
|
||||
e->rtc_cyc = 0;
|
||||
}
|
||||
|
||||
/* 1.7.2 test16: the guest clock as local time (seconds since 1970), 0 = stopped, being set or not a date */
|
||||
TC_SRAM uint32_t periph_rtc_get(Emu *e)
|
||||
{
|
||||
if ((IOB(0x301908) & 2) || (IOB(0x30190C) & 1)) return 0;
|
||||
uint32_t s = from_bcd(IOB(0x301910)), mi = from_bcd(IOB(0x301914)), h = from_bcd(IOB(0x301918));
|
||||
uint32_t d = from_bcd(IOB(0x30191C)), m = from_bcd(IOB(0x301920)), y = 2000u + from_bcd(IOB(0x301924));
|
||||
if (m < 1u || m > 12u || d < 1u || d > 31u || h > 23u || mi > 59u || s > 59u) return 0;
|
||||
return (uint32_t)days_from((int32_t)y, (int32_t)m, (int32_t)d) * 86400u + h * 3600u + mi * 60u + s;
|
||||
}
|
||||
|
||||
/* Advance the guest clock by n seconds (after a pause). */
|
||||
TC_SRAM void periph_rtc_advance(Emu *e, uint32_t n)
|
||||
{
|
||||
if ((IOB(0x301908) & 2) || (IOB(0x30190C) & 1)) return;
|
||||
while (n--) rtc_count_second(e);
|
||||
}
|
||||
|
||||
static void rtc_tick(Emu *e, uint32_t cycles)
|
||||
{
|
||||
uint32_t hz = mclk(e);
|
||||
#ifdef RTC_MS
|
||||
/* 1.6.8: seconds and the 1 s / 1 min / 1 h interrupt come from periph_rtc_ms (real milliseconds of the
|
||||
* main loop). Counted in guest cycles they went wrong whenever the guest changed its clock: a part of a
|
||||
* second at 18 MHz counted as minutes at 16 kHz (iD L: clock jumped when its display went off). */
|
||||
uint32_t sel = (IOB(0x301904) >> 2) & 3;
|
||||
if (sel) return;
|
||||
#else
|
||||
if (!(IOB(0x301908) & 2) && !(IOB(0x30190C) & 1)) {
|
||||
e->rtc_cyc += cycles;
|
||||
while (e->rtc_cyc >= hz) { e->rtc_cyc -= hz; rtc_count_second(e); }
|
||||
}
|
||||
uint32_t sel = (IOB(0x301904) >> 2) & 3;
|
||||
#endif
|
||||
uint64_t p = sel == 0 ? hz / 64 : sel == 1 ? hz : sel == 2 ? (uint64_t)hz * 60 : (uint64_t)hz * 3600;
|
||||
e->rtc_irq_cyc += cycles;
|
||||
#ifdef RTC_MS
|
||||
if (e->rtc_irq_cyc >= 2u * (uint32_t)p) e->rtc_irq_cyc = (uint32_t)p; /* clock switched down: no burst */
|
||||
#endif
|
||||
if (e->rtc_irq_cyc >= p) {
|
||||
e->rtc_irq_cyc -= (uint32_t)(p > 0xFFFFFFFFu ? 0xFFFFFFFFu : p);
|
||||
IOB(0x301900) |= 1;
|
||||
if (IOB(0x301904) & 1) IOB(0x300287) |= 4;
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef RTC_MS
|
||||
/* one real millisecond (called by the main loop for every guest ms) */
|
||||
void periph_rtc_ms(Emu *e)
|
||||
{
|
||||
if (!(IOB(0x301908) & 2) && !(IOB(0x30190C) & 1)) {
|
||||
if (++e->rtc_cyc >= 1000u) { e->rtc_cyc = 0; rtc_count_second(e); }
|
||||
}
|
||||
uint32_t sel = (IOB(0x301904) >> 2) & 3;
|
||||
if (!sel) return;
|
||||
uint32_t p = sel == 1 ? 1000u : sel == 2 ? 60000u : 3600000u;
|
||||
if (++e->rtc_irq_cyc >= p) {
|
||||
e->rtc_irq_cyc = 0;
|
||||
IOB(0x301900) |= 1;
|
||||
if (IOB(0x301904) & 1) { IOB(0x300287) |= 4; itc_scan(e); }
|
||||
}
|
||||
}
|
||||
#endif
|
||||
void periph_tick(Emu *e, uint32_t cycles)
|
||||
{
|
||||
rtc_tick(e, cycles);
|
||||
#ifdef TC_IRLINK
|
||||
if (g_ir.on) ir_tick(e);
|
||||
#endif
|
||||
#ifndef NO_PN
|
||||
if (e->dev.nfc_pn512) pn_tick(e);
|
||||
#endif
|
||||
for (int x = 0; x < 6; x++) {
|
||||
uint8_t c = IOB(0x300780 + (uint32_t)x * 8 + 6);
|
||||
if (!(c & 1)) continue;
|
||||
T16 *t = &e->t16[x];
|
||||
uint8_t ck = IOB(0x3007E0 + (uint32_t)x * 2);
|
||||
if (!(ck & 8)) continue;
|
||||
t->accum += cycles;
|
||||
uint32_t div = presc_div[ck & 7];
|
||||
uint32_t clocks = t->accum / div;
|
||||
t->accum -= clocks * div;
|
||||
if (clocks) t16_run(e, x, clocks);
|
||||
}
|
||||
itc_scan(e);
|
||||
}
|
||||
|
||||
/* Cycles until the next peripheral event (timers, clock). */
|
||||
uint32_t periph_next(Emu *e)
|
||||
{
|
||||
uint64_t best = 0xFFFFFFFFu;
|
||||
uint32_t hz = mclk(e);
|
||||
if ((IOB(0x301904) & 1) && (IOB(0x300277) & 4)
|
||||
#ifdef RTC_MS
|
||||
&& !((IOB(0x301904) >> 2) & 3)
|
||||
#endif
|
||||
) {
|
||||
uint32_t sel = (IOB(0x301904) >> 2) & 3;
|
||||
uint64_t p = sel == 0 ? hz / 64 : sel == 1 ? hz : sel == 2 ? (uint64_t)hz * 60 : (uint64_t)hz * 3600;
|
||||
if (p > e->rtc_irq_cyc && p - e->rtc_irq_cyc < best) best = p - e->rtc_irq_cyc;
|
||||
else if (p <= e->rtc_irq_cyc) best = 1;
|
||||
}
|
||||
for (int x = 0; x < 6; x++) {
|
||||
uint8_t c = IOB(0x300780 + (uint32_t)x * 8 + 6);
|
||||
if (!(c & 1)) continue;
|
||||
uint8_t ck = IOB(0x3007E0 + (uint32_t)x * 2);
|
||||
if (!(ck & 8)) continue;
|
||||
uint8_t en = IOB(0x300272 + (uint32_t)(x / 2));
|
||||
int ea = (en >> ((x & 1) ? 7 : 3)) & 1, eb = (en >> ((x & 1) ? 6 : 2)) & 1;
|
||||
if (!ea && !eb) continue;
|
||||
uint32_t base = 0x300780 + (uint32_t)x * 8;
|
||||
uint32_t cra = (uint32_t)(IOB(base) | (IOB(base + 1) << 8));
|
||||
uint32_t crb = (uint32_t)(IOB(base + 2) | (IOB(base + 3) << 8));
|
||||
T16 *t = &e->t16[x];
|
||||
uint32_t cnt = t->counter;
|
||||
uint32_t n = (crb > cnt) ? crb - cnt : 1u;
|
||||
if (!eb) n = 0xFFFFFu;
|
||||
if (ea && cra && !t->a_fired && cra > cnt && cra - cnt < n) n = cra - cnt;
|
||||
if (n == 0xFFFFFu) continue;
|
||||
uint64_t cyc = (uint64_t)n * presc_div[ck & 7];
|
||||
cyc = cyc > t->accum ? cyc - t->accum : 1;
|
||||
if (cyc < best) best = cyc;
|
||||
}
|
||||
#ifdef TC_IRLINK
|
||||
if (g_ir.on) { /* TX done, next received byte */
|
||||
uint64_t now = e->last_tick;
|
||||
if (g_ir.tx_ready_at) { uint64_t d = g_ir.tx_ready_at > now ? g_ir.tx_ready_at - now : 1u; if (d < best) best = d; }
|
||||
if (IOB(IR_TRIG) && tc_ir_avail()) { uint64_t d = g_ir.rx_next_at > now ? g_ir.rx_next_at - now : 1u; if (d < best) best = d; }
|
||||
}
|
||||
#endif
|
||||
return best ? (uint32_t)best : 1u;
|
||||
}
|
||||
|
||||
/* Guest sound: T16 channel 0 drives the buzzer. If it runs and its output
|
||||
* toggles (compare A below B), the frequency is clock / divider / (B+1). */
|
||||
uint32_t periph_tone_hz(Emu *e)
|
||||
{
|
||||
uint8_t c = IOB(0x300786), ck = IOB(0x3007E0);
|
||||
if (!(c & 1) || !(ck & 8)) return 0;
|
||||
uint32_t cra = (uint32_t)(IOB(0x300780) | (IOB(0x300781) << 8));
|
||||
uint32_t crb = (uint32_t)(IOB(0x300782) | (IOB(0x300783) << 8));
|
||||
if (!cra || cra >= crb) return 0;
|
||||
uint32_t hz = mclk(e) / presc_div[ck & 7] / (crb + 1);
|
||||
return (hz >= 20 && hz <= 12000) ? hz : 0;
|
||||
}
|
||||
|
||||
void periph_init(Emu *e)
|
||||
{
|
||||
e->cmu.osc3_hz = e->dev.osc3_hz;
|
||||
e->cmu.mclk_hz = 0;
|
||||
}
|
||||
Reference in new issue
Block a user