Showing posts with label geeky. Show all posts
Showing posts with label geeky. Show all posts

9.22.2007

Wired

Hey folks!

Here, for a second time, I present a glimpse into one of the facets of my academic life. As an Embedded Systems major, I am taking three decently hard but fun classes this semester: Digital Logic, Digital Signal Processing, and Operating Systems. Since Digital logic is currently my favorite of the three, I am going to show you what one of my recent labs was.

The assignment was to build a circuit that would display the digits of my phone number - simple, right? Not so much. The way it would display them would be by taking as input the numerical position of the digit, in binary, and using a simple seven-segment display (like one digit of your digital watch) to show the number. This is how the input works:
Input Output
0000 => 2
0001 => 0
0010 => 7
0011 => -
0100 => 7
0101 => 5
0110 => 1
0111 => -
1000 => 1
1001 => 0
1010 => 7
1011 => 4
1100 => X
1101 => X
1110 => X
1111 => X

The last four places didn't need to be anything in specific, which made the logic easier to design than otherwise. I won't go into boolean algebra or Karnaugh maps, but suffice it to say, it takes a bit of manipulating to get those numbers to come out.

A little about gates:
Gates are the basic unit of any circuit. They are like dendrites in your brain (i think) - they actually manipulate the ones and zeros running around inside the computer. There are 6 different types of gates: NOT, AND, OR, NAND, NOR, and XOR. I was able to simplify my circuit down to using only two types of gates: NANDs and NOTs.
A NOT gate is the most simple of the gates. it takes a bit (a one or a zero) and "flips" it. If it was a one, it makes it a zero, and vice versa.

The truth table for a NOT gate looks like this:
Input
Output
0
1
1
0

A NAND gate is slightly more complicated. It takes two or more inputs, and if they all are '1' then it returns a '0', otherwise, it returns a '1'.

The truth table for a two-input NAND gate looks like this:
Input 1
Input 2
Output
0
0
1
0
1
1
1
0
1
1
1
0

Now, before I actually physically built the circuit, I first simulated it in a program that allows you to test your design for errors. This is what the circuit looks like in symbolic representation:



The five dots on the left are the inputs (four binary digits and one that is always '1', for certain reasons). The green symbols are the gates. The triangle with the little circle in front of it is a NOT gate, and the one that looks like a D with a circle in front of it is a NAND gate. You can see the 7-segment display on the right. All the lines are wires that I have to connect.

Now, here are some pictures of the finished circuit. Ivan and I worked on putting this together for about 2 hours or so.





The wires we used



You can see the 7-segment display on the top right




The input switches - we used the first four.
When the switch is down, it inputs a zero,
and when it is up, it inputs a one.



Close-up of circuit

The black things you see in the above picture are chips, each of which contain anywhere from 2 to 6 gates, depending on what type of gate is on the chip. We used NOTs (6 per chip), 2-input NANDs ( 4 per chip) and 4-input NANDs (2 per chip).

Here is a video of me switching through the numbers:


Well, hopefully this didn't bore you too much, I know at least Ivan and probably Joel would like it, maybe more. I had a lot of fun doing this project, and just wanted to share it with you! :)

Have a happy Sabbath, everyone!

3.19.2007

I thought you guys might be interested to see part of what I do for my Organization, Architecture, and Assembly Language class. This is a program in assembly language that accepts two integers, and multiplies them manually, instead of using any built-in function. Assembly language is one step away from machine code, which the actual processor inside the computer can read. To show you what that looks like, I have translated the red bolded command below into machine code.
In hexidecimal notation, it would be 0x0251A024
In binary notation, it would be 00000010010100011010000000100100
Or, going the other direction, to a more sophisticated language, the green italicized part of the assembly code would be the following in pseudocode, which is much closer to languages like c++ or java:

int mult (int a, int b){
if (a == 0 || b == 0){
return 0;

}else if (a[0] == '0'){
return (mult (a<<1,>>1);

}else {
return (a + mult(a<<1,>>1);

}

}


And here is the actual assembly code:
[The weird lines across the page are a result of me using an easy way to indent lines in html...it is really kind of a pain to do it right, and evidently this website makes lines when I use that method]

#Lab 6
#Jonathan Gerrans

#Data Memory Section
.data
enterint: .asciiz "Please enter two integers: "
ans: .asciiz "The answer is: "
error: .asciiz "Error! Overflow!"
.align 2


#Program Memory Section
.text
.globl main




main:
li $v0, 4
la a0, enterint
syscall
li $v0, 5 #read in values
syscall
li $v1, 5
syscall
addi $s0, $zero, 0x00000001 #mask
addi $s1, $zero, 0x80000000 #mask2
move $s2, $v0
move $s3, $v1
move $s6, $zero
addi $s7, $zero, 0x00000020

overflow1:
addi $s6, $s6, 0x00000001 #overflow check
and $s4, $s2, $s1
sll $s2, $s2, 1
beq $s4, $zero, overflow1


overflow2;
addi $s6, $s6, 0x00000001 #overflow part two
and $s5, $s3, #s1
sll $s3, $s3, 1
beq $s5, $zero, overflow2
bgt $s6, $s7, error #print overflow error
add $a0, $zero, $v0 #first value
add $a1, $zero, $v1 #second value

jal mult
j end



mult:
beq $a0, $zero, return #if $a0 is zero, then return .
beq $a1, $zero, return #if $a1 is zero, then return .
and $t0, $a1, $v0 #check if first bit is a zero
bne $t0, $zero, notzero
sll $a0, $a0, 1 #shift a0 left one
srl $a1, $a1, 1 #shift a1 right one
jal mult #recurse
j return


notzero:
add $v0, $v0, $a0
sll $a0, $a0, 1 #shift a0 left one
srl $a1, $a1, 1 #shift a1 right one
jal mult #recurse


return: jr $ra

error:
li $v0, 4
la $a0, error
syscall
li $v0, 11 # ENDLINE
li $a0, 10
syscall
j main


end:
li $v0, 4 #Print the result
la $a0, ans
syscall
li $v0, 1
lw $a0, $v0
syscall
li $v0, 11 # ENDLINE
li $a0, 10
syscall
j main


This is so much fun!