Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
ch32v003fun
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Andreas Horn
ch32v003fun
Commits
25727a36
Commit
25727a36
authored
2 years ago
by
cnlohr
Browse files
Options
Downloads
Patches
Plain Diff
Improve demonstration of using self-modifying code.
parent
f63b60ab
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
examples/self_modify_code/self_modify_code.c
+26
-1
26 additions, 1 deletion
examples/self_modify_code/self_modify_code.c
with
26 additions
and
1 deletion
examples/self_modify_code/self_modify_code.c
+
26
−
1
View file @
25727a36
...
@@ -8,6 +8,8 @@
...
@@ -8,6 +8,8 @@
uint32_t
count
;
uint32_t
count
;
// This is a complicated way to do it from C land, as a demonstration
// Tell the compiler to put this code in the .data section. That
// Tell the compiler to put this code in the .data section. That
// will cause the startup code to copy it from flash into RAM where
// will cause the startup code to copy it from flash into RAM where
// it can be easily modified at runtime.
// it can be easily modified at runtime.
...
@@ -38,10 +40,14 @@ uint32_t ReadCSRSelfModify( uint16_t whichcsr )
...
@@ -38,10 +40,14 @@ uint32_t ReadCSRSelfModify( uint16_t whichcsr )
// can know what opcode we want to use, then we can let C tell us
// can know what opcode we want to use, then we can let C tell us
// what register it would like the value in.
// what register it would like the value in.
//
//
// The fence is needed to make sure the CPU knows to not use
// cached instructions.
//
// The constraints are "ret" is a "write" register, and register a3
// The constraints are "ret" is a "write" register, and register a3
// is going to be clobbered by the assembly code.
// is going to be clobbered by the assembly code.
asm
volatile
(
asm
volatile
(
".global readCSRLabel
\n
"
".global readCSRLabel
\n
"
" fence
\n
"
"readCSRLabel:
\n
"
"readCSRLabel:
\n
"
" csrrs a3, 0x000, x0
\n
"
" csrrs a3, 0x000, x0
\n
"
" addi %[ret], a3, 0
\n
"
" addi %[ret], a3, 0
\n
"
...
@@ -51,6 +57,25 @@ uint32_t ReadCSRSelfModify( uint16_t whichcsr )
...
@@ -51,6 +57,25 @@ uint32_t ReadCSRSelfModify( uint16_t whichcsr )
}
}
uint32_t
ReadCSRSelfModifySimple
(
uint16_t
whichcsr
)
__attribute__
((
section
(
".data"
)))
__attribute__
((
noinline
));
uint32_t
ReadCSRSelfModifySimple
(
uint16_t
whichcsr
)
{
uint32_t
ret
;
uint32_t
csrcmd
=
0x000026f3
|
(
whichcsr
<<
20
);
asm
volatile
(
".global readCSRLabel
\n
"
" la a3, readCSRLabel
\n
"
" sw %[csrcmd], 0(a3)
\n
"
" fence
\n
"
"readCSRLabel:
\n
"
" csrrs a3, 0x000, x0
\n
"
" addi %[ret], a3, 0
\n
"
:
[
ret
]
"=r"
(
ret
)
:
[
csrcmd
]
"r"
(
csrcmd
)
:
"a3"
);
return
ret
;
}
int
main
()
int
main
()
{
{
SystemInit48HSI
();
SystemInit48HSI
();
...
@@ -64,7 +89,7 @@ int main()
...
@@ -64,7 +89,7 @@ int main()
int
i
;
int
i
;
for
(
i
=
0x000
;
i
<
0x1000
;
i
++
)
for
(
i
=
0x000
;
i
<
0x1000
;
i
++
)
{
{
uint32_t
rv
=
ReadCSRSelfModify
(
i
);
uint32_t
rv
=
ReadCSRSelfModify
Simple
(
i
);
if
(
rv
)
if
(
rv
)
printf
(
"%03x = %08x
\n
"
,
i
,
rv
);
printf
(
"%03x = %08x
\n
"
,
i
,
rv
);
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment