Вы находитесь на странице: 1из 65

Embedded Logic Design

Logistics, Plagiarism, Makefile, Revision of C

Alexander Fell
alex@iiitd.ac.in

August 5th , 2014

1 / 59

Outline
Logistics
Rules
Timings
Evaluation
Plagiarism
Slices
Subversion/Git
Makefile
Revision of C
Indentation
Basic Types
Control Flow
Data Structures
Specialties for Embedded Systems
Assembler
References
2 / 59

Why ELD?

3 / 59

Why ELD?

Programmers find hardware too difficult.


m
Programming is tough for electrical engineers.

3 / 59

Bad Code
1
2
3
4
5
6
7
8
9
10
11
12

module part1 ( CLK , RST , getGrants_requests , EN_getGrants , getGrants ) ;


[...]
always@ ( p osedge CLK ) b e g i n
i f ( RST ) b e g i n temp < 1 d0 ; end e l s e b e g i n i f ( temp $ EN ) temp < temp $ D_IN ; end
always@ ( r o u t e r _ i p _ 0 _ o p _ v c N o $ D _ O U T o r o u t p u t P o r t s _ 2 _ r e s p o n s e _ p u t o r
outputPorts_0_response_put or outputPorts_1_response_put ) begin
case ( router_ip_0_op_vcNo$D_OUT )
2 d0 : t e m p 1 $ w s e t _ 1 __ V A L _ 1 = o u t p u t P o r t s _ 0 _ r e s p o n s e _ p u t ;
2 d1 : t e m p 1 $ w s e t _ 1 __ V A L _ 1 = o u t p u t P o r t s _ 1 _ r e s p o n s e _ p u t ;
d e f a u l t : t e m p 1 $ w s e t _1 _ _ V A L _ 1 = o u t p u t P o r t s _ 2 _ r e s p o n s e _ p u t ;
endcase
[...]

56708 lines down the code:


1
2
3
4
5
6
7
8
9
10
11
12

module part2 ( CLK , RST , getGrants_requests , EN_getGrants , getGrants ) ;


[...]
always@ ( p osedge CLK ) b e g i n
i f ( RST ) b e g i n temp < 1 d0 ; end e l s e b e g i n i f ( temp $ EN ) temp < temp $ D_IN ; end
always@ ( r o u t e r _ i p _ 1 _ o p _ v c N o $ D _ O U T o r o u t p u t P o r t s _ 2 _ r e s p o n s e _ p u t o r
outputPorts_0_response_put or outputPorts_1_response_put ) begin
case ( router_ip_1_op_vcNo$D_OUT )
2 d0 : t e m p 1 $ w s e t _ 1 __ V A L _ 1=o u t p u t P o r t s _ 0 _ r e s p o n s e _ p u t ;
2 d1 : t e m p 1 $ w s e t _ 1 __ V A L _ 1=o u t p u t P o r t s _ 1 _ r e s p o n s e _ p u t ;
d e f a u l t : t e m p 1 $ w s e t _1 _ _ V A L _ 1=o u t p u t P o r t s _ 2 _ r e s p o n s e _ p u t ;
endcase
[...]

4 / 59

Good Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

module part ( CLK , RST , getGrants_requests , EN_getGrants , getGrants ) ;


[...]
// RESET r o u t i n e
always@ ( p osedge CLK ) b e g i n
i f ( RST ) b e g i n matrix < 1 d0 ;
end e l s e b e g i n
i f ( matrix $ EN ) b e g i n
matrix < matrix $ D_IN ;
end
end
// Check f o r t h e v a l u e s o f t h e o u t p u t p o r t e n a b l e s i g n a l s .
//
I f i t i s f r e e , s e n d t h e data , w a i t o t h e r w i s e .
always@ (
router_ip_1_op_vcNo$D_OUT or outputPorts_2_response_put or
outputPorts_0_response_put or
outputPorts_1_response_put ) begin
c a s e ( r o u t e r _ i p _ 1 _ o p _ v c N o $ D _ O U T ) // f r e e s i g n a l from o u t p u t p o r t D t o i n p u t p o r t 1
2 d0 : t e m p 1 $ w s e t _1 _ _ V A L _ 1=o u t p u t P o r t s _ 0 _ r e s p o n s e _ p u t ;
2 d1 : t e m p 1 $ w s e t _1 _ _ V A L _ 1=o u t p u t P o r t s _ 1 _ r e s p o n s e _ p u t ;
d e f a u l t : t e m p 1 $ w s e t _ 1_ _ V A L _ 1=o u t p u t P o r t s _ 2 _ r e s p o n s e _ p u t ;
endcase
[...]

5 / 59

Bad Wiring

Source: http://gamercurmudgeon.blogspot.in/2011/04/chaos.html

6 / 59

Why ELD?

Because. . .
Embedded systems are ubiquitous!
I
I
I
I
I

A programmer should have knowledge about underlying hardware.


A electrical engineer benefits from knowledge about controller programming.
In ELD both are fused together.

7 / 59

Class

Requirements:
I
I
I

Computer architecture
Operating systems
Basic knowledge about C, Embedded C, Makefiles from the refresher course

http://www.usebackpack.com/
Enroll to Embedded Logic Design
I
I
I

Slides (uploaded before class)


Course material
Lab assignments

Feedback, criticism, suggestions:


I
I

Anonymously: http://bit.ly/ELD-feedback
Non-anonymously:
I
I

In person: B-101
By email: alex@iiitd.ac.in

8 / 59

Labs I
First 15-30 minutes, Q&A session about the next assignment
Requirements
I

Decent knowledge about your own system


I
I
I

I
I
I
I

Console
Compilers
Environments

Blinking LED, driving LCD using Arduino library


pinMode, digitalWrite, etc
Binary, hexadecimal number systems
Boolean algebra
Linear Circuits
C, ASM

Ms. Aarti and TAs are the commanders


No drinks nor food in lab
Submission of lab assignments through git or subversion
Last submission before deadline is evaluated
9 / 59

Labs II

Resources:
I
I
I
I
I
I

AVR library documentation


Arduino documentation and schematics
Source code of the library
Books about embedded systems
Various web sites, blogs, etc.
Search engines

MTech/PhD: Final project


I
I
I
I

Project Proposal incl. description, milestones, etc. in LATEX


Can include micro controller, FPGA, other hardware
Subject to approval
Presentation and demo by the end of the semester

10 / 59

Timings

Classes:
I
I
I

Tuesday, 2:00pm to 3:00pm (C02)


Thursday, 11:00am to 12:00pm (C02)
Friday, 2:00pm to 3:00pm (C02)

Lab:
I

Monday, 11:00am to 1:00pm (L32, L33)

Office Hours:
I
I

Wednesday, 11:00am to 12:30pm or prior appointment (B-101)


TA: Please contact them.

11 / 59

Plagiarism

According to institute policy:


Once caught: Reduction in grade
Second one: Fail grade in the course
Does not apply to the assignments, however the policy is followed for tests,
quizzes, exams, etc.

12 / 59

Tentative Schedule
Week 1-2

Week 3-4

Course logistics
Introduction into plagiarism and ethics
Makefile
Revision of C
I

Week 5-6

Week 7-8
Week 9-10
Week 11-12
Week 13

Specialties in C regarding embedded systems

Assembler
Introduction to embedded systems, microcomputers,
microprocessors, microcontrollers, FPGAs, ASICs
Architectural overview of the AVR microcontroller board
Timers, counters, watchdog, PWM, ADCs
Interrupts, interfacing external sensors
Communication protocols: RS232, I2C, SPI, 1 Wire
Introduction into FPGAs
Introduction into Verilog HDL
Combinatorial circuits
Sequential circuits
FSM
13 / 59

Tentative Evaluation I
BTech:
I
I
I
I
I
I

C refresher course: 6%
JAVA introductory course: 2%
Homework, (surprise) tests: 10%
Lab assignments: 32%
Mid term exam: 25% (micro controller)
Final exam: 25% (FPGA)

MTech/PhD:
I
I
I
I
I

Homework, (surprise) tests: 10%


Lab assignments: 35%
Mid term exam: 20% (micro controller)
Final exam: 20% (FPGA)
Final project: 15%

Raghav Rule: If you miss lab without excuse (e.g. medical certificate):
Scoreyou = Scoregroup 0.7

14 / 59

Tentative Evaluation II

Lab Assignments (BTech & MTech)


Same as last year, feel free to ask your seniors
This year: Each assignment is followed by a test in class
I
I
I
I

marks

yourTest
marksassignment = oldMarksassigment max(marks
test )
Probably but not limited to multiple choice questions
Will check mostly for the understanding of underlying concepts.
Simple thing, if you did your assignments by yourself and understood, what you did.

Example:
Your score in the assignment: oldMarksassigment = 13
In the test you have 8 marks out of 10 possible marks, hence
marksassignment = 10.4

15 / 59

Questions?

Questions?

16 / 59

Outline
Logistics
Rules
Timings
Evaluation
Plagiarism
Slices
Subversion/Git
Makefile
Revision of C
Indentation
Basic Types
Control Flow
Data Structures
Specialties for Embedded Systems
Assembler
References
17 / 59

Why a Lecture about Plagiarism in ELD?


1. You do not know, what plagiarism actually means.
2. You do not care.
3. Consequences are severe.
Ignorance does not protect you from punishment.

18 / 59

Why a Lecture about Plagiarism in ELD?


1. You do not know, what plagiarism actually means.
2. You do not care.
3. Consequences are severe.
Ignorance does not protect you from punishment.
Indian Penal Code, 1860, Section 363: Whoever kidnaps any person from *[India] or
from lawful guardianship, shall be punished with imprisonment of either description for
a term which may extend to seven years, and shall also be liable to fine.
But Your Honor, I didnt know that I am not allowed to kidnap somebody. How
many times can you use that sentence to evade legal prosecution? Do you think this is
going to work?

18 / 59

Why a Lecture about Plagiarism in ELD?


1. You do not know, what plagiarism actually means.
2. You do not care.
3. Consequences are severe.
Ignorance does not protect you from punishment.
Indian Penal Code, 1860, Section 363: Whoever kidnaps any person from *[India] or
from lawful guardianship, shall be punished with imprisonment of either description for
a term which may extend to seven years, and shall also be liable to fine.
But Your Honor, I didnt know that I am not allowed to kidnap somebody. How
many times can you use that sentence to evade legal prosecution? Do you think this is
going to work?
Latin Dictionary: Thief or kidnapper[6]
The Latin noun plaga referred to a hunting net or snare which was used for capturing
game. The netting of such animals was termed plagium. By extension, this word was
also used for the crime of kidnapping children or freemen and selling them as slaves,
the kidnapper being called a plagiarius. [1]
18 / 59

Prevalent and extensive Plagiarism in this Institute

In homeworks
In assignments
In the final project report 0 marks in the whole project, 15% of overall score
In Master Thesis Rejection of thesis, expulsion from institute: no degree, no
placement, no job (2 years wasted)

19 / 59

Original Text (from [2], p. 23)


The Virtex-II Pro is an SRAM-based island-style FPGA with several heterogeneous
computational elements interconnected through a complex routing matrix. The basic
logic tile is the configurable logic block (CLB), consisting of four slices and two 3-state
buffers. These CLBs are tiled across the device in rows and columns with a segmented,
hierarchical interconnect tying all the resources together.
The smallest piece of logic from the perspective of the interconnect structure is the
CLB. It consists of four equivalent slices organized into two columns of two slices each
with independent carry chains and a common shift chain. Each slice connects to the
general routing fabric through a configurable switch matrix and to each other in the
CLB through a fast local interconnect. Each slice comprises primarily two 4-LUT
function generators, two programmable registers for state holding and fast carry logic.
A handful of other gates provide extra functionality in the slice, including an XOR gate
to complete a 2-bit full adder in a single slice, an AND gate to improve multiplier
implementations in the logic fabric and an OR gate to facilitate implementation of
sum-of-products chains.
Write a short summary to explain slices!
20 / 59

FPGA I
result = a b + c d . . .

An abstract view of an FPGA; logic cells are embedded in a general routing structure[8]

21 / 59

FPGA II

Configurable Logic Blocks (CLBs) consisting of slices with LUTs, muxers and flip-flops, and Interconnect[12]

22 / 59

FPGA III
LUT Truth Table
Example: 2 input LUT (2-LUT) storing result = a + b
a
0
0
1
1

b
0
1
0
1

result
0
1
1
1

Example: multiple operations summarized into one 2-LUT: result = a b


a
0
0
1
1

b
0
1
0
1

c
1
1
0
1
23 / 59

Origin of Plagiarism

Laziness
Lack of English vocabulary.
The sentences so soooo nice, concise and beautiful. I must have it.
Lack of understanding

24 / 59

Quotation

According to Scott Hauck and Andre DeHon in [2], a slice with reference to the Xilinx
Virtex-II Pro FPGA comprises primarily two 4-LUT function generators, two
programmable registers for state holding and fast carry logic. (p. 23). However in
recent developments regarding FPGAs, this statement does not hold anymore. Due
to. . .

In your work, you discuss this statement. To inform the reader of your work, what you
are talking about, you need to cite the statement.

25 / 59

Citation

Recent developments regarding FPGAs suggest that slices do not consist of 4-LUTs
including periphery anymore as stated by the authors in [2]. . . .

Summarize! Somebody already wrote what you want to say. Do not reinvent the wheel.

26 / 59

Unintentional Plagiarism

Plagiarism, even not intended and accidental, is plagiarism nevertheless


consequences
Check your work. E.g.
eTBLAST: http://etest.vbi.vt.edu/etblast3/
turnitin (commercial): http://turnitin.com/
Do not use the plagiarism detection software to check, if your plagiarized nonsense is
detected!

27 / 59

Undeserved Success vs. Deserved Failure


Cheating: Unwillingness to accept the consequence of not studying.[9]

28 / 59

Further Reading

http://www.umuc.edu/writingcenter/plagiarism/index.cfm
http://en.wikipedia.org/wiki/Scientific_plagiarism_in_India
http://www.globalpost.com/dispatch/india/090921/
did-you-write-yaar-india-plagiarism-the-rise
https://www.indiana.edu/~istd/
http://tutorials.sjlibrary.org/tutorial/plagiarism/tutorial/
introduction.htm?flash=yes
http://lmgtfy.com/?q=Plagiarism+Tutorial
https://www2.fairfield.edu/library/lib_plagiarismcourtquiz.lasso

29 / 59

Outline
Logistics
Rules
Timings
Evaluation
Plagiarism
Slices
Subversion/Git
Makefile
Revision of C
Indentation
Basic Types
Control Flow
Data Structures
Specialties for Embedded Systems
Assembler
References
30 / 59

Repositories
Solve problems by slicing problems into smaller parts.
I
I
I

Working code
Added content (functions, modules, etc.)
Previously working code, does not work anymore

31 / 59

Repositories
Solve problems by slicing problems into smaller parts.
I
I
I

Working code
Added content (functions, modules, etc.)
Previously working code, does not work anymore

Option to revert back to the working code.

31 / 59

Repositories
Solve problems by slicing problems into smaller parts.
I
I
I

Working code
Added content (functions, modules, etc.)
Previously working code, does not work anymore

Option to revert back to the working code.


I

Backup your directory every time


I
I
I
I
I
I

./assignment1/
./Assign1/
./asgn1.backup/
./asgn1.backup1/
...
How will you make sure that your team partner gets the same code that you have
changed?

31 / 59

Repositories
Solve problems by slicing problems into smaller parts.
I
I
I

Working code
Added content (functions, modules, etc.)
Previously working code, does not work anymore

Option to revert back to the working code.


I

Backup your directory every time


I
I
I
I
I
I

./assignment1/
./Assign1/
./asgn1.backup/
./asgn1.backup1/
...
How will you make sure that your team partner gets the same code that you have
changed?

Use a repository
I
I
I
I

Create revisions
Tag revisions
Does not forget anything
Merge changes
31 / 59

Merging Revisions

Branching and merging of revisions[10]

32 / 59

Server Clients I

Revision 3
* changed *

Revision 3

commit
Revision 4 (new)
Revision 3
Revision 2
Revision 1
Revision 0

Revision 2
* changed *

33 / 59

Server Clients II

Cray X-MP (1986) used by e.g. Pixar (Jurassic Park) and NASA (aerodynamics of the space shuttle). In the
bench is the power and coolant system[4]. Image source: [11]
34 / 59

Server Clients III

Revision 4
* no change *

update

Revision 4
Revision 3
Revision 2
Revision 1
Revision 0

update
Revision 4

Revision 2
* changed *

update

Revision 4
* error merge *

35 / 59

Commands I

Server: svnadmin
svnadmin help
svnadmin help create

Client: svn
svn
svn
svn
svn
svn
svn
svn
svn

help
help
help
help
help
help
help
help

update
checkout
commit
add
remove
log
status

Example:
1. svn checkout
svn+ssh://username@192.168.1.152/home/groupXX /testSVN/
2. svn add newAwesomeFile.txt
3. svn commit -m"Added an awesome file"
4. svn update
36 / 59

Commands II

To prevent password entering SSH public/private key authentication

37 / 59

Outline
Logistics
Rules
Timings
Evaluation
Plagiarism
Slices
Subversion/Git
Makefile
Revision of C
Indentation
Basic Types
Control Flow
Data Structures
Specialties for Embedded Systems
Assembler
References
38 / 59

Makefile

Recipe, how to compile a program


Allows to recompile only those parts, which need to be recompiled automatically
(time stamp comparison)
Not only for C or LATEX
You do not have to remember all the parameters every time
Allows easy adaptation to a new situation (e.g. source code file)

39 / 59

Speedup
Imagine, you are a programmer changing a driver in the Linux Kernel and put an
empty new line somewhere.
I

Linux Kernel: 14.998.651 lines of code (version 3.2), average compilation time on a
2.8GHz, quad core, threaded: 45 minutes.

Regarding functionality no effect


However the whole kernel needs to be recompiled.
So what if only the sections, which depend on the changed file, are recompiled?
src1.c src2.c scr3.c
ext3.o

ext4.o

...

raiserFS.o

vfat.o

btrfs.o

usb1.o

file_systems.o

usb2.o

usb3.o

usb.o

io_system.o

...
40 / 59

Dependencies of the Linux Kernel 2.6.8

Source: [5]

41 / 59

Internals of a Makefile

42 / 59

Dependencies among Rules I

src1.c src2.c scr3.c


ext3.o

ext4.o

...

raiserFS.o

vfat.o

btrfs.o

usb1.o

file_systems.o

usb2.o

usb3.o

usb.o

io_system.o

...

usb.o cannot be compiled without having compiled usb1.o, usb2.o and usb3.o
priorly.

43 / 59

Further Reading

Dum Ka Biryani, Make for each other by Shakthi Kannan [7]


Also available on http://www.usebackpack.com

44 / 59

Outline
Logistics
Rules
Timings
Evaluation
Plagiarism
Slices
Subversion/Git
Makefile
Revision of C
Indentation
Basic Types
Control Flow
Data Structures
Specialties for Embedded Systems
Assembler
References
45 / 59

Compilation Flow

46 / 59

Indentation I
Write your code in such a way that it is readable (like in Python). You will get special
marks for beautiful code in the assignments!
Example:
1
2
3
4
5
6

7
8
9
10
11
12
13
14

uint8_t hourDisplay ;
i f ( hour == 0 ) { hourDisplay = 1 2 ; // i f h o u r == 0
}
e l s e i f ( hour > 1 2 ) { // s o m e t h i n g w i l l happen h e r e
hourDisplay = hour 1 2 ;
} e l s e { hourDisplay = hour ; } // awesome , I n e v e r saw a more b e a u t i f u l code
i f ( hourDisplay < 1 0 )
setPosition ( 1 0 , 0 ) ;
e l s e setPosition ( 9 , 0 ) ;
writeByte ( 0 x4 , 0 ) ; writeNumber ( hourDisplay ) ; writeByte ( : , 0 ) ;
i f ( minute < 1 0 ) {
writeByte ( 0 , 0 ) ;
}
writeNumber ( minute ) ;
47 / 59

Indentation II
Same code from previous example:
1
2
3
4
5
6
7
8
9
10
11
12

uint8_t hourDisplay ;
i f ( hour == 0 )
hourDisplay = 1 2 ;
e l s e i f ( hour > 1 2 ) {
hourDisplay = hour 1 2 ;
} else {
hourDisplay = hour ;
}
i f ( hourDisplay < 1 0 )
setPosition ( 1 0 , 0 ) ;
else
setPosition ( 9 , 0 ) ;

// i f a new day s t a r t s

13
14
15

writeByte ( 0 x4 , 0 ) ; writeNumber ( hourDisplay ) ; writeByte ( : , 0 ) ;


[...]

48 / 59

Basic Types

49 / 59

Control Flow

50 / 59

Data Structures

51 / 59

Specialties for Embedded Systems

52 / 59

Outline
Logistics
Rules
Timings
Evaluation
Plagiarism
Slices
Subversion/Git
Makefile
Revision of C
Indentation
Basic Types
Control Flow
Data Structures
Specialties for Embedded Systems
Assembler
References
53 / 59

Mnemonics
Atmega328P supports 131 instructions (refer to chapter 37 of the data sheet[3])
Somehow the instructions are represented in assembler (program.s) Mnemonics
Interrupt the built process by supplying the -S parameter to gcc.

Instruction Set of the Atmega328P CPU; source: [3]

54 / 59

Assembler Example

1
2
3
4
5
6
7
8
9
10
11
12
13

rjmp .L2
.L3 :
l d d r25 , Y+2
l d d r24 , Y+1
add r24 , r25
s t d Y+2, r24
l d d r24 , Y+1
s u b i r24 , lo8 ( (1) )
s t d Y+1, r24
.L2 :
l d d r24 , Y+1
c p i r24 , lo8 ( 1 0 0 )
b r l t .L3

What does it do?

55 / 59

Questions?

Questions?

56 / 59

Outline
Logistics
Rules
Timings
Evaluation
Plagiarism
Slices
Subversion/Git
Makefile
Revision of C
Indentation
Basic Types
Control Flow
Data Structures
Specialties for Embedded Systems
Assembler
References
57 / 59

References I
[1]

English-Word Information.
Plagiarisms: Past and Present.
http://wordinfo.info/unit/3424/ip:8/il:P.
Accessed: 2014-06-24.

[2]

Scott Hauck and Andr


e DeHon.
Reconfigurable Computing: The Theory and Practice of FPGA-Based Computation.
Morgan Kaufmann Publishers Inc., San Francisco, CA, USA, 2007.

[3]

Atmel Inc.
ATmega48A/PA/88A/PA/168A/PA/328/P Summary.
http://www.atmel.com/Images/Atmel-8271-8-bit-AVR-Microcontroller-ATmega48A-48PA-88A-88PA-168A-168PA-328-328P_
datasheet_Summary.pdf.
Accessed: 2013-08-06.

[4]

Cray Research Inc.


The CRAY X-MP - Series of Computer Systems.
1985.
Accessed: 2014-07-15.

[5]

James M. Kenefick Jr.


Coverage Galaxy Map - Linux Test Project.
http://ltp.sourceforge.net/coveragemap.php.
Accessed: 2014-06-24.

[6]

Latdict - Latin Dictionary & Grammar Resources.


Latin definition for: plagiarius, plagiarii.
http://www.latin-dictionary.net/definition/30640/plagiarius-plagiarii.
Accessed: 2014-06-24.

[7]

Shakthi Kannan.
Dum Ka Biryani, Make for each other.
http://shakthimaan.com/downloads/glv/presentations/dum-ka-biryani-make-for-each-other.pdf.
Accessed: 2014-06-24.

58 / 59

References II

[8]

Mike Strickland.
The evolution of FPGA coprocessing.
http://www.electronicproducts.com/Digital_ICs/Standard_and_Programmable_Logic/The_evolution_of_FPGA_coprocessing.aspx.
Accessed: 2013-10-01.

[9]

William B. Watterson.
Theres Treasure Everywhere.
March 1996.

[10] Wikipedia - The Free Encyclopedia.


Apache Subversion.
https://en.wikipedia.org/wiki/Apache_Subversion.
Accessed: 2014-06-27.
[11] Wikipedia - The Free Encyclopedia.
Cray X-MP.
http://commons.wikimedia.org/wiki/File:Cray_X-MP.jpg, 2010.
Accessed: 2014-07-15.
[12] www.fpga4fun.com and KNJN LLC.
How FPGAs work.
http://www.fpga4fun.com/FPGAinfo2.html.
Accessed: 2013-13-30.

59 / 59

Вам также может понравиться