{"id":31680982,"url":"https://github.com/gg-blake/mariokart","last_synced_at":"2025-10-08T07:22:53.806Z","repository":{"id":317781956,"uuid":"1051350949","full_name":"gg-blake/marIOkart","owner":"gg-blake","description":"Training an ML model to play Mariokart DS","archived":false,"fork":false,"pushed_at":"2025-10-06T15:53:05.000Z","size":25651,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-10-06T17:43:53.277Z","etag":null,"topics":["desmume","gtk","nitrosdk","pytorch","reverse-engineering"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gg-blake.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-09-05T20:51:32.000Z","updated_at":"2025-10-06T15:53:08.000Z","dependencies_parsed_at":"2025-10-03T02:51:18.650Z","dependency_job_id":"a164f18a-0dba-483a-8902-0d63696ad60c","html_url":"https://github.com/gg-blake/marIOkart","commit_stats":null,"previous_names":["gg-blake/mariokart-ml","gg-blake/mariokart"],"tags_count":null,"template":false,"template_full_name":null,"purl":"pkg:github/gg-blake/marIOkart","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gg-blake%2FmarIOkart","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gg-blake%2FmarIOkart/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gg-blake%2FmarIOkart/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gg-blake%2FmarIOkart/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gg-blake","download_url":"https://codeload.github.com/gg-blake/marIOkart/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gg-blake%2FmarIOkart/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278906042,"owners_count":26066425,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-08T02:00:06.501Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["desmume","gtk","nitrosdk","pytorch","reverse-engineering"],"created_at":"2025-10-08T07:22:48.641Z","updated_at":"2025-10-08T07:22:53.800Z","avatar_url":"https://github.com/gg-blake.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Training a model to play Mariokart DS\n\n```\npip install -r requirements.txt\n```\n\n# Overview\nThe project falls down into three main parts\n1) Reverse engineering MKDS Rom for car data (Done)\n2) Making a visualization for debugging and benchmarking (Done)\n3) Building and training the model (In Progess)\n\n# Reverse Engineering Mariokart DS\nThe reverse engineering process consisted mainly of using [dynamic code analysis](https://en.wikipedia.org/wiki/Dynamic_program_analysis) to extract import values from kart's game state. Values consisted of but were not limited to:\n- Car's position\n- Car's orientation\n- Car's acceleration/Speed\n- Car's collected ability\n- Enemy positions\n\n## Interpreting WRAM\nI used an emulation tool called [Desmume](https://desmume.org/) to run the MKDS rom on my PC. Desmume comes with built in features for watching the memory which I utilized heavily. Additionally, my ML interface utilized [py-desmume](https://py-desmume.readthedocs.io/en/latest/), a python interface for using desmume's frontend. For US versions of MKDS, the pointer to the kart's race data is located at `0x0217ACF8` in\n\nThe DS uses a dual CPU system with ARM9 and ARM7 CPUs. Although memory is divided between the two (i.e. they share the same address space), the data that I care about is stored exclusively on the ARM9 CPU's memory. In Mariokart DS, the game state is stored on the Main RAM of the ARM9 CPU from `0x02000000` to `0x023FFFFF`, in [little endian](https://en.wikipedia.org/wiki/Endianness) format. \n\n### Kart Data\nThe pointer to the kart's game data (position, orientation, powerup, etc.) is located at `0x0217ACF8`, stored as an unsigned 32-bit integer. Starting at the kart's game data in memory, kart values of interest live at the following offsets:\n- position vector (`0x80`) (3x `fx32`)\n- movement direction vector (`0x68`) (3x `fx32`)\nMany game values including vectors are stored as fixed point data types according to the [NitroSDK/NitroMath spec](https://twlsdk.randommeaninglesscharacters.com/docs/nitro/NitroSDK/fx/list_fx.html). Unlike floating point values that have an exponent component to the bit sequence, Mario Kart DS's fixed point datatypes, specifically, `fx32` have a dedicated 1-bit sign, 19-bit integer, and 12-bit fraction.\n\n### Camera Data\nThe pointer to the car camera's data is located at `0x0217AA4C`, stored as an unsigned 32-bit integer. Starting at the car camera's data in memory, camera values of interest live at the following offsets:\n- position vector (`0x24`) (3x `fx32`)\n- elevation (`0x178`) (1x `fx32`)\n- target position vector (`0x18`) (3x `fx32`)\n- field of view (`0x60`) (1x `u16`)\n- aspect ratio (`0x6C`) (1x `fx32`)\nThis is all the camera data that we need to reconstruct the camera's [perspective projection and model view matrices](https://www.opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices/#the-model-view-and-projection-matrices). This will come in handy when I discuss the visualization overlay later on.\n\n## Interpreting Course Data\nCourse data is located as an `.nkm` file within the MKDS ROM. In order the retreive this file, we need to unpack the ROM file. I used [kiwi.ds](https://projectpokemon.org/home/files/file/2073-nds-editor-kiwids/) to retreive the course files. The course files are compressed as `.carc`. To uncompress it, I used [NArchive](https://github.com/nickworonekin/narchive/tree/master/src/Narchive) to extract the course files. I'm mainly focused on reading the checkpoint data for a course, so I ignore the rest of the files except the `.nkm` files.\n\nNKM files are essentially specialized bin files. The spec for this file can be found [here](https://wiki.tockdom.com/wiki/NKM_(File_Format)#cite_note-MoreCPOIInfo-4). The NKM file has a header that specifies the byte offset of each data section in the file. The `CPOI` section contains all the entries for checkpoints on a map. It's section offset is found at `0x2C`. Each data section specifies it name and the number of entries. `CPOI` entries are 36 bytes in size and contain:\n1) left position vector (`0x00`) (2x `fx32`)\n2) right position vector (`0x08`) (2x `fx32`)\n...\n5) distance (`0x18`) (1x `fx32`)\n...\n8) key id (`0x20`) (1x `u16`)\n9) respawn id (`0x22`) (1x `u8`)\n...\nHaving this data is useful since I can use this to calculate the player's forward facing distance to wall, assuming the checkpoints are positioned within the bounds of the map.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgg-blake%2Fmariokart","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgg-blake%2Fmariokart","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgg-blake%2Fmariokart/lists"}