Skip to content
Snippets Groups Projects
Commit 25727a36 authored by cnlohr's avatar cnlohr
Browse files

Improve demonstration of using self-modifying code.

parent f63b60ab
No related branches found
No related tags found
No related merge requests found
...@@ -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 = ReadCSRSelfModifySimple( i );
if( rv ) if( rv )
printf( "%03x = %08x\n", i, rv ); printf( "%03x = %08x\n", i, rv );
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment