forked from NotArmin/miniProj
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdtekv-lib.c
More file actions
128 lines (118 loc) · 3.52 KB
/
Copy pathdtekv-lib.c
File metadata and controls
128 lines (118 loc) · 3.52 KB
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// dtekv-lib.c
#include "dtekv-lib.h"
#define JTAG_UART ((volatile unsigned int*) 0x04000040)
#define JTAG_CTRL ((volatile unsigned int*) 0x04000044)
void printc(char s)
{
while (((*JTAG_CTRL)&0xffff0000) == 0);
*JTAG_UART = s;
}
void print(const char *s)
{
while (*s != '\0') {
printc(*s);
s++;
}
}
void print_dec(unsigned int x)
{
unsigned divident = 1000000000;
char first = 0;
do {
int dv = x / divident;
if (dv != 0) first = 1;
if (first != 0)
printc(48+dv);
x -= dv*divident;
divident /= 10;
} while (divident != 0);
if (first == 0)
printc(48);
}
void print_hex32 ( unsigned int x)
{
printc('0');
printc('x');
for (int i = 7; i >= 0; i--) {
char hd = (char) ((x >> (i*4)) & 0xf);
if (hd < 10)
hd += '0';
else
hd += ('A' - 10);
printc(hd);
}
}
/* function: handle_exception
Description: This code handles an exception. */
void handle_exception ( unsigned arg0, unsigned arg1, unsigned arg2, unsigned arg3, unsigned arg4, unsigned arg5, unsigned mcause, unsigned syscall_num )
{
switch (mcause)
{
case 0:
print("\n[EXCEPTION] Instruction address misalignment. ");
break;
case 2:
print("\n[EXCEPTION] Illegal instruction. ");
break;
case 11:
if (syscall_num == 4)
print((char*) arg0);
if (syscall_num == 11)
printc(arg0);
return ;
break;
default:
print("\n[EXCEPTION] Unknown error. ");
break;
}
print("Exception Address: ");
print_hex32(arg0); printc('\n');
while (1);
}
/*
* nextprime
*
* Return the first prime number larger than the integer
* given as a parameter. The integer must be positive.
*/
#define PRIME_FALSE 0 /* Constant to help readability. */
#define PRIME_TRUE 1 /* Constant to help readability. */
int nextprime( int inval )
{
register int perhapsprime = 0; /* Holds a tentative prime while we check it. */
register int testfactor; /* Holds various factors for which we test perhapsprime. */
register int found; /* Flag, false until we find a prime. */
if (inval < 3 ) /* Initial sanity check of parameter. */
{
if(inval <= 0) return(1); /* Return 1 for zero or negative input. */
if(inval == 1) return(2); /* Easy special case. */
if(inval == 2) return(3); /* Easy special case. */
}
else
{
/* Testing an even number for primeness is pointless, since
* all even numbers are divisible by 2. Therefore, we make sure
* that perhapsprime is larger than the parameter, and odd. */
perhapsprime = ( inval + 1 ) | 1 ;
}
/* While prime not found, loop. */
for( found = PRIME_FALSE; found != PRIME_TRUE; perhapsprime += 2 )
{
/* Check factors from 3 up to perhapsprime/2. */
for( testfactor = 3; testfactor <= (perhapsprime >> 1) + 1; testfactor += 1 )
{
found = PRIME_TRUE; /* Assume we will find a prime. */
if( (perhapsprime % testfactor) == 0 ) /* If testfactor divides perhapsprime... */
{
found = PRIME_FALSE; /* ...then, perhapsprime was non-prime. */
goto check_next_prime; /* Break the inner loop, go test a new perhapsprime. */
}
}
check_next_prime:; /* This label is used to break the inner loop. */
if( found == PRIME_TRUE ) /* If the loop ended normally, we found a prime. */
{
return( perhapsprime ); /* Return the prime we found. */
}
}
return( perhapsprime ); /* When the loop ends, perhapsprime is a real prime. */
}