Giao Trinh BK Tech

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 55

C/C++

C++
Programming
C Author: Thai Van Sang

Version: 1.0.2023
C/C++ Course (14 sessions)

❖ Session 1 : Overview and Memory layout of C/C++


❖ Session 2 : Investigate about Primary variables and variables access scope (const, static, extern, volatile)
❖ Session 3 : Investigate about Derived variables (Function, Default function, Array)
❖ Session 4 : Investigate about Derived variables (Pointer , Reference, Pointer and Array)
❖ Session 5 : Type Casting (Dynamic casting & Static casting) in C/C++
❖ Session 6 : Investigate about User defined variables and Macro define
❖ Session 7 : Investigate about User defined variables (Class/Structure)
❖ Session 8 : Investigate about OOP (Class/Object/Inheritance)
❖ Session 9 : Investigate about OOP (Polymorphism/Encapsulation)
❖ Session 11 : Investigate about OOP (Function overloading/Operator overloading)
❖ Session 12 : Loops, Decision Making (For, While, Do-while, Switch case, If- Else,..)
❖ Session 13 : Implement exercises (Ram)
❖ Session 14 : Practice and final exam.
Course Goals
G-01 G-04
01 Know and present 04 Know and practice
about C/C++ concepts basic OOP exercises

G-02 G-05
02 Know and practice 05 Know and practice
basic C/C++ exercises advanced OOP exercises

G-03 G-06
03 Know and present 06 Introduce C/C++ language
about OOP concepts application in embedded
system
Assessment

Q&A

Research Testing
Judgment
Learn online
Pass/Fail
Unit Design Coding

Pratice
How to compile a C/C++ program

I-1.0 : How to compile a C/C++ program


(Refer : https://www3.ntu.edu.sg/home/ehchua/programming/cpp/gcc_make.html)
Lesson 1
C++ Install IDE

Visual Studio 2019 Visual Studio Code Dev C++

Lesson 1
How to write basic C/C++ program

Terminal:
sim.exe –Info class –Cmd 01

Lesson 1
Memory layout in C/C++

I-1.2 : Memory layout in C/C++


(Refer : https://youtu.be/9890mLEpECg)
Memory layout in C/C++

I-1.3 : DS - Memory layout in C/C++


(Refer : https://youtu.be/9890mLEpECg)
Memory layout in C/C++

I-1.4 : BSS - Memory layout in C/C++


(Refer : https://youtu.be/9890mLEpECg)
Memory layout in C/C++

I-1.5 : HEAP - Memory layout in C/C++


(Refer : https://youtu.be/9890mLEpECg)
Memory layout in C/C++

I-1.6 : Stack - Memory layout in C/C++


(Refer : https://youtu.be/9890mLEpECg)
Summary session 1
❑ Cách tạo một project bằng visual studio :
• File -> New -> Project - > Empty Project -> New -> Config your project (Name Project, Location Project,…)
-> Create.
❑ Cấu trúc một chương trình trong C/C++:
• Header file : #include <iostream>,…
• Body file : int main(int argc, char* argv[]). //project.e
❑ Memory layout trong c/c++:
• Text : Lưu trữ lệnh của chương trình.
• DS : Lưu biến global (toàn cục) và biến static được lập trình viên khởi tại với giá trị khác 0.
• BSS : Lưu biến global (toàn cục) và biến static được lập trình viên khởi tại với giá trị bằng 0 hoặc không được khởi
tạo.
• Heap : Là một nhớ động, bộ nhớ cho phép lập trình viên cấp phát và thu hồi.
Note: Khi thao tác với bộ nhớ Heap, cần lưu ý khi cấp phát buộc phải thu hồi, nếu không thì sẽ xảy ra “Memory
leak”.
• Stack : Là bộ nhớ để lưu trữ các biến local, stack frame của chương trình trong quá trình chạy (run_time).
C/C++ Variables

I-1.1 : Data types in C/C++


(Refer : https://www.geeksforgeeks.org/data-types-in-c/)
C/C++ Variables - Primary

Predict the output of source code


C/C++ Variables - Derived
Variables Derived

Function Array Reference

Stack frame Static Array Pointer

Local variable Dynamic Array Reference

Global variable Const Array

Static variable Reference Array

Const variable
C/C++ Variables - Function

I-1.7 : Function in C/C++


(Refer : https://youtu.be/9890mLEpECg)
C/C++ Variables - Function

I-1.7 : Stack Frame in C/C++


(Refer : https://youtu.be/9890mLEpECg)
C/C++ Variables - Array

HEAP
Allocate dynamic

STACK
Allocate static
C/C++ Variables - Array
C/C++ Variables - Array
C/C++ Variables – Pointer & Reference

0x11223344
0x000000AA

0x000000AA mVar
0x0000FFFC

0x0000FFFC mP1

0xF000F000

mP2
Address of variable Value of variable
C/C++ Variables – Pointer & Reference

01 Const Pointer

Pointer 02 Pointer to Const

03 Reference pointer
C/C++ Variables – Pointer & Reference
C/C++ Variables – Pointer & Reference

0x0000FFFF
0xAAAA0000

8
ptr
0x0000FFFF

i,ref
C/C++ Variables – Pointer & Reference

Pointer with “Const”


C/C++ Variables – Pointer & Reference

Double Pointer
C/C++ Variables – Pointer & Reference

Reference Pointer
C/C++ Variables – Pointer & Reference
C/C++ Variables – User Defined

Variables User Defined

Class / Structure Union Enum / Typedef / Define


C/C++ Variables – Typedef / Define
C/C++ Variables – Struct / Union
C/C++ Variables – Typedef/ enum/ define
Class / Structure
01 02 03

Class/ Object Encapsulation Abstraction

08 04
Function Polymorphism
overloading

07 06 05
Operator
Friend function Inheritance
overloading
Class / Object
Encapsulation / Abstraction
Polymorphism
Inheritance
Inheritance
Friend function
Operator overloading

Operators that can be overloaded Examples

Binary Arithmetic +, -, *, /, %

Unary Arithmetic +, -, ++, —

Assignment =, +=,*=, /=,-=, %=

Bitwise & , | , << , >> , ~ , ^

Function call ()

Subscript []

Relational >, < , = =, <=, >=


Function overloading
Class / Object
Loops
Loops
Decision Making
Design RAM

CPU RAM Memory

Register
APB bus AXI bus 4096
bytes

Data Move Controller

Overview block diagram


RAM Function
0xFFFF5000 0x11 0xFFFF5000 0x11
0xFFFF5001 0x22 0xFFFF5001 0x22
0xFFFF5002 0x33 0xFFFF5002 0x33
0xFFFF5003 0x44 0xFFFF5003 0x44
0xFFFF5004 0x55 0xFFFF5004 0x55
0xFFFF5005 0x66 0xFFFF5005 0x66
Move
0xFFFF5006 0x77 0xFFFF5006 0x77
0xFFFF5007 0x88 0xFFFF5007 0x88

0xFFFF5100 0x0 0xFFFF5100 0x11


0xFFFF5101 0x0 0xFFFF5101 0x22
0xFFFF5102 0x0 0xFFFF5102 0x33

Memory Memory
RAM Register & Memory

RAM base address : 0xFFFF0000 / Memory base address : 0xFFFF5000

Symbol Offset R/W Access size Description

CR xxx_base + 0x0 R/W 32 Control register

SRC xxx_base + 0x4 R/W 32 Source address register

DST xxx_base + 0x8 R/W 32 Destination address register


RAM Control Register (CR)
Bit : 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
- - - - - - - - - - LEN

Initial value : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
R/W : R R R R R R R R R R R/W R/W R/W R/W R/W R/W

Bit : 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
- - - - - - - - - - - INT - - - EN

Initial value : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
R/W : R R R R R R R R R R R R R R R R/W

Bit Bit Name Initial Value R/W Description


21 to 16 LEN 0 R/W Size move data

4 INT 0 R Status interrupt

0 EN 0 R/W Start move data


0 : Disable
1 : Enable. Auto set to 0 after finishing move operation.
RAM Source Address Register (SRC)
Bit : 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
SRC

Initial value : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
R/W : R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W

Bit : 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
SRC

Initial value : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
R/W : R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W

Bit Bit Name Initial Value R/W Description


31 to 0 SRC 0 R/W Source address
RAM Destination Address Register (DST)
Bit : 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16
DST

Initial value : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
R/W : R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W

Bit : 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
DST

Initial value : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
R/W : R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W R/W

Bit Bit Name Initial Value R/W Description


31 to 0 DST 0 R/W Destination address
Operation

o SRC = source address


o DST = destination address

Check CR.EN == 1 ?
yes

no

Start operation
Dump message not CR.EN = 0
start operation

Received interrupt
FILE STRUCTURE
UT_Top

Register

Test Pattern

Bus
Ram Memory

Input data Inheritance Association


FILE TEST PATTERN
Example :

Explanation :
Command Argument 1 Argument 2
[WRITE] Address register Write data

[READ] Address register None

[CHECK] Expected data None

[PASS] TRUE or FALSE None

[END] none none

You might also like