 
   // Iterate through the entire tile-line
   let tile = int16(*(vram + mapo + lineo));
-  // printf("gpu_td_select: %d\n", gpu_td_select);
-  if gpu_td_select and tile < 128 { tile += 256; }
+  if not gpu_td_select and tile < 128 { tile += 256; }
 
   let i = 0;
   while i < 160 {
+    // Tile Color Index
+    let color = *(*(*(gpu_tiles + tile) + y) + x);
+
+    // Palette
+    color = (gpu_palette >> (color * 2)) & 0x3;
+
     // Push pixel to framebuffer
-    *(gpu_framebuffer + fbo + i) = *(*(*(gpu_tiles + tile) + y) + x);
-    // *(gpu_framebuffer + fbo + i) = 1;
+    *(gpu_framebuffer + fbo + i) = color;
 
     x += 1;
     if (x == 8) {
       // Tile ends; pick a new one
       x = 0;
-      // mapo += 1;
-      lineo += 1;
-      lineo &= 31;
+      lineo = (lineo + 1) & 31;
       tile = int16(*(vram + mapo + lineo));
-      // lineo = (lineo + 1) & 31;
-      if gpu_td_select and tile < 128 { tile += 256; }
+      if not gpu_td_select and tile < 128 { tile += 256; }
     }
 
     i += 1;

    while x < 160 {
      let pixel = *(gpu_framebuffer + (y * 160 + x));

      if pixel == 0 {
        SDL_SetRenderDrawColor(_renderer, 255, 255, 255, 255);
      } else if pixel == 1 {
        SDL_SetRenderDrawColor(_renderer, 192, 192, 192, 255);
      } else if pixel == 2 {
        SDL_SetRenderDrawColor(_renderer, 96, 96, 96, 255);
      } else if pixel == 3 {
        SDL_SetRenderDrawColor(_renderer, 0, 0, 0, 255);
      }

      SDL_RenderDrawPoint(_renderer, c_int(x), c_int(y));

// Scrolling
let gpu_scy = 0;
let gpu_scx = 0;

// Palette
let gpu_palette: uint8 = 0;

def gpu_init() {
  gpu_framebuffer = malloc(160 * 144);
