User Tools

Site Tools


opus:spring2013:jpettie:start

JACOB PETTIE's Spring 2013 Opus

THE FAKE OCEAN

Introduction

I am a work in progress, a man with many procrastination issues to iron out, and someone worth while in the end-game if only with some guidance of self and other.

Computer Organization Topics

Topic: AND, OR, NOT (CPU Simulator)

I will be using Topics instead of Journals for this class, starting with the essential AND, OR, NOT gates.

I realized that OR could be written with NAND gates, so my initial start to the CPU Simulator is the NOT, and AND Gates.

AND Gate:

#ifndef AND_H
#define AND_H
 
//LOGICAL AND FUNCTION
char AND(char symVar1, char symVar2){
        if (symVar1 == sym1){
                if (symVar2 == sym1){
                        return(sym1);
                }else{
                        return(sym1);
                }
        }else if (symVar1 == sym2){
                if (symVar2 == sym1){
                        return(sym1);
                }else{
                        return(sym2);
                }
        }
}
 
#endif

NOT Gate:

#ifndef NOT_H
#define NOT_H
 
//LOGICAL NOT FUNCTION
char NOT(char symVar){
        if (symVar == sym1){
                return(sym2);
        }else{
                return(sym1);
        }
}
 
#endif

Topic: NAND and OR Gate Using NAND Gates (CPU Simulator)

By making use of other library files of the CPU Simulator as a whole, I am able to reduce code for newer functions to even one line, making the entire program dynamic.

NAND Gate:

#ifndef NAND_H
#define NAND_H
 
char NAND(char symVar1, char symVar2){
        NOT(AND(symVar1, symVar2));
}
 
#endif

OR Gate created using NAND Gates:

#ifndef OR_H
#define OR_H
 
//LOGICAL OR FUNCTION
char OR(char symVar1, char symVar2){
        NAND(NAND(symVar1, symVar1), NAND(symVar2, symVar2));
}
 
#endif

Topic: XOR (A vital step to SUM/CARRY) and Half/Full SUM/CARRY (CPU Simulator)

Manipulating other library files I am able to great a HALF and FULL ADDER library file for the CPU Simulator.

XOR Gate:

ifndef XOR_H
#define XOR_H
 
//LOGICAL XOR FUNCTION
char XOR(char symVar1, char symVar2){
        NAND(NAND(symVar1, NAND(symVar1, symVar2)), NAND(symVar2, NAND(symVar1, symVar2)));
}
 
#endif

HALF SUM Gate:

#ifndef HSUM_H
#define HSUM_H
 
//LOGICAL HALF SUM FUNCTION
char HSUM(char symVar1, char symVar2){
        XOR(symVar1, symVar2);
}
 
#endif

HALF CARRY Gate:

#ifndef HCARRY_H
#define HCARRY_H
 
//LOGICAL HALF CARRY FUNCTION
char HCARRY(char symVar1, char symVar2){
        AND(symVar1, symVar2);
}
 
#endif

FULL SUM Gate:

#ifndef FSUM_H
#define FSUM_H
 
//LOGICAL FULL SUM FUNCTION
char FSUM(char symVar1, char symVar2, char symCarry){
        XOR(XOR(symVar1, symVar2), symCarry);
}
 
#endif

FULL CARRY Gate:

#ifndef FCARRY_H
#define FCARRY_H
 
//LOGICAL FULL CARRY FUNCTION
char FCARRY(char symVar1, char symVar2, char symCarry){
        OR(AND(XOR(symVar1, symVar2), symCarry), AND(symVar1, symVar2));
}
 
#endif

Topic: First Attempts at Latches (CPU Simulator)

My initial thought for Latches was to create a dynamic function that would keep track and set its initial states to create a working latch, but by using globals and maybe if statements, I found it was over working the project and there should be an easier way to finish what is needed.

NAND Latch Attempt:

#ifndef NANDL_H
#define NANDL_H
 
//Logical RS NAND Latch Function
char *NANDL(char in, char which){
        char *result;
        result = (char *)malloc(sizeof(char));
        if (P == NULL && Q == NULL && R == NULL && S == NULL){
                P = random(2);
                if (which == 0){
                        R = random(2);
                }else if (which == 1){
                        S = random(2);
                }
        }
        if (which == 0){
                *(result + 0) = NAND(in, P);
                if (Q != *(result + 0))
                        Q = *(result + 0);
                *(result + 1) = NAND(R, Q);
        }else if (which == 1){
                *(result + 1) = NAND(in, Q);
                if (P != *(result + 1))
                        P = *(result + 1);
                *(result + 0) = NAND(S, P);
        }
        return result;
}
 
#endif

NOR Latch Attempt:

#ifndef NORL_H
#define NORL_H
 
//Logical RS NOR Latch Function
char *NORL(char in, char which){
        char *result;
        result = (char *)malloc(sizeof(char));
        if (P == NULL && Q == NULL && R == NULL && S == NULL){
                P = random(2);
                if (which == 0){
                        R = random(2);
                }else if (which == 1){
                        S = random(2);
                }
        }
        if (which == 0){
                *(result + 0) = NOR(in, P);
                if (Q != *(result + 0))
                        Q = *(result + 0);
                *(result + 1) = NOR(R, Q);
        }else if (which == 1){
                *(result + 1) = NOR(in, Q);
                if (P != *(result + 1))
                        P = *(result + 1);
                *(result + 0) = NOR(S, P);
        }
        return result;
}
 
#endif

Topic: RS NAND Latch and Clocked RS NAND Latch (CPU Simulator)

The solution to my issues with NAND Latch and NOR Latches. I found it much easier than I originally thought, and came to these solutions.

RS NAND Latch:

#ifndef RSNANDL_H
#define RSNANDL_H
 
//Logical RS NAND Latch Function
char *RSNANDL(char s, char r){
        Q = NAND(s, P);
        P = NAND(Q, r);
 
        char *result;
        result = (char *)malloc(sizeof(char));
        *(result + 0) = Q;
        *(result + 1) = P;
        return result;
}
 
#endif

Clocked RS NAND Latch:

#ifndef CRSNANDL_H
#define CRSNANDL_H
 
//Logical Clocked RS NAND Latch Function
char *CRSNANDL(char s, char clock, char r){
        P = (NAND(Q, NAND(clock, r)));
        Q = (NAND(NAND(s, clock), P));
 
        char *result;
        result = (char *)malloc(sizeof(char));
        *(result + 0) = Q;
        *(result + 1) = P;
        return result;
}
 
#endif

Topic: TWO INPUT MULTIPLEX/DEPLEXER (CPU Simulator)

Taking a break from Latches, writing these library files were a nice subdivision and cute idea of learning from Joe, these would be interesting to implement and I am still not really sure what we would be using them for in the long run, haha.

On a side note, the demultiplexer gave me a good learning experience in terms of returning two different results from one function, using a malloc'd char array as a result-set and dereferencing it in main as a way to display it through printf(s).

Like so: *(functionName(param1, param2) + 0), *(functionName(param1, param2) + 1)

Two Input Multiplexer:

#ifndef TWOINMULTIPLEX_H
#define TWOINMULTIPLEX_H
 
//TWO INPUT MULTIPLEXER FUNCTION
char TWOINMULTIPLEX(char symVar1){
        OR(AND(NOT(NOT(symVar1)), sym1), AND(NOT(symVar1), sym2));
}
 
#endif

Two Input Demultiplexer:

#ifndef TWOINDEMULTIPLEX_H
#define TWOINDEMULTIPLEX_H
 
//TWO INPUT MULTIPLEXER FUNCTION
char *TWOINDEMULTIPLEX(char symVar1, char symVar2){
        char *result;
        result = (char *)malloc(sizeof(char));
        *(result + 0) = AND(NOT(symVar1), symVar2);
        *(result + 1) = AND(NOT(NOT(symVar1)), symVar2);
        return result;
}
 
#endif

Topic: JK FLIPPY FLOPPY (CPU Simulator)

Oh, you joyous mess of a logic, so very tedious, unrelenting assault was needed to acquire a working version of such. Be hold, the most interesting gate I had to write in Computer Organization, not without much needed time to understand it backwards and many hours of not really getting it and fighting with others about it.

JK Flippy Floppy Gate:

#ifndef JKFLIPFLOP_H
#define JKFLIPFLOP_H
 
//Logical JK FLIP FLOP Function
char *JKFLIPFLOP(char j, char clock, char k){
        Q = *(CRSNANDL(*(RSNANDL(VNAND(j, P, clock), VNAND(clock, Q, k)) + 0), $
        P = *(CRSNANDL(*(RSNANDL(VNAND(j, P, clock), VNAND(clock, Q, k)) + 0), $
 
        char *result;
        result = (char *)malloc(sizeof(char));
        *(result + 0) = Q;
        *(result + 1) = P;
        return result;
}
 
#endif

Topic: Tying It All Together (CPU Simulator)

A joyous amount of Gates, and Logically operations, comes together in one, dynamic header file:

#ifndef LFUNC_H
#define LFUNC_H
 
//LOGICAL LIBRARY FILE
 
//INCLUDE FILES
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
 
/*GLOBAL SYMBOLS SYM2 HAS PRESIDENCE */
char sym1, sym2;
 
char Q, P;
 
#include "AND.h" //AND
#include "NOT.h" //NOT
#include "NAND.h" //NOT AND
#include "OR.h" //OR
#include "XOR.h" //EXCLUSIVE OR
#include "HSUM.h" //HALF SUM
#include "HCARRY.h" //HALF CARRY
#include "FSUM.h" //FULL SUM
#include "FCARRY.h" //FULL CARRY
#include "TWOINMULTIPLEX.h" //TWO INPUT MULTIPLEXER
#include "TWOINDEMULTIPLEX.h" //TWO INPUT DEMULTIPLEXER
#include "VAND.h" //VARIABLE INPUT AND
#include "VNAND.h" //VARIABLE INPUT NAND
#include "VOR.h" //VARIABLE INPUT OR
#include "CRSNANDL.h" //CLOCKED RS NAND LATCH
#include "RSNANDL.h" //RS NAND LATCH
#include "JKFLIPFLOP.h" //JK FLIP FLOP
 
#endif

Linking this to a main file with command line args will create a way to issue any symbol to my logically dynamic functions and be able to execute up to the output of a JK FLIPPY FLOPPY DISC.

:D!

hpc2 Journals

YADA HADA! TALK IS CHEAP!?

I believe my project pages for HPC 2 will do a lot of talking for me, in terms of the lack of substance in Journal entries. Oh, how I love writing in my Vampire Diary, show on CBS, shit is boss. Also, Teen Mom 2, pretty decent.

Overall, my starting and not finishing of many different projects has taught me that the mind wanders and that everything is but a chance to move on and learn the best way to survive. Random stuff, quotes to come, maybe…

Nope. Kthxbai.

sysprog Journals

Pong: The Rising, and Falling Act

I must give credit where credit is due, I got a shit-ton of help from Alex Hughes on this one, he is a boss, and I am lame. But I have been updating and changing some logic flaws. :)

MAIN.CPP

#include <SDL/SDL.h>
#include <iostream>
#include <SDL/SDL_ttf.h>
#include "ball.h"
#include "paddle.h"
 
SDL_Surface* load_image(const char* c,Uint32 colorkey=0)
{
        SDL_Surface* tmp=SDL_LoadBMP(c);
        if(colorkey!=0)
        {
 
                SDL_SetColorKey(tmp, SDL_SRCCOLORKEY,colorkey);
 
        }
        return tmp;
}
 
int main()
{
        SDL_Surface* screen;
        const int width=640;
        const int height=480;
        const int FPS=30;
 
        screen = SDL_SetVideoMode(width,height,32,SDL_SWSURFACE);
        TTF_Font* font;
        TTF_Init();
        font = TTF_OpenFont("lazy.ttf",20);
        SDL_Color color ={0,0,0};
        SDL_Event event;
        Uint32 start;
        bool running=true;
        bool arr[4] = {0,0,0,0};
        paddle player1(load_image("paddle.bmp"),0,225,10,50,4);
        paddle player2(load_image("paddle.bmp"),width-10,225,10,50,4);
        ball ball1(load_image("ball.bmp", SDL_MapRGB(screen->format,0x00,0xff,0xff)),320,240,20,20,3,5);
        SDL_Surface* text;
 
        while(running)
        {
                start=SDL_GetTicks();
                //handle events
 
                while(SDL_PollEvent(&event)){
 
                switch(event.type)
                {
                        case SDL_QUIT:
                           running=false;
                           break;
 
                           case SDL_KEYDOWN:
                           switch(event.key.keysym.sym)
                        {
                                case SDLK_UP:
                                  arr[0]=1;
                                  break;
                                case SDLK_DOWN:
                                  arr[1]=1;
                                  break;
                                case SDLK_w:
                                  arr[2]=1;
                                  break;
                                case SDLK_s:
                                  arr[3]=1;
                                  break;
                        }
                        break;
 
                        case SDL_KEYUP:
                           switch(event.key.keysym.sym)
                        {
                                case SDLK_UP:
                                  arr[0]=0;
                                  break;
                                case SDLK_DOWN:
                                  arr[1]=0;
                                  break;
                                case SDLK_w:
                                  arr[2]=0;
                                  break;
                                case SDLK_s:
                                  arr[3]=0;
                                  break;
                        }
                        break;
                        }
                }
                //logic
                if(arr[0])
                        player2.moveUp();
                else if(arr[1])
                        player2.moveDown();
                if (arr[2])
                        player1.moveUp();
                else if (arr[3])
                        player1.moveDown();
                ball1.move(player1.getRect(),player2.getRect());
 
                //scoring by out of screen
                switch(ball1.isOut()){
 
                        case 1:
                           player2.incpoint();
                           player1.setBack(0,255,10,50,3);
                           player2.setBack(width-10,225,10,50,3);
                           ball1.setBack(320,240,20,20,3,3);
                           break;
                        case 2:
                           player1.incpoint();
                           player1.setBack(0,255,10,50,3);
                           player2.setBack(width-10,225,10,50,3);
                           ball1.setBack(320,240,20,20,3,3);
                           break;
                }
 
                //render
                SDL_FillRect(screen,&screen->clip_rect,SDL_MapRGB(screen->format,0xff,0xff,0xff));
                player1.show();
                player2.show();
                ball1.show();
 
                //text box for score
                char c[5];
                SDL_Rect tmp = {10,0};
                sprintf(c,"%d", player1.getPoints());
                c[4]='\0';
                text = TTF_RenderText_Solid(font,c,color);
                SDL_BlitSurface(text,NULL, screen,&tmp);
                SDL_FreeSurface(text);
 
                tmp.x=width-40;
                sprintf(c,"%d", player2.getPoints());
                text = TTF_RenderText_Solid(font,c,color);
                SDL_BlitSurface(text,NULL,screen,&tmp);
                SDL_FreeSurface(text);
 
                SDL_Flip(screen);
                //regulate FPS
                if(1000/FPS>(SDL_GetTicks()-start))
                        SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
                }
 
                TTF_CloseFont(font);
 
                TTF_Quit();
                SDL_Quit();
        }

PADDLE.CPP

#include "paddle.h"
 
paddle::paddle(SDL_Surface* img, int x, int y, int w,int h, int yVel)
{
	box.x=x;
	box.y=y;
	box.w=w;
	box.h=h;
	image=img;
	yvel=yVel;
	point=0;
}
 
paddle::~paddle()
{
	SDL_FreeSurface(image);
}
 
void paddle::moveUp()
{
 
	 if(box.y>0)
	 box.y-=yvel;
 
}
 
void paddle::moveDown()
{
	if(box.y+box.h<SDL_GetVideoSurface()->clip_rect.h)
	  box.y+=yvel;
}
 
void paddle::show()
{
	SDL_BlitSurface(image,NULL, SDL_GetVideoSurface(), &box);
}
 
SDL_Rect* paddle::getRect()
{
	return &box;
}
 
void paddle::incpoint()
{
	point++;
}
 
void paddle::setBack(int x,int y,int w,int h,int yVel){
 
	box.x=x;
        box.y=y;
        box.w=w;
        box.h=h;
        yvel=yVel;
}
 
int paddle::getPoints()
{
	return point;
}

BALL.CPP

#include "ball.h"
 
ball::ball(SDL_Surface* img, int x, int y, int w, int h, int xVel, int yVel)
{
	image=img;
	box.x=x;
	box.y=y;
	box.w=w;
	box.h=h;
	xvel=xVel;
	yvel=yVel;
}
 
ball::~ball()
{
	SDL_FreeSurface(image);
}
 
void ball::show()
{
	SDL_BlitSurface(image,NULL,SDL_GetVideoSurface(),&box);
}
 
void ball::move(SDL_Rect* player1,SDL_Rect* player2)
{
	box.x+=xvel;
	box.y+=yvel;
	if(box.y<=0)
		yvel=-yvel;
	if(box.y+box.h>=SDL_GetVideoSurface()->clip_rect.h)
		yvel=-yvel;
 
	if(collision(&box,player1))
	{
 
		//top of paddle collision
		if(box.x+3<player1->x+player1->w)
		  yvel=-yvel;
		else
		  xvel=-xvel;
	}
 
	if(collision(&box,player2))
	{
 
		if(box.x+box.w-3>player2->w)
		  yvel=-yvel;
		else
		  xvel=-xvel;
 
	}
 
}
 
bool ball::collision(SDL_Rect* rec1,SDL_Rect* rec2)
{
	if(rec1->y >= rec2->y + rec2->h)
		return 0;
	if(rec1->x >= rec2->x + rec2->w)
		return 0;
	if(rec1->y + rec2->h <= rec2->y)
		return 0;
	if(rec1->x + rec1->w <= rec2->x)
		return 0;
	return 1;
}
 
int ball::isOut() 
{
	if(box.x<=0)
		return 1;
	if(box.x=SDL_GetVideoSurface()->clip_rect.w)
		return 2;
	return 0;
}
 
 
void ball::setBack(int x,int y, int w, int h, int xVel, int yVel) 
{
	box.x=x;
	box.y=y;
	box.w=w;
	box.h=h;
	xvel=xVel;
	yvel=yVel;
}

BALL.H

#include <SDL/SDL.h>
#ifndef BALL_H
#define BALL_H
 
	class ball{
 
		int xvel,yvel;
		SDL_Surface* image;
		SDL_Rect box;
		bool collision(SDL_Rect* rec1,SDL_Rect* rec2);
 
	public:
		ball(SDL_Surface* img, int x, int y, int w, int h, int xVel, int yVel);
		~ball();
		void show();
		void move(SDL_Rect* player1,SDL_Rect* player2);
		//ball out of screen
		int isOut();
		void setBack(int x,int y, int w, int h, int xVel, int yVel);
	};
 
 
#endif

PADDLE.H

#include <SDL/SDL.h>
#ifndef PADDLE_H
#define PADDLE_H
 
	class paddle{
		SDL_Rect box;
		SDL_Surface* image;
		int yvel;
		int point;
	public:
		paddle(SDL_Surface* img, int x, int y, int w, int h, int yVel);
 
	~paddle();
 
	SDL_Rect* getRect();
 
 
	void moveUp();
	void moveDown();
	void show();
		void incpoint();
        	void setBack(int x,int y,int w,int h,int yVel);
       		 int getPoints();
	};
#endif
opus/spring2013/jpettie/start.txt · Last modified: 2013/08/25 12:16 by 127.0.0.1