2016年2月26日金曜日

開発環境

  • OS X El Capitan - Apple (OS)
  • Emacs(Text Editor)
  • Java (実行環境)

コンピュータシステムの理論と実装 (Noam Nisan (著)、Shimon Schocken (著)、斎藤 康毅(翻訳)、オライリージャパン)の12章(オペレーティングシステム)、12.5(プロジェクト)を取り組んでみる。

12.5(プロジェクト)

コード(Emacs)

Screen.jack

class Screen {
  static boolean color;

  function void init() {
    let color = true;
    return;
  }
  function void clearScreen() {
    return;
  }
  function void setColor(boolean b) {    
    let color = b;
    return;
  }
  function void drawPixel(int x, int y) {    
    var int address;
    var int word;
    var int c;
    var int m;

    let address = 16384 + Math.multiply(y, 32) + Math.divide(x, 16) ;
    let word = Memory.peek(address);
    let c = 1;
    let m = x - Math.multiply(16, Math.divide(x, 16));

    while (m > 0) {
      let c = Math.multiply(c, 2);
      let m = m - 1;
    }
    if (color) {
      do Memory.poke(address, word | c);
    } else {
      do Memory.poke(address, word & ~c);
    }
    return;
  }
  function void drawLine(int x1, int y1, int x2, int y2) {
    var int a, b, dx, dy, ady_bdx;
    
    let a = 0;
    let b = 0;
    let dx = x2 - x1;
    let dy = y2 - y1;
    let ady_bdx = 0;

    if (dx = 0) {
      if (dy > 0) {
        while (~(y1 = y2)) {
          do Screen.drawPixel(x1, y1);
          let y1 = y1 + 1;
        }
        return;
      }
      if (dy < 0) {
        do Screen.drawLine(x1, y2, x2, y1);
        return;
      }
      return;
    }
    if (((dx = 0) | (dx > 0)) & ((dy = 0) | (dy > 0))) {
      while (((a = dx)|(a < dx)) & ((b = dy)|(b < dy))) {      
        do Screen.drawPixel(x1 + a, y1 + b);
        if ((ady_bdx < 0) | (ady_bdx = 0)) {
          let a = a + 1;
          let ady_bdx = ady_bdx + dy;
        } else {        
          let b = b + 1;
          let ady_bdx = ady_bdx - dx;
        }
      }
      return;
    }
    if (((dx = 0) | (dx > 0)) & ((dy = 0) | (dy < 0))) {
      while (((a = dx)|(a < dx)) & ((b = dy)|(b > dy))) {        
        do Screen.drawPixel(x1 + a, y1 + b);
        if ((ady_bdx < 0) | (ady_bdx = 0)) {
          let a = a + 1;
          let ady_bdx = ady_bdx - dy;
        } else {          
          let b = b - 1;
          let ady_bdx = ady_bdx - dx;
        }
      }
      return;
    }    
    do Screen.drawLine(x2, y2, x1, y1);
    return;
  }
  function void drawRectangle(int x1, int y1, int x2, int y2) {
    while (y1 < y2) {
      do Screen.drawLine(x1, y1, x2, y1);
      let y1 = y1 + 1;
    }
    return;
  }
  function void drawCircle(int cx, int cy, int r) {
    var int dy;
    
    let dy = -r;

    while ((dy < r) | (dy = r)) {
      do Screen.drawLine(cx - Math.sqrt((r * r) - (dy * dy)), cy + dy,
                         cx + Math.sqrt((r * r) - (dy * dy)), cy + dy);
      let dy = dy + 1;
    }
    return;
  }
}

0 コメント:

コメントを投稿

Comments on Google+: