342 lines
9.1 KiB
Plaintext
Executable File
342 lines
9.1 KiB
Plaintext
Executable File
class Main {
|
|
|
|
static int division_tmp;
|
|
static Array mem;
|
|
static Array st;
|
|
static int f;
|
|
static boolean color;
|
|
|
|
/** Initializes the library. */
|
|
function void init_test() {
|
|
var int a;
|
|
let st = Array.new(16);
|
|
let st[0] = 1;
|
|
while (a < 15){
|
|
let a = a + 1;
|
|
let st[a] = st[a - 1] + st[a - 1];
|
|
}
|
|
let color = 1;
|
|
return;
|
|
}
|
|
|
|
|
|
function void main(){
|
|
do Main.init_test();
|
|
do Main.test_drawPixel(256,128);
|
|
do Main.test_drawPixel(255,128);
|
|
do Main.test_drawPixel(257,128);
|
|
do Main.test_changeColor(0);
|
|
do Main.test_drawPixel(256,128);
|
|
return;
|
|
}
|
|
function void test_changeColor(boolean x){
|
|
let color = x;
|
|
return;
|
|
}
|
|
function void test_clearScreen(){
|
|
var int a;
|
|
let a = 16384;
|
|
while (a < 24575){
|
|
let mem[a] = 0;
|
|
let a = a + 1;
|
|
}
|
|
return;
|
|
}
|
|
function void test_drawRectangle(int x1, int y1, int x2, int y2){
|
|
var int a, tmp;
|
|
if (x1 > x2){
|
|
let tmp = x1;
|
|
let x1 = x2;
|
|
let x2 = tmp;
|
|
}
|
|
if (y1 > y2){
|
|
let tmp = y1;
|
|
let y1 = y2;
|
|
let y2 = tmp;
|
|
}
|
|
if((x1 < 0) | (x2 > 511) | (y1 < 0) | (y2 > 255)){
|
|
do Sys.error(8);
|
|
}
|
|
while(a < y2){
|
|
do Screen.drawLine(x1, y1 + a, x2, y1 + a);
|
|
let a = a + 1;
|
|
}
|
|
return;
|
|
}
|
|
function void test_drawCicle(int x, int y, int r){
|
|
var int dy, dx, tmp;
|
|
if(r > 181){
|
|
do Sys.error(7);
|
|
}
|
|
let dy = - r;
|
|
while (dy < r){
|
|
let tmp = Math.sqrt((r * r) - (dy * dy));
|
|
do Screen.drawLine(x - tmp, y + dy, x + tmp, y + dy);
|
|
let dy = dy + 1;
|
|
}
|
|
return;
|
|
}
|
|
function void test_drawLine_optimize(int x1, int y1, int x2, int y2){
|
|
var int diff, dx, dy, a, b, tmp;
|
|
if (x1 > x2){
|
|
let tmp = x1;
|
|
let x1 = x2;
|
|
let x2 = tmp;
|
|
}
|
|
if (y1 > y2){
|
|
let tmp = y1;
|
|
let y1 = y2;
|
|
let y2 = tmp;
|
|
}
|
|
if((x1 < 0) | (x2 > 511) | (y1 < 0) | (y2 > 255)){
|
|
do Sys.error(8);
|
|
}
|
|
let dx = x2 - x1;
|
|
let dy = y2 - y1;
|
|
if (dx = 0){
|
|
while(~(b > dy)){
|
|
do Screen.drawPixel(x1, y1 + b);
|
|
let b = b + 1;
|
|
}
|
|
return;
|
|
}
|
|
if (dy = 0){
|
|
while(~(a > dx)){
|
|
do Screen.drawPixel(x1 + a, y1);
|
|
let a = a + 1;
|
|
}
|
|
return;
|
|
}
|
|
while ((~(a > dx)) & (~(b > dy))){
|
|
do Screen.drawPixel(x1 + a, y1 + b);
|
|
if (diff < 0){
|
|
let a = a + 1;
|
|
let diff = diff + dy;
|
|
}
|
|
else{
|
|
let b = b + 1;
|
|
let diff = diff - dx;
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
function void test_drawPixel(int x, int y) {
|
|
var int value, address, bit, mem_block;
|
|
if ((x > 511) | (x < 0) | (y > 255) | (y < 0)){
|
|
do Sys.error(7);
|
|
}
|
|
let mem_block = x / 16;
|
|
let address = (32 * y) + mem_block + 16384;
|
|
let value = mem[address];
|
|
let bit = x - (mem_block * 16);
|
|
if (color = 1){
|
|
let mem[address] = st[bit] | value;
|
|
}
|
|
else{
|
|
let mem[address] = (~st[bit])& value;
|
|
}
|
|
return;
|
|
}
|
|
|
|
|
|
function int test_multiply(int x, int y){
|
|
var int sum, temp, y_comp, i, neg;
|
|
let neg = ((x < 0) & (y > 0)) | ((x > 0) & (y < 0));
|
|
let x = Math.abs(x);
|
|
let y = Math.abs(y);
|
|
if (x < y){
|
|
let temp = x;
|
|
let x = y;
|
|
let y = temp;
|
|
}
|
|
if (y = 1){
|
|
if(neg){
|
|
return -x;
|
|
}
|
|
return x;
|
|
}
|
|
while((y_comp - 1) < (y - 1)){
|
|
if ((st[i] & y) > 0) {
|
|
let sum = sum + x;
|
|
let y_comp = y_comp + st[i];
|
|
}
|
|
let x = x + x;
|
|
let i = i + 1;
|
|
}
|
|
if(neg){
|
|
let sum = -sum;
|
|
}
|
|
return sum;
|
|
}
|
|
function int test_max(int x, int y){
|
|
if (x > y){
|
|
return x;
|
|
}
|
|
return y;
|
|
}
|
|
|
|
function int test_min(int x, int y){
|
|
if (x < y){
|
|
return x;
|
|
}
|
|
return y;
|
|
}
|
|
function int test_sqrt(int x) {
|
|
var int j, b, d, y;
|
|
if (x < 0){
|
|
do Sys.error(4);
|
|
}
|
|
let j = 7;
|
|
while (j > -1){
|
|
let d = y + st[j];
|
|
let b = Main.test_multiply(d,d);
|
|
if ((~(b > x)) & (b > 0)){
|
|
let y = d;
|
|
}
|
|
let j = j - 1;
|
|
}
|
|
return y;
|
|
}
|
|
function int test_divide(int x, int y) {
|
|
var int q, neg;
|
|
if (f = 0){
|
|
let division_tmp = 0;
|
|
let neg = ((x < 0) & (y > 0)) | ((x > 0) & (y < 0));
|
|
let x = Math.abs(x);
|
|
let y = Math.abs(y);
|
|
if (y = 0){
|
|
do Sys.error(3);
|
|
}
|
|
let f = 1;
|
|
}
|
|
if ((y > x) | (y < 0)){
|
|
return 0;
|
|
}
|
|
let q = test_divide(x, y + y);
|
|
let f = 0;
|
|
let q = Math.abs(q);
|
|
if ((q & 1) = 1){
|
|
let division_tmp = division_tmp + y + y;
|
|
}
|
|
if (x - division_tmp < y){
|
|
if (neg){
|
|
return -(q + q);
|
|
}
|
|
return q + q;
|
|
}
|
|
else{
|
|
if (neg){
|
|
return -(q + q + 1);
|
|
}
|
|
return q + q + 1;
|
|
}
|
|
}
|
|
|
|
|
|
function int test_abs(int x) {
|
|
if (x > 0){
|
|
return x;
|
|
}
|
|
return -x;
|
|
}
|
|
|
|
function int test_peek(int x){
|
|
return mem[x];
|
|
}
|
|
|
|
function void test_poke(int x, int y){
|
|
let mem[x] = y;
|
|
return;
|
|
}
|
|
|
|
function int test_dealloc_stolen(Array x){
|
|
var int obj_add,b;
|
|
let obj_add = x - 2;
|
|
let b = obj_add[1];
|
|
if (b[0] = 0){
|
|
let obj_add[0] = obj_add[1] - obj_add -2;
|
|
}
|
|
else{
|
|
let obj_add[0] = obj_add[1] - obj_add + b[0];
|
|
if (b[1] = b +2){
|
|
let obj_add[1] = obj_add + 2;
|
|
}
|
|
else{
|
|
let obj_add[1] = b[1];
|
|
}
|
|
}
|
|
}
|
|
|
|
function void test_dealloc(Array o){
|
|
var Array current_block;
|
|
let current_block = 2048;
|
|
let o[1]= current_block[1];
|
|
let current_block[1] = o;
|
|
}
|
|
|
|
function int test_alloc(int size){
|
|
var Array current_block;
|
|
var int tmp;
|
|
let current_block = 2048;
|
|
if (size < 0){
|
|
do Sys.error(5);
|
|
}
|
|
if (size = 0){
|
|
let size = 1;
|
|
}
|
|
while (current_block[0] < (size + 2)) {
|
|
let current_block = current_block[1];
|
|
}
|
|
let current_block[0] = current_block[0] - size - 2;
|
|
let current_block = current_block + current_block[0] + 2;
|
|
let current_block[0] = size;
|
|
return current_block + 2;
|
|
}
|
|
|
|
function int test_alloc_stolen(int x){
|
|
var Array curret_block, next_block;
|
|
if (x < 0){
|
|
do Sys.error(5);
|
|
}
|
|
if (x = 0){
|
|
let x = 1;
|
|
}
|
|
let curret_block = 2048;
|
|
let curret_block[0] = 14334;
|
|
let curret_block[1] = 2050;
|
|
while ((curret_block < 16383) & (curret_block[0] < x)){
|
|
let next_block = curret_block[1];
|
|
if ((curret_block[0] = 0) | (next_block > 16382) | (next_block[0] = 0)){ // this sends directly out of while
|
|
let curret_block = next_block;
|
|
}
|
|
else {
|
|
let curret_block[0] = curret_block[1] - curret_block + next_block[0]; // cb size =
|
|
if (next_block[1] = next_block + 2){
|
|
let curret_block[1] = curret_block + 2;
|
|
}
|
|
else{
|
|
let curret_block[1] = next_block[1];
|
|
}
|
|
}
|
|
}
|
|
if ((curret_block + x) > 16379){
|
|
do Sys.error(6);
|
|
}
|
|
if (curret_block[0] > (x + 2)){ // if current block fills the desired size perfectly
|
|
let curret_block[x + 2] = curret_block[0] - x -2;
|
|
if (curret_block[1] = curret_block + 2){
|
|
let curret_block[x + 3] = curret_block + x + 4;
|
|
}
|
|
else{
|
|
let curret_block[x + 3] = curret_block[1];
|
|
}
|
|
}
|
|
let curret_block[0] = 0;
|
|
return (curret_block + 2);
|
|
}
|
|
|
|
|
|
}
|
|
|