├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── gradle.yml ├── .gitignore ├── CHANGES.md ├── LICENSE ├── README.md ├── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src ├── main └── kotlin │ └── kmips │ └── Assembler.kt └── test └── kotlin └── kmips └── AssemblerTest.kt /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | indent_style = space 6 | indent_size = 2 7 | insert_final_newline = true 8 | trim_trailing_whitespace = true 9 | max_line_length = 150 10 | end_of_line = lf 11 | 12 | [*.md] 13 | max_line_length = off 14 | trim_trailing_whitespace = false 15 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.github/workflows/gradle.yml: -------------------------------------------------------------------------------- 1 | name: Kotlin CI 2 | 3 | on: [ push ] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/checkout@v2 10 | - name: Set up JDK 11 | uses: actions/setup-java@v2 12 | with: 13 | distribution: adopt 14 | java-version: 11 15 | - name: Build with Gradle 16 | run: chmod +x gradlew && ./gradlew clean test 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Maven 2 | target/ 3 | 4 | ## Java 5 | 6 | *.class 7 | *.war 8 | *.ear 9 | hs_err_pid* 10 | 11 | ## Android Studio and Intellij 12 | 13 | .idea/ 14 | *.ipr 15 | *.iws 16 | *.iml 17 | out/ 18 | com_crashlytics_export_strings.xml 19 | 20 | ## Eclipse 21 | 22 | .metadata 23 | bin/ 24 | tmp/ 25 | *.tmp 26 | *.bak 27 | *.swp 28 | *~.nib 29 | local.properties 30 | .settings/ 31 | .loadpath 32 | .pydevproject 33 | .project 34 | .classpath 35 | .externalToolBuilders/ 36 | *.launch 37 | 38 | ## NetBeans 39 | nbproject/private/ 40 | build/ 41 | nbbuild/ 42 | dist/ 43 | nbdist/ 44 | nbactions.xml 45 | nb-configuration.xml 46 | 47 | ## Gradle 48 | 49 | .gradle 50 | /target 51 | -------------------------------------------------------------------------------- /CHANGES.md: -------------------------------------------------------------------------------- 1 | #### Version: 1.8 2 | 3 | #### Version: 1.7 4 | 5 | - Fixed label couldn't be placed at very high address 6 | - Added function to create and place label (useful if label doesn't need to be accessed before the declaration address) 7 | - Updated to Kotlin 1.9.24 8 | 9 | #### Version: 1.6 10 | 11 | - Updated to Kotlin 1.6.10 12 | 13 | #### Version: 1.5 14 | 15 | - Updated to Kotlin 1.5.30 16 | - JVM target is now JVM 11 17 | 18 | #### Version: 1.4 19 | 20 | - Fixed incorrect assembly for pseudo-instructions: `blt`, `bge`, `bgt`, `ble` 21 | - Updated to Kotlin 1.3.50 22 | 23 | #### Version: 1.3 24 | 25 | - Updated to Kotlin 1.3.20 26 | 27 | #### Version: 1.2 28 | 29 | - Added MIPS II instruction set (including FPU instructions) 30 | 31 | #### Version: 1.1 32 | 33 | - Added FPU instruction set 34 | - Updated instruction set to closer match reference document 35 | - `Int`s are now used as asm instructions argument types instead of `Short`s 36 | - `li` pseudo instruction will get converted to `addiu` for negative values and to `ori` for positive values 37 | 38 | #### Version: 1.0 39 | 40 | - Initial release 41 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | kmips 2 | ----- 3 | 4 | kmips is a MIPS assembler that is invoked directly from Kotlin code. It implements MIPS II instruction set, including FPU 5 | (coprocessor 1) instructions. The main purpose of kmips is to provide simple way of writing code patches for compiled 6 | executables. It was successfully used in few fan translations and game modding projects. 7 | 8 | kmips is available from the Maven Central repository: 9 | 10 | ```groovy 11 | implementation "com.kotcrab.kmips:kmips:1.7" 12 | ``` 13 | 14 | ##### Example code 15 | 16 | Assemble some instruction to fill 32 bytes of memory at address 0x08804100 with incrementing value 17 | 18 | ```kotlin 19 | import kmips.Label 20 | import kmips.Reg.* 21 | import kmips.assemble 22 | 23 | assemble(startPc = 0x8804000) { 24 | val loop = Label() 25 | val target = 0x08804100 26 | val bytes = 32 27 | 28 | la(s0, target) // write target address 29 | li(t1, 0) // loop counter 30 | li(t2, bytes) // how many bytes to write 31 | label(loop) // 'loop' label will be placed here 32 | sb(t1, 0, s0) // store byte in memory at register s0 with offset 0 33 | addiu(t1, t1, 1) // increment loop counter 34 | addiu(s0, s0, 1) // increment memory address pointer 35 | bne(t1, t2, loop) // jump to `loop` branch if not equal 36 | nop() // ignoring branch delay slot for example simplicity 37 | } 38 | ``` 39 | 40 | Result of running the assembled code: 41 | 42 | ``` 43 | 0x08804100: 00 01 02 03 | 04 05 06 07 | 08 09 0A 0B | 0C 0D 0E 0F 44 | 0x08804110: 10 11 12 13 | 14 15 16 17 | 18 19 1A 1B | 1C 1D 1E 1F 45 | ``` 46 | 47 | Specifying `startPc` (initial program counter) is necessary for calculating address of branch and jump instructions. 48 | 49 | Alternatively to `assemble` which returns a `List` you can also use `assembleAsHexString` or 50 | `assembleAsByteArray`. In any case, the result is ready to be written into the target executable. If you're executable is 51 | relocatable you might also need to manually update relocation table. 52 | 53 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id "org.jetbrains.kotlin.jvm" version "1.9.24" 3 | id "java-library" 4 | id "maven-publish" 5 | id "io.github.gradle-nexus.publish-plugin" version "1.1.0" 6 | id "signing" 7 | } 8 | 9 | group "com.kotcrab.kmips" 10 | version "1.8-SNAPSHOT" 11 | 12 | repositories { 13 | mavenCentral() 14 | } 15 | 16 | compileKotlin { 17 | kotlinOptions { 18 | jvmTarget = "11" 19 | } 20 | } 21 | 22 | compileTestKotlin { 23 | kotlinOptions { 24 | jvmTarget = "11" 25 | } 26 | } 27 | 28 | dependencies { 29 | implementation "org.jetbrains.kotlin:kotlin-stdlib:1.9.24" 30 | testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2' 31 | testImplementation 'org.junit.jupiter:junit-jupiter-params:5.8.2' 32 | testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.2' 33 | } 34 | 35 | test { 36 | useJUnitPlatform() 37 | } 38 | 39 | java { 40 | withJavadocJar() 41 | withSourcesJar() 42 | } 43 | 44 | nexusPublishing { 45 | repositories { 46 | sonatype() 47 | } 48 | } 49 | 50 | publishing { 51 | publications { 52 | mavenJava(MavenPublication) { 53 | from(components.java) 54 | pom { 55 | name.set("kmips") 56 | description.set("MIPS assembler intended for assembling small code patches") 57 | url.set("https://github.com/kotcrab/kmips") 58 | licenses { 59 | license { 60 | name.set("The Apache License, Version 2.0") 61 | url.set("https://www.apache.org/licenses/LICENSE-2.0.txt") 62 | } 63 | } 64 | developers { 65 | developer { 66 | id.set("kotcrab") 67 | name.set("Kotcrab") 68 | url.set("https://kotcrab.com") 69 | } 70 | } 71 | scm { 72 | connection.set("scm:git:git@github.com:kotcrab/kmips.git") 73 | developerConnection.set("scm:git:git@github.com:kotcrab/kmips.git") 74 | url.set("git@github.com:kotcrab/kmips.git") 75 | } 76 | } 77 | } 78 | } 79 | } 80 | 81 | signing { 82 | required { !project.version.endsWith("-SNAPSHOT") && !project.hasProperty("skipSigning") } 83 | sign publishing.publications.mavenJava 84 | } 85 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kotcrab/kmips/878209b29b972587842fa000194c1ce8dda667be/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | # 21 | # Gradle start up script for POSIX generated by Gradle. 22 | # 23 | # Important for running: 24 | # 25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 26 | # noncompliant, but you have some other compliant shell such as ksh or 27 | # bash, then to run this script, type that shell name before the whole 28 | # command line, like: 29 | # 30 | # ksh Gradle 31 | # 32 | # Busybox and similar reduced shells will NOT work, because this script 33 | # requires all of these POSIX shell features: 34 | # * functions; 35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 37 | # * compound commands having a testable exit status, especially «case»; 38 | # * various built-in commands including «command», «set», and «ulimit». 39 | # 40 | # Important for patching: 41 | # 42 | # (2) This script targets any POSIX shell, so it avoids extensions provided 43 | # by Bash, Ksh, etc; in particular arrays are avoided. 44 | # 45 | # The "traditional" practice of packing multiple parameters into a 46 | # space-separated string is a well documented source of bugs and security 47 | # problems, so this is (mostly) avoided, by progressively accumulating 48 | # options in "$@", and eventually passing that to Java. 49 | # 50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 52 | # see the in-line comments for details. 53 | # 54 | # There are tweaks for specific operating systems such as AIX, CygWin, 55 | # Darwin, MinGW, and NonStop. 56 | # 57 | # (3) This script is generated from the Groovy template 58 | # https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 59 | # within the Gradle project. 60 | # 61 | # You can find Gradle at https://github.com/gradle/gradle/. 62 | # 63 | ############################################################################## 64 | 65 | # Attempt to set APP_HOME 66 | 67 | # Resolve links: $0 may be a link 68 | app_path=$0 69 | 70 | # Need this for daisy-chained symlinks. 71 | while 72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 73 | [ -h "$app_path" ] 74 | do 75 | ls=$( ls -ld "$app_path" ) 76 | link=${ls#*' -> '} 77 | case $link in #( 78 | /*) app_path=$link ;; #( 79 | *) app_path=$APP_HOME$link ;; 80 | esac 81 | done 82 | 83 | # This is normally unused 84 | # shellcheck disable=SC2034 85 | APP_BASE_NAME=${0##*/} 86 | # Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036) 87 | APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit 88 | 89 | # Use the maximum available, or set MAX_FD != -1 to use that value. 90 | MAX_FD=maximum 91 | 92 | warn () { 93 | echo "$*" 94 | } >&2 95 | 96 | die () { 97 | echo 98 | echo "$*" 99 | echo 100 | exit 1 101 | } >&2 102 | 103 | # OS specific support (must be 'true' or 'false'). 104 | cygwin=false 105 | msys=false 106 | darwin=false 107 | nonstop=false 108 | case "$( uname )" in #( 109 | CYGWIN* ) cygwin=true ;; #( 110 | Darwin* ) darwin=true ;; #( 111 | MSYS* | MINGW* ) msys=true ;; #( 112 | NONSTOP* ) nonstop=true ;; 113 | esac 114 | 115 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 116 | 117 | 118 | # Determine the Java command to use to start the JVM. 119 | if [ -n "$JAVA_HOME" ] ; then 120 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 121 | # IBM's JDK on AIX uses strange locations for the executables 122 | JAVACMD=$JAVA_HOME/jre/sh/java 123 | else 124 | JAVACMD=$JAVA_HOME/bin/java 125 | fi 126 | if [ ! -x "$JAVACMD" ] ; then 127 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 128 | 129 | Please set the JAVA_HOME variable in your environment to match the 130 | location of your Java installation." 131 | fi 132 | else 133 | JAVACMD=java 134 | if ! command -v java >/dev/null 2>&1 135 | then 136 | die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 137 | 138 | Please set the JAVA_HOME variable in your environment to match the 139 | location of your Java installation." 140 | fi 141 | fi 142 | 143 | # Increase the maximum file descriptors if we can. 144 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 145 | case $MAX_FD in #( 146 | max*) 147 | # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. 148 | # shellcheck disable=SC2039,SC3045 149 | MAX_FD=$( ulimit -H -n ) || 150 | warn "Could not query maximum file descriptor limit" 151 | esac 152 | case $MAX_FD in #( 153 | '' | soft) :;; #( 154 | *) 155 | # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. 156 | # shellcheck disable=SC2039,SC3045 157 | ulimit -n "$MAX_FD" || 158 | warn "Could not set maximum file descriptor limit to $MAX_FD" 159 | esac 160 | fi 161 | 162 | # Collect all arguments for the java command, stacking in reverse order: 163 | # * args from the command line 164 | # * the main class name 165 | # * -classpath 166 | # * -D...appname settings 167 | # * --module-path (only if needed) 168 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 169 | 170 | # For Cygwin or MSYS, switch paths to Windows format before running java 171 | if "$cygwin" || "$msys" ; then 172 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 173 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 174 | 175 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 176 | 177 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 178 | for arg do 179 | if 180 | case $arg in #( 181 | -*) false ;; # don't mess with options #( 182 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 183 | [ -e "$t" ] ;; #( 184 | *) false ;; 185 | esac 186 | then 187 | arg=$( cygpath --path --ignore --mixed "$arg" ) 188 | fi 189 | # Roll the args list around exactly as many times as the number of 190 | # args, so each arg winds up back in the position where it started, but 191 | # possibly modified. 192 | # 193 | # NB: a `for` loop captures its iteration list before it begins, so 194 | # changing the positional parameters here affects neither the number of 195 | # iterations, nor the values presented in `arg`. 196 | shift # remove old arg 197 | set -- "$@" "$arg" # push replacement arg 198 | done 199 | fi 200 | 201 | 202 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 203 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 204 | 205 | # Collect all arguments for the java command: 206 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments, 207 | # and any embedded shellness will be escaped. 208 | # * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be 209 | # treated as '${Hostname}' itself on the command line. 210 | 211 | set -- \ 212 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 213 | -classpath "$CLASSPATH" \ 214 | org.gradle.wrapper.GradleWrapperMain \ 215 | "$@" 216 | 217 | # Stop when "xargs" is not available. 218 | if ! command -v xargs >/dev/null 2>&1 219 | then 220 | die "xargs is not available" 221 | fi 222 | 223 | # Use "xargs" to parse quoted args. 224 | # 225 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 226 | # 227 | # In Bash we could simply go: 228 | # 229 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 230 | # set -- "${ARGS[@]}" "$@" 231 | # 232 | # but POSIX shell has neither arrays nor command substitution, so instead we 233 | # post-process each arg (as a line of input to sed) to backslash-escape any 234 | # character that might be a shell metacharacter, then use eval to reverse 235 | # that process (while maintaining the separation between arguments), and wrap 236 | # the whole thing up as a single "set" statement. 237 | # 238 | # This will of course break if any of these variables contains a newline or 239 | # an unmatched quote. 240 | # 241 | 242 | eval "set -- $( 243 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 244 | xargs -n1 | 245 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 246 | tr '\n' ' ' 247 | )" '"$@"' 248 | 249 | exec "$JAVACMD" "$@" 250 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%"=="" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%"=="" set DIRNAME=. 29 | @rem This is normally unused 30 | set APP_BASE_NAME=%~n0 31 | set APP_HOME=%DIRNAME% 32 | 33 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 34 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 35 | 36 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 37 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 38 | 39 | @rem Find java.exe 40 | if defined JAVA_HOME goto findJavaFromJavaHome 41 | 42 | set JAVA_EXE=java.exe 43 | %JAVA_EXE% -version >NUL 2>&1 44 | if %ERRORLEVEL% equ 0 goto execute 45 | 46 | echo. 1>&2 47 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 48 | echo. 1>&2 49 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 50 | echo location of your Java installation. 1>&2 51 | 52 | goto fail 53 | 54 | :findJavaFromJavaHome 55 | set JAVA_HOME=%JAVA_HOME:"=% 56 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 57 | 58 | if exist "%JAVA_EXE%" goto execute 59 | 60 | echo. 1>&2 61 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 62 | echo. 1>&2 63 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 64 | echo location of your Java installation. 1>&2 65 | 66 | goto fail 67 | 68 | :execute 69 | @rem Setup the command line 70 | 71 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 72 | 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if %ERRORLEVEL% equ 0 goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | set EXIT_CODE=%ERRORLEVEL% 85 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 86 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 87 | exit /b %EXIT_CODE% 88 | 89 | :mainEnd 90 | if "%OS%"=="Windows_NT" endlocal 91 | 92 | :omega 93 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'kmips' 2 | -------------------------------------------------------------------------------- /src/main/kotlin/kmips/Assembler.kt: -------------------------------------------------------------------------------- 1 | package kmips 2 | 3 | import java.io.ByteArrayOutputStream 4 | 5 | fun assemble(startPc: Int = 0, endianness: Endianness = Endianness.Little, init: Assembler.() -> Unit): List { 6 | val assembler = Assembler(startPc, endianness) 7 | assembler.init() 8 | return assembler.assembleAsList() 9 | } 10 | 11 | fun assembleAsHexString(startPc: Int = 0, endianness: Endianness = Endianness.Little, init: Assembler.() -> Unit): String { 12 | val assembler = Assembler(startPc, endianness) 13 | assembler.init() 14 | return assembler.assembleAsHexString() 15 | } 16 | 17 | fun assembleAsByteArray( 18 | startPc: Int = 0, 19 | endianness: Endianness = Endianness.Little, 20 | init: Assembler.() -> Unit, 21 | ): ByteArray { 22 | val assembler = Assembler(startPc, endianness) 23 | assembler.init() 24 | return assembler.assembleAsByteArray() 25 | } 26 | 27 | class Assembler(startPc: Int, val endianness: Endianness) { 28 | companion object { 29 | /** FPU (coprocessor 1) */ 30 | private const val COP1 = 0b010_001 31 | 32 | /** 32-bit float */ 33 | private const val FMT_S = 16 34 | 35 | /** 64-bit float */ 36 | private const val FMT_D = 17 37 | 38 | /** 32-bit signed magnitude int */ 39 | private const val FMT_W = 20 40 | 41 | /** 64-bit signed magnitude int */ 42 | private const val FMT_L = 21 43 | } 44 | 45 | var virtualPc = startPc 46 | private set 47 | private val instructions = mutableListOf() 48 | 49 | // MIPS II 50 | 51 | fun lb(rt: Reg, offset: Int, base: Reg) = emit(IInstruction(0b100_000, base, rt, offset)) 52 | fun lbu(rt: Reg, offset: Int, base: Reg) = emit(IInstruction(0b100_100, base, rt, offset)) 53 | fun sb(rt: Reg, offset: Int, base: Reg) = emit(IInstruction(0b101_000, base, rt, offset)) 54 | 55 | fun lh(rt: Reg, offset: Int, base: Reg) = emit(IInstruction(0b100_001, base, rt, offset)) 56 | fun lhu(rt: Reg, offset: Int, base: Reg) = emit(IInstruction(0b100_101, base, rt, offset)) 57 | fun sh(rt: Reg, offset: Int, base: Reg) = emit(IInstruction(0b101_001, base, rt, offset)) 58 | 59 | fun lw(rt: Reg, offset: Int, base: Reg) = emit(IInstruction(0b100_011, base, rt, offset)) 60 | fun sw(rt: Reg, offset: Int, base: Reg) = emit(IInstruction(0b101_011, base, rt, offset)) 61 | 62 | fun lwl(rt: Reg, offset: Int, base: Reg) = emit(IInstruction(0b100_010, base, rt, offset)) 63 | fun lwr(rt: Reg, offset: Int, base: Reg) = emit(IInstruction(0b100_110, base, rt, offset)) 64 | fun swl(rt: Reg, offset: Int, base: Reg) = emit(IInstruction(0b101_010, base, rt, offset)) 65 | fun swr(rt: Reg, offset: Int, base: Reg) = emit(IInstruction(0b101_110, base, rt, offset)) 66 | 67 | fun ll(rt: Reg, offset: Int, base: Reg) = emit(IInstruction(0b110_000, base, rt, offset)) 68 | fun sc(rt: Reg, offset: Int, base: Reg) = emit(IInstruction(0b111_000, base, rt, offset)) 69 | 70 | fun addi(rt: Reg, rs: Reg, imm: Int) = emit(IInstruction(0b001_000, rs, rt, imm)) 71 | fun addiu(rt: Reg, rs: Reg, imm: Int) = emit(IInstruction(0b001_001, rs, rt, imm)) 72 | fun slti(rt: Reg, rs: Reg, imm: Int) = emit(IInstruction(0b001_010, rs, rt, imm)) 73 | fun sltiu(rt: Reg, rs: Reg, imm: Int) = emit(IInstruction(0b001_011, rs, rt, imm)) 74 | fun andi(rt: Reg, rs: Reg, imm: Int) = emit(IInstruction(0b001_100, rs, rt, imm)) 75 | fun ori(rt: Reg, rs: Reg, imm: Int) = emit(IInstruction(0b001_101, rs, rt, imm)) 76 | fun xori(rt: Reg, rs: Reg, imm: Int) = emit(IInstruction(0b001_110, rs, rt, imm)) 77 | fun lui(rt: Reg, imm: Int) = emit(IInstruction(0b001_111, Reg.zero, rt, imm)) 78 | 79 | fun add(rd: Reg, rs: Reg, rt: Reg) = emit(RInstruction(0, rs, rt, rd, 0, 0b100_000)) 80 | fun addu(rd: Reg, rs: Reg, rt: Reg) = emit(RInstruction(0, rs, rt, rd, 0, 0b100_001)) 81 | fun sub(rd: Reg, rs: Reg, rt: Reg) = emit(RInstruction(0, rs, rt, rd, 0, 0b100_010)) 82 | fun subu(rd: Reg, rs: Reg, rt: Reg) = emit(RInstruction(0, rs, rt, rd, 0, 0b100_011)) 83 | 84 | fun slt(rd: Reg, rs: Reg, rt: Reg) = emit(RInstruction(0, rs, rt, rd, 0, 0b101_010)) 85 | fun sltu(rd: Reg, rs: Reg, rt: Reg) = emit(RInstruction(0, rs, rt, rd, 0, 0b101_011)) 86 | fun and(rd: Reg, rs: Reg, rt: Reg) = emit(RInstruction(0, rs, rt, rd, 0, 0b100_100)) 87 | fun or(rd: Reg, rs: Reg, rt: Reg) = emit(RInstruction(0, rs, rt, rd, 0, 0b100_101)) 88 | fun xor(rd: Reg, rs: Reg, rt: Reg) = emit(RInstruction(0, rs, rt, rd, 0, 0b100_110)) 89 | fun nor(rd: Reg, rs: Reg, rt: Reg) = emit(RInstruction(0, rs, rt, rd, 0, 0b100_111)) 90 | 91 | fun sll(rd: Reg, rt: Reg, sa: Int) = emit(RInstruction(0, Reg.zero, rt, rd, sa, 0)) 92 | fun srl(rd: Reg, rt: Reg, sa: Int) = emit(RInstruction(0, Reg.zero, rt, rd, sa, 0b000_010)) 93 | fun sra(rd: Reg, rt: Reg, sa: Int) = emit(RInstruction(0, Reg.zero, rt, rd, sa, 0b000_011)) 94 | fun sllv(rd: Reg, rt: Reg, rs: Reg) = emit(RInstruction(0, rs, rt, rd, 0, 0b000_100)) 95 | fun srlv(rd: Reg, rt: Reg, rs: Reg) = emit(RInstruction(0, rs, rt, rd, 0, 0b000_110)) 96 | fun srav(rd: Reg, rt: Reg, rs: Reg) = emit(RInstruction(0, rs, rt, rd, 0, 0b000_111)) 97 | 98 | fun mult(rs: Reg, rt: Reg) = emit(RInstruction(0, rs, rt, Reg.zero, 0, 0b011_000)) 99 | fun multu(rs: Reg, rt: Reg) = emit(RInstruction(0, rs, rt, Reg.zero, 0, 0b011_001)) 100 | fun div(rs: Reg, rt: Reg) = emit(RInstruction(0, rs, rt, Reg.zero, 0, 0b011_010)) 101 | fun divu(rs: Reg, rt: Reg) = emit(RInstruction(0, rs, rt, Reg.zero, 0, 0b011_011)) 102 | fun mfhi(rd: Reg) = emit(RInstruction(0, Reg.zero, Reg.zero, rd, 0, 0b010_000)) 103 | fun mthi(rs: Reg) = emit(RInstruction(0, rs, Reg.zero, Reg.zero, 0, 0b010_001)) 104 | fun mflo(rd: Reg) = emit(RInstruction(0, Reg.zero, Reg.zero, rd, 0, 0b010_010)) 105 | fun mtlo(rs: Reg) = emit(RInstruction(0, rs, Reg.zero, Reg.zero, 0, 0b010_011)) 106 | 107 | fun j(address: Int) = emitJumpInstruction(0b000_010, address) 108 | fun jal(address: Int) = emitJumpInstruction(0b000_011, address) 109 | fun jr(rs: Reg) = emit(RInstruction(0, rs, Reg.zero, Reg.zero, 0, 0b001_000)) 110 | fun jalr(rs: Reg) = jalr(Reg.ra, rs) 111 | fun jalr(rd: Reg, rs: Reg) = emit(RInstruction(0, rs, Reg.zero, rd, 0, 0b001_001)) 112 | 113 | fun beq(rs: Reg, rt: Reg, label: Label) = emitBranchInstruction(0b000_100, rs, rt, label) 114 | fun bne(rs: Reg, rt: Reg, label: Label) = emitBranchInstruction(0b000_101, rs, rt, label) 115 | fun blez(rs: Reg, label: Label) = emitBranchInstruction(0b000_110, rs, Reg.zero, label) 116 | fun bgtz(rs: Reg, label: Label) = emitBranchInstruction(0b000_111, rs, Reg.zero, label) 117 | fun beql(rs: Reg, rt: Reg, label: Label) = emitBranchInstruction(0b010_100, rs, rt, label) 118 | fun bnel(rs: Reg, rt: Reg, label: Label) = emitBranchInstruction(0b010_101, rs, rt, label) 119 | fun blezl(rs: Reg, label: Label) = emitBranchInstruction(0b010_110, rs, Reg.zero, label) 120 | fun bgtzl(rs: Reg, label: Label) = emitBranchInstruction(0b010_111, rs, Reg.zero, label) 121 | 122 | fun bltz(rs: Reg, label: Label) = emitBranchInstruction(0b000_001, rs.id, 0b00000, label) 123 | fun bgez(rs: Reg, label: Label) = emitBranchInstruction(0b000_001, rs.id, 0b00001, label) 124 | fun bltzal(rs: Reg, label: Label) = emitBranchInstruction(0b000_001, rs.id, 0b10000, label) 125 | fun bgezal(rs: Reg, label: Label) = emitBranchInstruction(0b000_001, rs.id, 0b10001, label) 126 | fun bltzl(rs: Reg, label: Label) = emitBranchInstruction(0b000_001, rs.id, 0b00010, label) 127 | fun bgezl(rs: Reg, label: Label) = emitBranchInstruction(0b000_001, rs.id, 0b00011, label) 128 | fun bltzall(rs: Reg, label: Label) = emitBranchInstruction(0b000_001, rs.id, 0b10010, label) 129 | fun bgezall(rs: Reg, label: Label) = emitBranchInstruction(0b000_001, rs.id, 0b10011, label) 130 | 131 | fun syscall(code: Int) = emit(CodeInstruction(0, code, 0b001_100)) 132 | fun `break`(code: Int) = emit(CodeInstruction(0, code, 0b001_101)) 133 | 134 | fun tge(rs: Reg, rt: Reg, code: Int = 0x200) = emit(RInstruction(0, rs.id, rt.id, code ushr 5, code and 0x1F, 0b110_000)) 135 | fun tgeu(rs: Reg, rt: Reg, code: Int = 0x200) = 136 | emit(RInstruction(0, rs.id, rt.id, code ushr 5, code and 0x1F, 0b110_001)) 137 | 138 | fun tlt(rs: Reg, rt: Reg, code: Int = 0x200) = emit(RInstruction(0, rs.id, rt.id, code ushr 5, code and 0x1F, 0b110_010)) 139 | fun tltu(rs: Reg, rt: Reg, code: Int = 0x200) = 140 | emit(RInstruction(0, rs.id, rt.id, code ushr 5, code and 0x1F, 0b110_011)) 141 | 142 | fun teq(rs: Reg, rt: Reg, code: Int = 0x200) = emit(RInstruction(0, rs.id, rt.id, code ushr 5, code and 0x1F, 0b110_100)) 143 | fun tne(rs: Reg, rt: Reg, code: Int = 0x200) = emit(RInstruction(0, rs.id, rt.id, code ushr 5, code and 0x1F, 0b110_110)) 144 | 145 | fun tgei(rs: Reg, imm: Int) = emit(IInstruction(0b000_001, rs.id, 0b01000, imm)) 146 | fun tgeiu(rs: Reg, imm: Int) = emit(IInstruction(0b000_001, rs.id, 0b01001, imm)) 147 | fun tlti(rs: Reg, imm: Int) = emit(IInstruction(0b000_001, rs.id, 0b01010, imm)) 148 | fun tltiu(rs: Reg, imm: Int) = emit(IInstruction(0b000_001, rs.id, 0b01011, imm)) 149 | fun teqi(rs: Reg, imm: Int) = emit(IInstruction(0b000_001, rs.id, 0b01100, imm)) 150 | fun tnei(rs: Reg, imm: Int) = emit(IInstruction(0b000_001, rs.id, 0b01110, imm)) 151 | 152 | fun sync(stype: Int = 0) = emit(RInstruction(0, 0, 0, 0, stype, 0b001_111)) 153 | 154 | fun nop() = emit(NopInstruction()) 155 | 156 | // FPU (MIPS II) 157 | 158 | fun lwc1(ft: FpuReg, offset: Int, base: Reg) = emit(IInstruction(0b110_001, base.id, ft.id, offset)) 159 | fun swc1(ft: FpuReg, offset: Int, base: Reg) = emit(IInstruction(0b111_001, base.id, ft.id, offset)) 160 | 161 | fun mtc1(rt: Reg, fs: FpuReg) = emit(RInstruction(COP1, 0b00100, rt.id, fs.id, 0, 0)) 162 | fun mfc1(rt: Reg, fs: FpuReg) = emit(RInstruction(COP1, 0b00000, rt.id, fs.id, 0, 0)) 163 | fun ctc1(rt: Reg, fs: FpuReg) = emit(RInstruction(COP1, 0b00110, rt.id, fs.id, 0, 0)) 164 | fun cfc1(rt: Reg, fs: FpuReg) = emit(RInstruction(COP1, 0b00010, rt.id, fs.id, 0, 0)) 165 | 166 | val add = AddFpu() 167 | val sub = SubFpu() 168 | val mul = MulFpu() 169 | val div = DivFpu() 170 | val abs = AbsFpu() 171 | val neg = NegFpu() 172 | val sqrt = SqrtFpu() 173 | val round = RoundFpu() 174 | val trunc = TruncFpu() 175 | val ceil = CeilFpu() 176 | val floor = FloorFpu() 177 | 178 | inner class AddFpu { 179 | fun s(fd: FpuReg, fs: FpuReg, ft: FpuReg) = emit(FpuInstruction(COP1, FMT_S, ft, fs, fd, 0b000_000)) 180 | fun d(fd: FpuReg, fs: FpuReg, ft: FpuReg) = emit(FpuInstruction(COP1, FMT_D, ft, fs, fd, 0b000_000)) 181 | } 182 | 183 | inner class SubFpu { 184 | fun s(fd: FpuReg, fs: FpuReg, ft: FpuReg) = emit(FpuInstruction(COP1, FMT_S, ft, fs, fd, 0b000_001)) 185 | fun d(fd: FpuReg, fs: FpuReg, ft: FpuReg) = emit(FpuInstruction(COP1, FMT_D, ft, fs, fd, 0b000_001)) 186 | } 187 | 188 | inner class MulFpu { 189 | fun s(fd: FpuReg, fs: FpuReg, ft: FpuReg) = emit(FpuInstruction(COP1, FMT_S, ft, fs, fd, 0b000_010)) 190 | fun d(fd: FpuReg, fs: FpuReg, ft: FpuReg) = emit(FpuInstruction(COP1, FMT_D, ft, fs, fd, 0b000_010)) 191 | } 192 | 193 | inner class DivFpu { 194 | fun s(fd: FpuReg, fs: FpuReg, ft: FpuReg) = emit(FpuInstruction(COP1, FMT_S, ft, fs, fd, 0b000_011)) 195 | fun d(fd: FpuReg, fs: FpuReg, ft: FpuReg) = emit(FpuInstruction(COP1, FMT_D, ft, fs, fd, 0b000_011)) 196 | } 197 | 198 | inner class AbsFpu { 199 | fun s(fd: FpuReg, fs: FpuReg) = emit(FpuInstruction(COP1, FMT_S, FpuReg.f0, fs, fd, 0b000_101)) 200 | fun d(fd: FpuReg, fs: FpuReg) = emit(FpuInstruction(COP1, FMT_D, FpuReg.f0, fs, fd, 0b000_101)) 201 | } 202 | 203 | inner class NegFpu { 204 | fun s(fd: FpuReg, fs: FpuReg) = emit(FpuInstruction(COP1, FMT_S, FpuReg.f0, fs, fd, 0b000_111)) 205 | fun d(fd: FpuReg, fs: FpuReg) = emit(FpuInstruction(COP1, FMT_D, FpuReg.f0, fs, fd, 0b000_111)) 206 | } 207 | 208 | inner class SqrtFpu { 209 | fun s(fd: FpuReg, fs: FpuReg) = emit(FpuInstruction(COP1, FMT_S, FpuReg.f0, fs, fd, 0b000_100)) 210 | fun d(fd: FpuReg, fs: FpuReg) = emit(FpuInstruction(COP1, FMT_D, FpuReg.f0, fs, fd, 0b000_100)) 211 | } 212 | 213 | inner class RoundFpu { 214 | val w = RoundWFpu() 215 | 216 | inner class RoundWFpu { 217 | fun s(fd: FpuReg, fs: FpuReg) = emit(FpuInstruction(COP1, FMT_S, FpuReg.f0, fs, fd, 0b001_100)) 218 | fun d(fd: FpuReg, fs: FpuReg) = emit(FpuInstruction(COP1, FMT_D, FpuReg.f0, fs, fd, 0b001_100)) 219 | } 220 | } 221 | 222 | inner class TruncFpu { 223 | val w = TruncWFpu() 224 | 225 | inner class TruncWFpu { 226 | fun s(fd: FpuReg, fs: FpuReg) = emit(FpuInstruction(COP1, FMT_S, FpuReg.f0, fs, fd, 0b001_101)) 227 | fun d(fd: FpuReg, fs: FpuReg) = emit(FpuInstruction(COP1, FMT_D, FpuReg.f0, fs, fd, 0b001_101)) 228 | } 229 | } 230 | 231 | inner class CeilFpu { 232 | val w = CeilWFpu() 233 | 234 | inner class CeilWFpu { 235 | fun s(fd: FpuReg, fs: FpuReg) = emit(FpuInstruction(COP1, FMT_S, FpuReg.f0, fs, fd, 0b001_110)) 236 | fun d(fd: FpuReg, fs: FpuReg) = emit(FpuInstruction(COP1, FMT_D, FpuReg.f0, fs, fd, 0b001_110)) 237 | } 238 | } 239 | 240 | inner class FloorFpu { 241 | val w = FloorWFpu() 242 | 243 | inner class FloorWFpu { 244 | fun s(fd: FpuReg, fs: FpuReg) = emit(FpuInstruction(COP1, FMT_S, FpuReg.f0, fs, fd, 0b001_111)) 245 | fun d(fd: FpuReg, fs: FpuReg) = emit(FpuInstruction(COP1, FMT_D, FpuReg.f0, fs, fd, 0b001_111)) 246 | } 247 | } 248 | 249 | val c = CompareFpu() 250 | 251 | inner class CompareFpu { 252 | val eq = CondEq() 253 | val le = CondLe() 254 | val lt = CondLt() 255 | 256 | inner class CondEq { 257 | fun s(fs: FpuReg, ft: FpuReg) = emit(FpuInstruction(COP1, FMT_S, ft, fs, FpuReg.f0, 0b11_0010)) 258 | fun d(fs: FpuReg, ft: FpuReg) = emit(FpuInstruction(COP1, FMT_D, ft, fs, FpuReg.f0, 0b11_0010)) 259 | } 260 | 261 | inner class CondLe { 262 | fun s(fs: FpuReg, ft: FpuReg) = emit(FpuInstruction(COP1, FMT_S, ft, fs, FpuReg.f0, 0b11_1110)) 263 | fun d(fs: FpuReg, ft: FpuReg) = emit(FpuInstruction(COP1, FMT_D, ft, fs, FpuReg.f0, 0b11_1110)) 264 | } 265 | 266 | inner class CondLt { 267 | fun s(fs: FpuReg, ft: FpuReg) = emit(FpuInstruction(COP1, FMT_S, ft, fs, FpuReg.f0, 0b11_1100)) 268 | fun d(fs: FpuReg, ft: FpuReg) = emit(FpuInstruction(COP1, FMT_D, ft, fs, FpuReg.f0, 0b11_1100)) 269 | } 270 | } 271 | 272 | val cvt = CvtFpu() 273 | 274 | inner class CvtFpu { 275 | val s = CvtS() 276 | val d = CvtD() 277 | val w = CvtW() 278 | 279 | inner class CvtS { 280 | fun d(fd: FpuReg, fs: FpuReg) = emit(FpuInstruction(COP1, FMT_D, FpuReg.f0, fs, fd, 0b100_000)) 281 | fun w(fd: FpuReg, fs: FpuReg) = emit(FpuInstruction(COP1, FMT_W, FpuReg.f0, fs, fd, 0b100_000)) 282 | } 283 | 284 | inner class CvtD { 285 | fun s(fd: FpuReg, fs: FpuReg) = emit(FpuInstruction(COP1, FMT_S, FpuReg.f0, fs, fd, 0b100_001)) 286 | fun w(fd: FpuReg, fs: FpuReg) = emit(FpuInstruction(COP1, FMT_W, FpuReg.f0, fs, fd, 0b100_001)) 287 | } 288 | 289 | inner class CvtW { 290 | fun s(fd: FpuReg, fs: FpuReg) = emit(FpuInstruction(COP1, FMT_S, FpuReg.f0, fs, fd, 0b100_100)) 291 | fun d(fd: FpuReg, fs: FpuReg) = emit(FpuInstruction(COP1, FMT_D, FpuReg.f0, fs, fd, 0b100_100)) 292 | } 293 | } 294 | 295 | val mov = MovFpu() 296 | 297 | inner class MovFpu { 298 | fun s(fd: FpuReg, fs: FpuReg) = emit(FpuInstruction(COP1, FMT_S, FpuReg.f0, fs, fd, 0b000_110)) 299 | fun d(fd: FpuReg, fs: FpuReg) = emit(FpuInstruction(COP1, FMT_D, FpuReg.f0, fs, fd, 0b000_110)) 300 | } 301 | 302 | fun bc1f(label: Label) = emitBranchInstruction(COP1, 0b01000, 0b00, label) 303 | fun bc1t(label: Label) = emitBranchInstruction(COP1, 0b01000, 0b01, label) 304 | fun bc1tl(label: Label) = emitBranchInstruction(COP1, 0b01000, 0b11, label) 305 | fun bc1fl(label: Label) = emitBranchInstruction(COP1, 0b01000, 0b10, label) 306 | 307 | // Pseudo instructions / aliases 308 | 309 | fun b(label: Label) { 310 | beq(Reg.zero, Reg.zero, label) 311 | } 312 | 313 | fun blt(rt: Reg, rs: Reg, label: Label) { 314 | slt(Reg.at, rt, rs) 315 | bne(Reg.at, Reg.zero, label) 316 | } 317 | 318 | fun bge(rt: Reg, rs: Reg, label: Label) { 319 | slt(Reg.at, rt, rs) 320 | beq(Reg.at, Reg.zero, label) 321 | } 322 | 323 | fun bgt(rt: Reg, rs: Reg, label: Label) { 324 | slt(Reg.at, rs, rt) 325 | bne(Reg.at, Reg.zero, label) 326 | } 327 | 328 | fun ble(rt: Reg, rs: Reg, label: Label) { 329 | slt(Reg.at, rs, rt) 330 | beq(Reg.at, Reg.zero, label) 331 | } 332 | 333 | fun neg(rd: Reg, rt: Reg) = sub(rd, Reg.zero, rt) 334 | 335 | fun not(rd: Reg, rs: Reg) = nor(rd, rs, Reg.zero) 336 | 337 | fun la(rs: Reg, imm: Int) { 338 | lui(rs, imm ushr 16) 339 | ori(rs, rs, imm and 0xFFFF) 340 | } 341 | 342 | fun li(rs: Reg, imm: Int) { 343 | if (imm < 0) { 344 | addiu(rs, Reg.zero, imm) 345 | } else { 346 | ori(rs, Reg.zero, imm) 347 | } 348 | } 349 | 350 | fun move(rd: Reg, rs: Reg) { 351 | addu(rd, rs, Reg.zero) 352 | } 353 | 354 | fun sge(rd: Reg, rs: Reg, rt: Reg) { 355 | slt(rd, rs, rt) 356 | li(Reg.at, 1) 357 | subu(rd, Reg.at, rd) 358 | } 359 | 360 | fun sgt(rd: Reg, rs: Reg, rt: Reg) { 361 | slt(rd, rt, rs) 362 | } 363 | 364 | fun data(data: Int) = emit(DataPseudoInstruction(data)) 365 | 366 | fun label(): Label { 367 | val label = Label() 368 | label.address = virtualPc 369 | return label 370 | } 371 | 372 | fun label(label: Label) { 373 | label.address = virtualPc 374 | } 375 | 376 | private fun emitBranchInstruction(opcode: Int, rs: Reg, rt: Reg, label: Label) { 377 | emitBranchInstruction(opcode, rs.id, rt.id, label) 378 | } 379 | 380 | private fun emitBranchInstruction(opcode: Int, rs: Int, rt: Int, label: Label) { 381 | val instrVirtualPc = virtualPc 382 | emit(IInstruction(opcode, rs, rt) { (label.address - instrVirtualPc - 0x4) / 0x4 }) 383 | } 384 | 385 | private fun emitJumpInstruction(opcode: Int, address: Int) { 386 | val instrVirtualPc = virtualPc + 0x4 387 | if (address and 0xf0000000.toInt() != instrVirtualPc and 0xf0000000.toInt()) { 388 | error("can't calculate jump address because address is too far from current pc (pc bits 31-28 mismatch)") 389 | } 390 | if (address and 0b11 != 0) { 391 | error("can't calculate jump address because last two bits of address are != 0") 392 | } 393 | emit(JInstruction(opcode, address and 0xFFFFFFF ushr 2)) 394 | } 395 | 396 | fun emit(instruction: Instruction) { 397 | instructions.add(instruction) 398 | virtualPc += 4 399 | } 400 | 401 | fun assembleAsList(): List { 402 | return when (endianness) { 403 | Endianness.Little -> instructions.map { it.assemble().toLittleEndian() } 404 | Endianness.Big -> instructions.map { it.assemble() } 405 | } 406 | } 407 | 408 | fun assembleAsHexString(): String { 409 | return assembleAsList().joinToString(separator = "") { it.toHex() } 410 | } 411 | 412 | fun assembleAsByteArray(): ByteArray { 413 | val instr = assembleAsList() 414 | val out = ByteArrayOutputStream(instr.size * 4) 415 | instr.forEach { 416 | out.write(it ushr 24) 417 | out.write(it ushr 16 and 0xFF) 418 | out.write(it ushr 8 and 0xFF) 419 | out.write(it and 0xFF) 420 | } 421 | return out.toByteArray() 422 | } 423 | } 424 | 425 | class Label { 426 | var address: Int = -1 427 | get() { 428 | if (field == -1) error("label was not assigned") 429 | return field 430 | } 431 | set(value) { 432 | if (field != -1) error("label was already assigned") 433 | field = value 434 | } 435 | } 436 | 437 | class RInstruction internal constructor( 438 | private val opcode: Int, 439 | private val rs: Int, 440 | private val rt: Int, 441 | private val rd: Int, 442 | private val shift: Int = 0, 443 | private val funct: Int = 0, 444 | ) : Instruction { 445 | constructor(opcode: Int, rs: Reg, rt: Reg, rd: Reg, shift: Int = 0, funct: Int = 0) 446 | : this(opcode, rs.id, rt.id, rd.id, shift, funct) 447 | 448 | override fun assemble(): Int { 449 | if (opcode > 0x3F) error("opcode value is too big: $opcode") 450 | if (shift > 0x1F) error("shift value is too big: $shift") 451 | if (funct > 0x3F) error("funct value is too big: $funct") 452 | return (opcode shl 26) or (rs shl 21) or (rt shl 16) or (rd shl 11) or (shift shl 6) or funct 453 | } 454 | } 455 | 456 | class IInstruction internal constructor( 457 | private val opcode: Int, 458 | private val rs: Int, 459 | private val rt: Int, 460 | private val imm: () -> Int, 461 | ) : Instruction { 462 | internal constructor(opcode: Int, rs: Int, rt: Int, imm: Int) : this(opcode, rs, rt, { imm }) 463 | constructor(opcode: Int, rs: Reg, rt: Reg, imm: Int) : this(opcode, rs, rt, { imm }) 464 | constructor(opcode: Int, rs: Reg, rt: Reg, imm: () -> Int) : this(opcode, rs.id, rt.id, imm) 465 | 466 | override fun assemble(): Int { 467 | if (opcode > 0x3F) error("opcode value is too big: $opcode") 468 | return (opcode shl 26) or (rs shl 21) or (rt shl 16) or (imm().toShort().toInt() and 0xFFFF) 469 | } 470 | } 471 | 472 | class JInstruction( 473 | private val opcode: Int, 474 | private val address: () -> Int, 475 | ) : Instruction { 476 | constructor(opcode: Int, address: Int) : this(opcode, { address }) 477 | 478 | override fun assemble(): Int { 479 | if (opcode > 0x3F) error("opcode value is too big: $opcode") 480 | if (Integer.compareUnsigned(address(), 0x3FFFFFF) > 0) error("address value is too big: 0x${address().toHex()}") 481 | return (opcode shl 26) or address() 482 | } 483 | } 484 | 485 | class CodeInstruction( 486 | private val opcode: Int, 487 | private val code: Int, 488 | private val funct: Int = 0, 489 | ) : Instruction { 490 | override fun assemble(): Int { 491 | if (opcode > 0x3F) error("opcode value is too big: $opcode") 492 | if (code > 0xFFFF) error("code value is too big: $opcode") 493 | if (funct > 0x3F) error("funct value is too big: $funct") 494 | return (opcode shl 26) or (code and 0xFFFF shl 6) or funct 495 | } 496 | } 497 | 498 | class FpuInstruction internal constructor( 499 | private val opcode: Int, 500 | private val fmt: Int, 501 | private val ft: Int, 502 | private val fs: Int, 503 | private val fd: Int, 504 | private val funct: Int = 0, 505 | ) : Instruction { 506 | constructor(opcode: Int, fmt: Int, ft: FpuReg, fs: FpuReg, fd: FpuReg, funct: Int = 0) 507 | : this(opcode, fmt, ft.id, fs.id, fd.id, funct) 508 | 509 | override fun assemble(): Int { 510 | if (opcode > 0x3F) error("opcode value is too big: $opcode") 511 | if (fmt > 0x1F) error("fmt value is too big: $fmt") 512 | if (funct > 0x3F) error("funct value is too big: $funct") 513 | return (opcode shl 26) or (fmt shl 21) or (ft shl 16) or (fs shl 11) or (fd shl 6) or funct 514 | } 515 | } 516 | 517 | class DataPseudoInstruction( 518 | private val data: Int, 519 | ) : Instruction { 520 | override fun assemble(): Int = data 521 | } 522 | 523 | class NopInstruction : Instruction { 524 | override fun assemble(): Int = 0 525 | } 526 | 527 | interface Instruction { 528 | fun assemble(): Int 529 | } 530 | 531 | enum class Endianness { 532 | Little, Big 533 | } 534 | 535 | @Suppress("EnumEntryName", "unused") 536 | enum class Reg(val id: Int) { 537 | zero(0), 538 | at(1), 539 | v0(2), v1(3), 540 | a0(4), a1(5), a2(6), a3(7), 541 | t0(8), t1(9), t2(10), t3(11), t4(12), t5(13), t6(14), t7(15), t8(24), t9(25), 542 | s0(16), s1(17), s2(18), s3(19), s4(20), s5(21), s6(22), s7(23), 543 | k0(26), k1(27), 544 | gp(28), sp(29), fp(30), ra(31); 545 | } 546 | 547 | @Suppress("EnumEntryName", "unused") 548 | enum class FpuReg(val id: Int) { 549 | f0(0), f1(1), f2(2), f3(3), f4(4), 550 | f5(5), f6(6), f7(7), f8(8), f9(9), 551 | f10(10), f11(11), f12(12), f13(13), f14(14), 552 | f15(15), f16(16), f17(17), f18(18), f19(19), 553 | f20(20), f21(21), f22(22), f23(23), f24(24), 554 | f25(25), f26(26), f27(27), f28(28), f29(29), 555 | f30(30), f31(31) 556 | } 557 | 558 | internal fun Int.toHex() = String.format("%08X", this) 559 | 560 | internal fun Int.toLittleEndian(): Int { 561 | return (this and 0xFF shl 24) or 562 | (this and 0xFF00 shl 8) or 563 | (this and 0xFF0000 ushr 8) or 564 | (this and 0xFF000000.toInt() ushr 24) 565 | } 566 | -------------------------------------------------------------------------------- /src/test/kotlin/kmips/AssemblerTest.kt: -------------------------------------------------------------------------------- 1 | package kmips 2 | 3 | import kmips.FpuReg.f12 4 | import kmips.FpuReg.f24 5 | import kmips.FpuReg.f4 6 | import kmips.Reg.a0 7 | import kmips.Reg.s0 8 | import kmips.Reg.sp 9 | import kmips.Reg.t0 10 | import kmips.Reg.zero 11 | import org.junit.jupiter.api.Assertions.assertArrayEquals 12 | import org.junit.jupiter.api.Assertions.assertEquals 13 | import org.junit.jupiter.api.Test 14 | import org.junit.jupiter.api.assertThrows 15 | 16 | class AssemblerTest { 17 | @Test 18 | fun testLb() = testInstruction("809000CD", { lb(s0, 0xCD, a0) }) 19 | 20 | @Test 21 | fun testLbu() = testInstruction("909000CD", { lbu(s0, 0xCD, a0) }) 22 | 23 | @Test 24 | fun testSb() = testInstruction("A09000CD", { sb(s0, 0xCD, a0) }) 25 | 26 | @Test 27 | fun testLh() = testInstruction("849000CD", { lh(s0, 0xCD, a0) }) 28 | 29 | @Test 30 | fun testLhu() = testInstruction("949000CD", { lhu(s0, 0xCD, a0) }) 31 | 32 | @Test 33 | fun testSh() = testInstruction("A49000CD", { sh(s0, 0xCD, a0) }) 34 | 35 | @Test 36 | fun testLw() = testInstruction("8C9000CD", { lw(s0, 0xCD, a0) }) 37 | 38 | @Test 39 | fun testSw() = testInstruction("AC9000CD", { sw(s0, 0xCD, a0) }) 40 | 41 | @Test 42 | fun testLwl() = testInstruction("889000CD", { lwl(s0, 0xCD, a0) }) 43 | 44 | @Test 45 | fun testLwr() = testInstruction("989000CD", { lwr(s0, 0xCD, a0) }) 46 | 47 | @Test 48 | fun testSwl() = testInstruction("A89000CD", { swl(s0, 0xCD, a0) }) 49 | 50 | @Test 51 | fun testSwr() = testInstruction("B89000CD", { swr(s0, 0xCD, a0) }) 52 | 53 | @Test 54 | fun testLl() = testInstruction("C09000CD", { ll(s0, 0xCD, a0) }) 55 | 56 | @Test 57 | fun testSc() = testInstruction("E09000CD", { sc(s0, 0xCD, a0) }) 58 | 59 | @Test 60 | fun testAddi() = testInstruction("209000CD", { addi(s0, a0, 0xCD) }) 61 | 62 | @Test 63 | fun testAddiu() = testInstruction("249000CD", { addiu(s0, a0, 0xCD) }) 64 | 65 | @Test 66 | fun testSlti() = testInstruction("289000CD", { slti(s0, a0, 0xCD) }) 67 | 68 | @Test 69 | fun testSltiu() = testInstruction("2C9000CD", { sltiu(s0, a0, 0xCD) }) 70 | 71 | @Test 72 | fun testAndi() = testInstruction("309000CD", { andi(s0, a0, 0xCD) }) 73 | 74 | @Test 75 | fun testOri() = testInstruction("349000CD", { ori(s0, a0, 0xCD) }) 76 | 77 | @Test 78 | fun testXori() = testInstruction("389000CD", { xori(s0, a0, 0xCD) }) 79 | 80 | @Test 81 | fun testLui() = testInstruction("3C1000CD", { lui(s0, 0xCD) }) 82 | 83 | @Test 84 | fun testAdd() = testInstruction("00888020", { add(s0, a0, t0) }) 85 | 86 | @Test 87 | fun testAddu() = testInstruction("00888021", { addu(s0, a0, t0) }) 88 | 89 | @Test 90 | fun testSub() = testInstruction("00888022", { sub(s0, a0, t0) }) 91 | 92 | @Test 93 | fun testSubu() = testInstruction("00888023", { subu(s0, a0, t0) }) 94 | 95 | @Test 96 | fun testSlt() = testInstruction("0088802A", { slt(s0, a0, t0) }) 97 | 98 | @Test 99 | fun testSltu() = testInstruction("0088802B", { sltu(s0, a0, t0) }) 100 | 101 | @Test 102 | fun testAnd() = testInstruction("00888024", { and(s0, a0, t0) }) 103 | 104 | @Test 105 | fun testOr() = testInstruction("00888025", { or(s0, a0, t0) }) 106 | 107 | @Test 108 | fun testXor() = testInstruction("00888026", { xor(s0, a0, t0) }) 109 | 110 | @Test 111 | fun testNor() = testInstruction("00888027", { nor(s0, a0, t0) }) 112 | 113 | @Test 114 | fun testSll() = testInstruction("00048440", { sll(s0, a0, 0x11) }) 115 | 116 | @Test 117 | fun testSrl() = testInstruction("00048442", { srl(s0, a0, 0x11) }) 118 | 119 | @Test 120 | fun testSra() = testInstruction("00048443", { sra(s0, a0, 0x11) }) 121 | 122 | @Test 123 | fun testSllv() = testInstruction("01048004", { sllv(s0, a0, t0) }) 124 | 125 | @Test 126 | fun testSrlv() = testInstruction("01048006", { srlv(s0, a0, t0) }) 127 | 128 | @Test 129 | fun testSrav() = testInstruction("01048007", { srav(s0, a0, t0) }) 130 | 131 | @Test 132 | fun testMult() = testInstruction("02040018", { mult(s0, a0) }) 133 | 134 | @Test 135 | fun testMultu() = testInstruction("02040019", { multu(s0, a0) }) 136 | 137 | @Test 138 | fun testDiv() = testInstruction("0204001A", { div(s0, a0) }) 139 | 140 | @Test 141 | fun testDivu() = testInstruction("0204001B", { divu(s0, a0) }) 142 | 143 | @Test 144 | fun testMfhi() = testInstruction("00008010", { mfhi(s0) }) 145 | 146 | @Test 147 | fun testMthi() = testInstruction("02000011", { mthi(s0) }) 148 | 149 | @Test 150 | fun testMflo() = testInstruction("00008012", { mflo(s0) }) 151 | 152 | @Test 153 | fun testMtlo() = testInstruction("02000013", { mtlo(s0) }) 154 | 155 | @Test 156 | fun testJ() = testInstruction("0A3F48E8", { j(0x08FD23A0) }, 0x0896D6E4) 157 | 158 | @Test 159 | fun testJal() = testInstruction("0E3F48E8", { jal(0x08FD23A0) }, 0x0896D6E4) 160 | 161 | @Test 162 | fun testJr() = testInstruction("00800008", { jr(a0) }) 163 | 164 | @Test 165 | fun testJalr() = testInstruction("0200F809", { jalr(s0) }) 166 | 167 | @Test 168 | fun testJalr2() = testInstruction("02002009", { jalr(a0, s0) }) 169 | 170 | @Test 171 | fun testBeq() = testBranchInstruction("12040001") { beq(s0, a0, it) } 172 | 173 | @Test 174 | fun testBne() = testBranchInstruction("16040001") { bne(s0, a0, it) } 175 | 176 | @Test 177 | fun testBlez() = testBranchInstruction("1A000001") { blez(s0, it) } 178 | 179 | @Test 180 | fun testBgtz() = testBranchInstruction("1E000001") { bgtz(s0, it) } 181 | 182 | @Test 183 | fun testBeql() = testBranchInstruction("52040001") { beql(s0, a0, it) } 184 | 185 | @Test 186 | fun testBnel() = testBranchInstruction("56040001") { bnel(s0, a0, it) } 187 | 188 | @Test 189 | fun testBlezl() = testBranchInstruction("5A000001") { blezl(s0, it) } 190 | 191 | @Test 192 | fun testBgtzl() = testBranchInstruction("5E000001") { bgtzl(s0, it) } 193 | 194 | @Test 195 | fun testBltz() = testBranchInstruction("06000001") { bltz(s0, it) } 196 | 197 | @Test 198 | fun testBgez() = testBranchInstruction("06010001") { bgez(s0, it) } 199 | 200 | @Test 201 | fun testBltzal() = testBranchInstruction("06100001") { bltzal(s0, it) } 202 | 203 | @Test 204 | fun testBgezal() = testBranchInstruction("06110001") { bgezal(s0, it) } 205 | 206 | @Test 207 | fun testBltzl() = testBranchInstruction("06020001") { bltzl(s0, it) } 208 | 209 | @Test 210 | fun testBgezl() = testBranchInstruction("06030001") { bgezl(s0, it) } 211 | 212 | @Test 213 | fun testBltzall() = testBranchInstruction("06120001") { bltzall(s0, it) } 214 | 215 | @Test 216 | fun testBgezall() = testBranchInstruction("06130001") { bgezall(s0, it) } 217 | 218 | @Test 219 | fun testSyscall() = testInstruction("0033734C", { syscall(0xCDCD) }) 220 | 221 | @Test 222 | fun testBreak() = testInstruction("0033734D", { `break`(0xCDCD) }) 223 | 224 | @Test 225 | fun testTge() = testInstruction("02048030", { tge(s0, a0) }) 226 | 227 | @Test 228 | fun testTgeu() = testInstruction("02048031", { tgeu(s0, a0) }) 229 | 230 | @Test 231 | fun testTlt() = testInstruction("02048032", { tlt(s0, a0) }) 232 | 233 | @Test 234 | fun testTltu() = testInstruction("02048033", { tltu(s0, a0) }) 235 | 236 | @Test 237 | fun testTeq() = testInstruction("02048034", { teq(s0, a0) }) 238 | 239 | @Test 240 | fun testTne() = testInstruction("02048036", { tne(s0, a0) }) 241 | 242 | @Test 243 | fun testTgei() = testInstruction("060800CD", { tgei(s0, 0xCD) }) 244 | 245 | @Test 246 | fun testTgeiu() = testInstruction("060900CD", { tgeiu(s0, 0xCD) }) 247 | 248 | @Test 249 | fun testTlti() = testInstruction("060A00CD", { tlti(s0, 0xCD) }) 250 | 251 | @Test 252 | fun testTltiu() = testInstruction("060B00CD", { tltiu(s0, 0xCD) }) 253 | 254 | @Test 255 | fun testTeqi() = testInstruction("060C00CD", { teqi(s0, 0xCD) }) 256 | 257 | @Test 258 | fun testTnei() = testInstruction("060E00CD", { tnei(s0, 0xCD) }) 259 | 260 | @Test 261 | fun testSync() = testInstruction("0000000F", { sync(0) }) 262 | 263 | @Test 264 | fun testNop() = testInstruction("00000000", { nop() }) 265 | 266 | @Test 267 | fun testLwc1() = testInstruction("C48C00CD", { lwc1(f12, 0xCD, a0) }) 268 | 269 | @Test 270 | fun testSwc1() = testInstruction("E48C00CD", { swc1(f12, 0xCD, a0) }) 271 | 272 | @Test 273 | fun testMtc1() = testInstruction("44906000", { mtc1(s0, f12) }) 274 | 275 | @Test 276 | fun testMfc1() = testInstruction("44106000", { mfc1(s0, f12) }) 277 | 278 | @Test 279 | fun testCtc1() = testInstruction("44D06000", { ctc1(s0, f12) }) 280 | 281 | @Test 282 | fun testCfc1() = testInstruction("44506000", { cfc1(s0, f12) }) 283 | 284 | @Test 285 | fun testFpuAddS() = testInstruction("46186100", { add.s(f4, f12, f24) }) 286 | 287 | @Test 288 | fun testFpuAddD() = testInstruction("46386100", { add.d(f4, f12, f24) }) 289 | 290 | @Test 291 | fun testFpuSubS() = testInstruction("46186101", { sub.s(f4, f12, f24) }) 292 | 293 | @Test 294 | fun testFpuSubD() = testInstruction("46386101", { sub.d(f4, f12, f24) }) 295 | 296 | @Test 297 | fun testFpuMulS() = testInstruction("46186102", { mul.s(f4, f12, f24) }) 298 | 299 | @Test 300 | fun testFpuMulD() = testInstruction("46386102", { mul.d(f4, f12, f24) }) 301 | 302 | @Test 303 | fun testFpuDivS() = testInstruction("46186103", { div.s(f4, f12, f24) }) 304 | 305 | @Test 306 | fun testFpuDivD() = testInstruction("46386103", { div.d(f4, f12, f24) }) 307 | 308 | @Test 309 | fun testFpuAbsS() = testInstruction("46006105", { abs.s(f4, f12) }) 310 | 311 | @Test 312 | fun testFpuAbsD() = testInstruction("46206105", { abs.d(f4, f12) }) 313 | 314 | @Test 315 | fun testFpuNegS() = testInstruction("46006107", { neg.s(f4, f12) }) 316 | 317 | @Test 318 | fun testFpuNegD() = testInstruction("46206107", { neg.d(f4, f12) }) 319 | 320 | @Test 321 | fun testFpuSqrtS() = testInstruction("46006104", { sqrt.s(f4, f12) }) 322 | 323 | @Test 324 | fun testFpuSqrtD() = testInstruction("46206104", { sqrt.d(f4, f12) }) 325 | 326 | @Test 327 | fun testFpuRoundWS() = testInstruction("4600610C", { round.w.s(f4, f12) }) 328 | 329 | @Test 330 | fun testFpuRoundWD() = testInstruction("4620610C", { round.w.d(f4, f12) }) 331 | 332 | @Test 333 | fun testFpuTruncWS() = testInstruction("4600610D", { trunc.w.s(f4, f12) }) 334 | 335 | @Test 336 | fun testFpuTruncWD() = testInstruction("4620610D", { trunc.w.d(f4, f12) }) 337 | 338 | @Test 339 | fun testFpuCeilWS() = testInstruction("4600610E", { ceil.w.s(f4, f12) }) 340 | 341 | @Test 342 | fun testFpuCeilWD() = testInstruction("4620610E", { ceil.w.d(f4, f12) }) 343 | 344 | @Test 345 | fun testFpuFloorWS() = testInstruction("4600610F", { floor.w.s(f4, f12) }) 346 | 347 | @Test 348 | fun testFpuFloorWD() = testInstruction("4620610F", { floor.w.d(f4, f12) }) 349 | 350 | @Test 351 | fun testFpuCondEqS() = testInstruction("460C2032", { c.eq.s(f4, f12) }) 352 | 353 | @Test 354 | fun testFpuCondEqD() = testInstruction("462C2032", { c.eq.d(f4, f12) }) 355 | 356 | @Test 357 | fun testFpuCondLeS() = testInstruction("460C203E", { c.le.s(f4, f12) }) 358 | 359 | @Test 360 | fun testFpuCondLeD() = testInstruction("462C203E", { c.le.d(f4, f12) }) 361 | 362 | @Test 363 | fun testFpuCondLtS() = testInstruction("460C203C", { c.lt.s(f4, f12) }) 364 | 365 | @Test 366 | fun testFpuCondLtD() = testInstruction("462C203C", { c.lt.d(f4, f12) }) 367 | 368 | @Test 369 | fun testFpuCvtSD() = testInstruction("46206120", { cvt.s.d(f4, f12) }) 370 | 371 | @Test 372 | fun testFpuCvtSW() = testInstruction("46806120", { cvt.s.w(f4, f12) }) 373 | 374 | @Test 375 | fun testFpuCvtDS() = testInstruction("46006121", { cvt.d.s(f4, f12) }) 376 | 377 | @Test 378 | fun testFpuCvtDW() = testInstruction("46806121", { cvt.d.w(f4, f12) }) 379 | 380 | @Test 381 | fun testFpuCvtWS() = testInstruction("46006124", { cvt.w.s(f4, f12) }) 382 | 383 | @Test 384 | fun testFpuCvtWD() = testInstruction("46206124", { cvt.w.d(f4, f12) }) 385 | 386 | @Test 387 | fun testFpuMovS() = testInstruction("46006106", { mov.s(f4, f12) }) 388 | 389 | @Test 390 | fun testFpuMovD() = testInstruction("46206106", { mov.d(f4, f12) }) 391 | 392 | @Test 393 | fun testBc1f() = testBranchInstruction("45000001") { bc1f(it) } 394 | 395 | @Test 396 | fun testBc1t() = testBranchInstruction("45010001") { bc1t(it) } 397 | 398 | @Test 399 | fun testBc1tl() = testBranchInstruction("45030001") { bc1tl(it) } 400 | 401 | @Test 402 | fun testBc1fl() = testBranchInstruction("45020001") { bc1fl(it) } 403 | 404 | @Test 405 | fun testB() = testBranchInstruction("10000001") { b(it) } 406 | 407 | @Test 408 | fun testBlt() = testBranchInstruction("0090082A14200001") { blt(a0, s0, it) } 409 | 410 | @Test 411 | fun testBge() = testBranchInstruction("0090082A10200001") { bge(a0, s0, it) } 412 | 413 | @Test 414 | fun testBgt() = testBranchInstruction("0204082A14200001") { bgt(a0, s0, it) } 415 | 416 | @Test 417 | fun testBle() = testBranchInstruction("0204082A10200001") { ble(a0, s0, it) } 418 | 419 | @Test 420 | fun testNeg() = testInstruction("00048022", { neg(s0, a0) }) 421 | 422 | @Test 423 | fun testNot() = testInstruction("00808027", { not(s0, a0) }) 424 | 425 | @Test 426 | fun testLa() = testInstruction("3C10ABAB3610CDCD", { la(s0, 0xABABCDCD.toInt()) }) 427 | 428 | @Test 429 | fun testLiAsOr() = testInstruction("341000CD", { li(s0, 0xCD) }) 430 | 431 | @Test 432 | fun testLiAsOr2() = testInstruction("3410FFF0", { li(s0, 0xFFF0) }) 433 | 434 | @Test 435 | fun testLiAsAddiu() = testInstruction("2410FFF0", { li(s0, -16) }) 436 | 437 | @Test 438 | fun testMove() = testInstruction("00808021", { move(s0, a0) }) 439 | 440 | @Test 441 | fun testSge() = testInstruction("0088802A3401000100308023", { sge(s0, a0, t0) }) 442 | 443 | @Test 444 | fun testSgt() = testInstruction("0104802A", { sgt(s0, a0, t0) }) 445 | 446 | @Test 447 | fun testJIllegalLsbBits() { 448 | assertThrows { testInstruction("0A3F48E8", { j(0x08FD23A1) }, 0x0896D6E4) } 449 | } 450 | 451 | @Test 452 | fun testJIllegalMsbBits() { 453 | assertThrows { testInstruction("0A3F48E8", { j(0xF8FD23A0.toInt()) }, 0x0896D6E4) } 454 | } 455 | 456 | @Test 457 | fun testAddSigned() = testInstruction("23BDFFFC", { addi(sp, sp, -4) }) 458 | 459 | @Test 460 | fun testData() = testInstruction("AABBCCDD", { data(0xAABBCCDD.toInt()) }) 461 | 462 | private fun testInstruction(expected: String, instruction: Assembler.() -> Unit, startPc: Int = 0) { 463 | val assembler = Assembler(startPc, Endianness.Big) 464 | assembler.instruction() 465 | assertEquals(expected, assembler.assembleAsHexString()) 466 | } 467 | 468 | private fun testBranchInstruction(expected: String, instruction: Assembler.(label: Label) -> Unit) { 469 | val assembler = Assembler(0, Endianness.Big) 470 | val testLabel = Label() 471 | assembler.instruction(testLabel) 472 | assembler.nop() 473 | assembler.label(testLabel) 474 | assertEquals(expected + "00000000", assembler.assembleAsHexString()) 475 | } 476 | 477 | @Test 478 | fun testBranches() { 479 | val result = assemble(0x0896D6E4) { 480 | val label = Label() 481 | beq(a0, a0, label) 482 | nop() 483 | add(a0, a0, a0) 484 | label(label) 485 | add(s0, s0, s0) 486 | }.joinToString(separator = " ") { it.toHex() } 487 | assertEquals("02008410 00000000 20208400 20801002", result) 488 | } 489 | 490 | @Test 491 | fun testLabel() { 492 | val result = assemble(0x8896D6E4.toInt()) { 493 | add(a0, a0, a0) 494 | val label = label() 495 | add(s0, s0, s0) 496 | beq(a0, a0, label) 497 | nop() 498 | }.joinToString(separator = " ") { it.toHex() } 499 | assertEquals("20208400 20801002 FEFF8410 00000000", result) 500 | } 501 | 502 | @Test 503 | fun testAssembleHelper() { 504 | val result = assemble(endianness = Endianness.Big) { 505 | sw(s0, 0xCD, a0) 506 | } 507 | assertArrayEquals(arrayOf(0xAC9000CD.toInt()), result.toTypedArray()) 508 | } 509 | 510 | @Test 511 | fun testAssembleAsHexStringHelper() { 512 | val result = assembleAsHexString(endianness = Endianness.Big) { 513 | sw(s0, 0xCD, a0) 514 | } 515 | assertEquals("AC9000CD", result) 516 | } 517 | 518 | @Test 519 | fun testAssembleAsByteArrayHelper() { 520 | val result = assembleAsByteArray(endianness = Endianness.Big) { 521 | sw(s0, 0xCD, a0) 522 | } 523 | assertArrayEquals(arrayOf(0xAC, 0x90, 0x00, 0xCD).map { it.toByte() }.toByteArray(), result) 524 | } 525 | } 526 | 527 | class LabelTest { 528 | @Test 529 | fun testNotAssigned() { 530 | assertThrows { 531 | Label().address 532 | } 533 | } 534 | 535 | @Test 536 | fun testTwiceAssigned() { 537 | assertThrows { 538 | val label = Label() 539 | label.address = 0 540 | label.address = 0 541 | } 542 | } 543 | } 544 | 545 | class RInstructionTest { 546 | @Test 547 | fun testOpcode() { 548 | (0 until 0x3F).forEach { 549 | assertEquals(RInstruction(it, zero, zero, zero).assemble() ushr 26 and 0b111111, it) 550 | } 551 | } 552 | 553 | @Test 554 | fun testIllegalOpcode() { 555 | assertThrows { RInstruction(0x3F + 1, zero, zero, zero).assemble() } 556 | } 557 | 558 | @Test 559 | fun testShift() { 560 | (0 until 0x1F).forEach { 561 | assertEquals(it, RInstruction(0, zero, zero, zero, it).assemble() ushr 6 and 0b11111) 562 | } 563 | } 564 | 565 | @Test 566 | fun testIllegalShift() { 567 | assertThrows { RInstruction(0, zero, zero, zero, 0x1F + 1).assemble() } 568 | } 569 | 570 | @Test 571 | fun testFunct() { 572 | (0 until 0x3F).forEach { 573 | assertEquals(it, RInstruction(0, zero, zero, zero, 0, it).assemble() and 0b111111) 574 | } 575 | } 576 | 577 | @Test 578 | fun testIllegalFunct() { 579 | assertThrows { RInstruction(0, zero, zero, zero, 0, 0x3F + 1).assemble() } 580 | } 581 | 582 | @Test 583 | fun testRsReg() { 584 | Reg.values().forEach { 585 | assertEquals(it.id, RInstruction(0, it, zero, zero).assemble() ushr 21 and 0b11111) 586 | } 587 | } 588 | 589 | @Test 590 | fun testRtReg() { 591 | Reg.values().forEach { 592 | assertEquals(it.id, RInstruction(0, zero, it, zero).assemble() ushr 16 and 0b11111) 593 | } 594 | } 595 | 596 | @Test 597 | fun testRdReg() { 598 | Reg.values().forEach { 599 | assertEquals(it.id, RInstruction(0, zero, zero, it).assemble() ushr 11 and 0b11111) 600 | } 601 | } 602 | } 603 | 604 | class IInstructionTest { 605 | @Test 606 | fun testOpcode() { 607 | (0 until 0x3F).forEach { 608 | assertEquals(it, IInstruction(it, zero, zero, 0).assemble() ushr 26 and 0b111111) 609 | } 610 | } 611 | 612 | @Test 613 | fun testIllegalOpcode() { 614 | assertThrows { IInstruction(0x3F + 1, zero, zero, 0).assemble() } 615 | } 616 | 617 | @Test 618 | fun testImm() { 619 | (0 until 0xFFFF).forEach { 620 | assertEquals(it, IInstruction(0, zero, zero, it).assemble() and 0xFFFF) 621 | } 622 | } 623 | 624 | @Test 625 | fun testRsReg() { 626 | Reg.values().forEach { 627 | assertEquals(it.id, IInstruction(0, it, zero, 0).assemble() ushr 21 and 0b11111) 628 | } 629 | } 630 | 631 | @Test 632 | fun testRtReg() { 633 | Reg.values().forEach { 634 | assertEquals(it.id, IInstruction(0, zero, it, 0).assemble() ushr 16 and 0b11111) 635 | } 636 | } 637 | } 638 | 639 | class JInstructionTest { 640 | @Test 641 | fun testOpcode() { 642 | (0 until 0x3F).forEach { 643 | assertEquals(it, JInstruction(it, 0).assemble() ushr 26 and 0b111111) 644 | } 645 | } 646 | 647 | @Test 648 | fun testIllegalOpcode() { 649 | assertThrows { JInstruction(0x3F + 1, 0).assemble() } 650 | } 651 | 652 | @Test 653 | fun testAddress() { 654 | (0 until 0x10000).forEach { 655 | assertEquals(it, JInstruction(0, it).assemble() and 0x3FFFFFF) 656 | } 657 | } 658 | 659 | @Test 660 | fun testIllegalAddress() { 661 | assertThrows { JInstruction(0, 0x3FFFFFF + 1).assemble() } 662 | } 663 | } 664 | 665 | class NopInstructionTest { 666 | @Test 667 | fun testNop() { 668 | assertEquals(0, NopInstruction().assemble()) 669 | } 670 | } 671 | 672 | class TestIntHelpers { 673 | @Test 674 | fun testToHex() { 675 | assertEquals("00000000", 0.toHex()) 676 | assertEquals("CDCDCDCD", 0xCDCDCDCD.toInt().toHex()) 677 | assertEquals("FFFFFFFF", 0xFFFFFFFF.toInt().toHex()) 678 | } 679 | 680 | @Test 681 | fun testToLittleEndian() { 682 | assertEquals("44332211", 0x11223344.toLittleEndian().toHex()) 683 | } 684 | } 685 | --------------------------------------------------------------------------------