Git Product home page Git Product logo

agnes's Introduction

AgNES: A C-- NES compiler written from scratch

Created by Niko Savas and Vicky Bilbily as a project for McMaster's 4TB3 Compilers class

How to use

First you'll need a couple tools:

  1. An NES emulator. For testing, we used FCEUX.
  2. A 6502 assembler. We recommend WLA DX. Make sure that wla-6502 and wlalink are on your path.

The simplest way to start using AgNES is to check out the /example/ folder. NES files require custom headers and footers, which are provided as NES_HEADER.s and NES_FOOTER.s. By running build.sh, you will build a file named game.nes. This can be run with an emulator - the example game is a sprite moving across the screen. By changing example/input.c you can have the sprite move in different directions.

Compilation steps

Scanner

Input: C-- file as a string
Output: List of tokens (token.h)

Parser

Input: List of tokens
Output: Abstract syntax tree (astNode.h)

Semantic Analysis

Input: Abstract syntax tree
Output: Symbol table (symbolTable.h)

Intermediate Code (IR) generation

Input: Symbol table
Output: List of three address code (TAC) instructions (irGenerator.h)

Code Generation

Input: TAC list and symbol table
Output: 6502 Assembly file

Design challenges

6502 architecture only has 3 registers

This causes generated 6502 code to be much more verbose without optimizations. Not only are there only 3 registers, but many instructions only work with specific registers. For example, addition can only be performed against the Accumulator register. This is different from other instruction sets which have more freedom about where values can be loaded from and modified. While the scope of this project did not include optimizations, they could be added (in between the IR generation and code generation step) to significantly reduce the number of instructions generated by AgNES.

NES stack is tiny

The NES stack is a 256-byte array. This restriction can be somewhat limiting to recursion depth, as traditionally the stack would contain return addresses, register values, as well as local variables. To avoid this, we only use the native stack for JSR (Jump to Subroutine) and RTS (Return from subroutine). The rest of the information traditionally stored on the stack instead uses a custom stack implementation that uses the front of the NES main memory. This allows for much more recursion depth in the C-- applications.

The NES has special memory locations and functionality

The way that the NES handles sprites and refreshing the screen is not intuitive when the 6502 assembly code is abstracted away behind C--. To combat this, we included a number of custom tokens (macros) that can be found in token.h. While writing the C-- code, these tokens can be used and will be translated into the necessary 6502 assembly code for the functionality desired.

Future Development

Due to time constraints on this project, some features were left out. These include:

  • Boolean expressions
  • Function return values
  • Arrays

agnes's People

Contributors

taurheim avatar bickybee avatar

Stargazers

Paco Juan Quirós avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.