Initial Import from SVN

This commit is contained in:
Matt Jenkins
2014-04-09 14:27:18 +01:00
parent 8976e834c4
commit 895f96d2f7
3153 changed files with 748589 additions and 0 deletions

14
share/example/Makefile Normal file
View File

@@ -0,0 +1,14 @@
all: ashello echo
ashello: ashello.o
$(LD) ashello.o -o $@
chello: chello.o
$(CC) chello.o -o $@
echo: echo.o
$(LD) $@.o -o $@
clean:
rm -f *.o ashello echo *.dis *~

View File

@@ -0,0 +1,30 @@
TOPSRC = $(shell cd ../..; pwd)
include $(TOPSRC)/target.mk
CFLAGS += -Werror
ASFLAGS += -DCROSS
ASLDFLAGS = --oformat=elf32-tradlittlemips -N -nostartfiles -T $(TOPSRC)/src/elf32-mips.ld
all: hello cplus
hello: hello.o
${CC} ${LDFLAGS} -o hello.elf hello.o ${LIBS}
${OBJDUMP} -S hello.elf > hello.dis
${SIZE} hello.elf
${ELF2AOUT} hello.elf $@
cplus: cplus.o
${CXX} ${LDFLAGS} -nostdlib -o cplus.elf cplus.o ${LIBS}
${OBJDUMP} -S cplus.elf > cplus.dis
${SIZE} cplus.elf
${ELF2AOUT} cplus.elf $@
echo: echo.o
${LD} ${ASLDFLAGS} -o $@.elf $@.o
${OBJDUMP} -S $@.elf > $@.dis
${SIZE} $@.elf
${ELF2AOUT} $@.elf $@
./aout $@ > $@.dis
clean:
rm -f *.o *.elf ${MAN} hello cplus *.elf *.dis tags *~

25
share/example/ashello.S Normal file
View File

@@ -0,0 +1,25 @@
/*
* This is an example of MIPS assembly program for RetroBSD.
*
* To compile this program, type:
* cc -c ashello.S
* ld ashello.o -o ashello
*/
#include <syscall.h>
.data // begin data segment
hello: .ascii "Hello, assembly world!\n" // a string
.text // begin code segment
.globl start // entry point for ld
start:
li $a0, 0 // arg 1: stdout fd
la $a1, hello // arg 2: string address
li $a2, 23 // arg 3: string length
syscall SYS_write // call the kernel: write()
nop // returns here on error
nop // skips two words on success
li $a0, 0 // arg 1: exit status
syscall SYS_exit // call the kernel: exit()
// no return

61
share/example/blkjack.bas Normal file
View File

@@ -0,0 +1,61 @@
10 rem **************************************
20 rem *** Play the "blackjack" (21) game ***
30 rem **************************************
40 dim C(52): m = 1000
50 rem
60 rem Create deck of cards and shuffle it
70 rem
80 for i=0 to 51: c(i) = i: next i
90 for i=0 to 51: i1=rnd(52): i2=c(i): c(i)=c(i2): c(i2)=i1: next i
100 rem
110 rem Prompt for amount of bet (on this game)
120 rem
130 print "You have", m, " dollars"
140 input "How much do you wish to bet?", b: if b=0 then stop
150 lif b>m then print "You don't have enough money":goto 140
160 t = 0: d = 0
170 rem
180 rem Prompt PLAYER for another card
190 rem
200 print "Total:", t,:Input " Another card (Y/N)?", a$
210 if a$="n" then 380
220 lif a$<>"y" then print "Please answer y-Yes or n-No":goto 200
230 c = c(d): d = d + 1: gosub 530
240 c = c % 13: if c > 9 then c = 9
250 if c > 0 then 300
260 input "(1)one or (t)ten ?", a$
270 if a$="1" then 300
280 if a$<>"t" then 260
290 c = 9
300 t = t + c + 1
310 if t <= 21 then 200
320 print "You went over 21! - you LOSE!"
330 m = m - b: if m > 0 then 80
340 print "You went BUST!":end
350 rem
360 rem Play DEALER
370 rem
380 t1 = 0
390 c = c(d): d = d + 1: print "Dealer draws ",: gosub 530
400 c = c % 13: if c > 9 then c = 9
410 if c > 0 then 470
420 if t1 < 10 then 450
430 if (t1+10) > 23 then 460
440 if (t1+10) >= t then 450
450 c = 9
460 print "Dealer chooses", c+1
470 t1 = t1 + c + 1: print "Dealer totals", t1: if t1 < t then 390
480 lif t1 <= 21 then print "Dealer wins - You LOSE!": goto 330
490 print "Dealer loses - You WIN!!!": m = m + b: goto 80
500 rem
510 rem Subroutine to display text description of a card
520 rem
530 order 590
540 for a = 0 to c / 13: read a$: next a
550 order 600
560 for a = 0 to c % 13: read a1$:next a
570 print a1$, " of ", a$
580 return
590 data "Hearts", "Diamonds", "Clubs", "Spades"
600 data "Ace", "Two", "Three", "Four", "Five", "Six", "Seven"
610 data "Eight", "Nine", "Ten", "Jack", "Queen", "King"

7
share/example/chello.c Normal file
View File

@@ -0,0 +1,7 @@
#include <stdio.h>
int main()
{
printf ("Hello, C World!\n");
return 0;
}

11
share/example/cplus.cpp Normal file
View File

@@ -0,0 +1,11 @@
extern "C" {
#include <string.h>
#include <unistd.h>
};
int main()
{
const char *message = "Hello, C++ World!\n";
write (1, message, strlen (message));
return 0;
}

67
share/example/echo.S Normal file
View File

@@ -0,0 +1,67 @@
/*
* This is a standard /bin/echo utility, rewritten in MIPS assembler.
*
* To compile this program, type:
* cc -c echo.S
* ld echo.o -o echo
*
* Run as:
* ./echo Make love not war
*/
#include <syscall.h>
.data // begin data segment
eoln: .ascii "\n"
space: .ascii " "
.text // begin code segment
start: .globl start // entry point for ld
//
// Program gets three arguments:
// argc in $a0 - number of words in command line
// argv in $a1 - address of list of pointers to words
// env in $a2 - address of list of pointers to environment variables
//
addi $s0, $a0, -1 // argc - 1
beq $s0, $zero, done // if (argc == 0) goto gone
addi $s1, $a1, 4 // argv + 4
loop:
jal print // print string
lw $a0, 0($s1) // arg 1: *argv
addi $s0, $s0, -1 // --argc
la $a1, eoln // arg2: newline
beq $s0, $zero, last // if (argc == 0) goto last
li $a0, 1 // arg1: stdout
la $a1, space // arg2: space
last:
li $a2, 1 // arg3: length
syscall SYS_write // call the kernel: write()
nop // ignore errors
nop
bne $s0, $zero, loop // if (argc == 0) goto gone
addi $s1, $s1, 4 // ++argv
done:
li $a0, 0 // arg1: exit status
syscall SYS_exit // call the kernel: exit()
// no return
print: move $a1, $a0 // arg2: string
addi $a2, $a0, 1 // compute length
strlen:
lb $v0, 0($a0) // get byte from string
bne $v0, $zero, strlen // continue if not end
addi $a0, $a0, 1 // increment pointer
subu $a2, $a0, $a2 // arg3: length
li $a0, 1 // arg1: stdout
syscall SYS_write // call the kernel: write()
nop // ignore errors
nop
jr $ra
nop

23
share/example/fact.fth Normal file
View File

@@ -0,0 +1,23 @@
\ Iterative factorial function.
." Defining fact function ... "
: fact ( n -- n! )
dup 2 < if drop 1 else
dup begin 1- swap over * swap dup 1 = until
drop then
; ." done." cr
." 1! = " 1 fact . cr
." 2! = " 2 fact . cr
." 3! = " 3 fact . cr
." 4! = " 4 fact . cr
." 5! = " 5 fact . cr
." 6! = " 6 fact . cr
." 7! = " 7 fact . cr
." 8! = " 8 fact . cr
." 9! = " 9 fact . cr
." 10! = " 10 fact . cr
." 11! = " 11 fact . cr
." 12! = " 12 fact . cr
halt

13
share/example/hilow.bas Normal file
View File

@@ -0,0 +1,13 @@
10 rem ****************************
20 rem *** Play the HI/LOW game ***
30 rem ****************************
40 n = rnd(100)
50 c = 0
60 input "Guess a number? ", g
70 c = c+1
80 if g=n then 120
90 if g>n then print "lower"
100 if g<n then print "higher"
110 goto 60
120 print "You guessed it in", c, " tries!"
130 exit

29
share/example/prime.scm Normal file
View File

@@ -0,0 +1,29 @@
(define (square n) (* n n))
(define (divides? a b)
(= (remainder b a) 0))
(define (find-divisor n test-divisor)
(cond ((> (square test-divisor) n) n)
((divides? test-divisor n) test-divisor)
(else (find-divisor n (+ test-divisor 1)))))
(define (smallest-divisor n)
(find-divisor n 2))
(define (prime? n)
(= n (smallest-divisor n)))
(define (t n)
(display n)
(if (prime? n)
(display '-prime)
(display '-compound))
(newline))
(t 2)
(t 20)
(t 23)
(t 65)
(t 67)
(t 2011)

14
share/example/stars.bas Normal file
View File

@@ -0,0 +1,14 @@
10 INPUT "Your name: ", U$
20 PRINT "Hello ", U$
30 INPUT "How many stars do you want: ", N
40 S$ = ""
50 FOR I = 1 TO N
60 S$ = S$ + "*"
70 NEXT I
80 PRINT S$
90 INPUT "Do you want more stars? ", A$
100 IF A$ = "" THEN GOTO 90
110 IF A$ = "Y" THEN GOTO 30
120 IF A$ = "y" THEN GOTO 30
130 PRINT "Goodbye ", U$
140 END

41
share/smallc/Makefile Normal file
View File

@@ -0,0 +1,41 @@
CC = scc
PROG = hello primelist primesum test1 test2 test3 gpio adc rain \
webserver q8
all: $(PROG)
hello: hello.c
$(CC) $(CFLAGS) -o $@ $(LDFLAGS) hello.c
primelist: primelist.c
$(CC) $(CFLAGS) -o $@ $(LDFLAGS) primelist.c
primesum: primesum.c
$(CC) $(CFLAGS) -o $@ $(LDFLAGS) primesum.c
webserver: webserver.c
$(CC) $(CFLAGS) -o $@ $(LDFLAGS) webserver.c -lwiznet
gpio: gpio.c
$(CC) $(CFLAGS) -o $@ $(LDFLAGS) gpio.c
adc: adc.c
$(CC) $(CFLAGS) -o $@ $(LDFLAGS) adc.c
rain: rain.c
$(CC) $(CFLAGS) -o $@ $(LDFLAGS) rain.c
q8: q8.c
$(CC) $(CFLAGS) -o $@ $(LDFLAGS) q8.c
test1: test1.c
$(CC) $(CFLAGS) -o $@ $(LDFLAGS) test1.c
test2: test2.c
$(CC) $(CFLAGS) -o $@ $(LDFLAGS) test2.c
test3: test3.c
$(CC) $(CFLAGS) -o $@ $(LDFLAGS) test3.c
clean:
rm -f *.o $(PROG)

26
share/smallc/adc.c Normal file
View File

@@ -0,0 +1,26 @@
/*
* Example of reading ADC data.
*/
#include <fcntl.h>
char buf[100];
main()
{
int i, fd, value;
for (i=0; i<16; i++) {
sprintf(buf, "/dev/adc%d", i);
fd = open(buf, O_RDWR);
if (fd < 0) {
printf("Error: unable to open %s\n", buf);
} else {
if (read(fd, buf, 20) > 0) {
value = strtol (buf, 0, 0);
printf("adc%-2d = %d\n", i, value);
}
close(fd);
}
}
return 0;
}

24
share/smallc/gpio.c Normal file
View File

@@ -0,0 +1,24 @@
/*
* Example of polling general purpose i/o pins.
*/
#include <fcntl.h>
#include <sys/gpio.h>
main ()
{
int fd, pnum, value;
fd = open ("/dev/porta", O_RDWR);
if (fd < 0) {
perror ("/dev/porta");
return -1;
}
for (pnum=0; pnum<7; pnum++) {
value = ioctl (fd, GPIO_POLL | GPIO_PORT (pnum), 0);
if (value < 0)
perror ("GPIO_POLL");
printf ("port%c = 0x%04x\n", pnum + 'A', value);
}
return 0;
}

6
share/smallc/hello.c Normal file
View File

@@ -0,0 +1,6 @@
extern int printf();
int main()
{
printf ("Hello, SmallC World!\n");
}

31
share/smallc/primelist.c Normal file
View File

@@ -0,0 +1,31 @@
/*
* Print the list of prime numbers up to 100.
*/
main()
{
int n;
for (n=2; n<100; ++n) {
if (isprime(n)) {
printf("%d ", n);
}
}
printf("\n");
}
isprime(n)
int n;
{
int j;
if (n == 2)
return 1;
if (n % 2 == 0)
return 0;
for (j=3; j*j<=n; j+=2)
if (n % j == 0)
return 0;
return 1;
}

32
share/smallc/primesum.c Normal file
View File

@@ -0,0 +1,32 @@
/*
* Compute the sum of prime numbers up to 10000.
*/
main()
{
int sum, n;
sum = 0;
for (n=2; n<10000; ++n) {
if (isprime(n)) {
sum += n;
}
}
printf("Sum of primes less than 10000: %d\n", sum);
}
isprime(n)
int n;
{
int j;
if (n == 2)
return 1;
if (n % 2 == 0)
return 0;
for (j=3; j*j<=n; j+=2)
if (n % j == 0)
return 0;
return 1;
}

208
share/smallc/q8.c Normal file
View File

@@ -0,0 +1,208 @@
/*
* Eight Queens puzzle
*
* (C) 2010 by Mark Sproul
* Open source as per standard Arduino code
* Modified by Pito 12/2012 for SmallC
*/
#define TRUE 1
#define FALSE 0
unsigned int gChessBoard[8];
unsigned int gLoopCounter;
int gValidCount;
CheckCurrentBoard()
{
int ii;
int jj;
int theRow;
int theLongRow;
int theLongColumns;
int bitCount;
//* we know we have 1 in each row,
//* Check for 1 in each column
theRow = 0;
for (ii=0; ii<8; ii++) {
theRow |= gChessBoard[ii];
}
if (theRow != 0x0ff) {
return FALSE;
}
//* we have 1 in each column, now check the diagonals
theLongColumns = 0;
for (ii=0; ii<8; ii++) {
theLongRow = gChessBoard[ii] & 0x0ff;
theLongRow = theLongRow << ii;
theLongColumns |= theLongRow;
}
//* now count the bits
bitCount = 0;
for (ii=0; ii<16; ii++) {
if ((theLongColumns & 0x01) == 0x01) {
bitCount++;
}
theLongColumns = theLongColumns >> 1;
}
if (bitCount != 8) {
return FALSE;
}
//* we now have to check the other diagonal
theLongColumns = 0;
for (ii=0; ii<8; ii++) {
theLongRow = gChessBoard[ii] & 0x0ff;
theLongRow = theLongRow << 8;
theLongRow = theLongRow >> ii;
theLongColumns |= theLongRow;
}
//* now count the bits
bitCount = 0;
for (ii=0; ii<16; ii++) {
if ((theLongColumns & 0x01) == 0x01) {
bitCount++;
}
theLongColumns = theLongColumns >> 1;
}
if (bitCount != 8) {
return FALSE;
}
return TRUE;
}
CheckForDone()
{
int ii;
int weAreDone;
int theRow;
weAreDone = FALSE;
//* we know we have 1 in each row,
//* Check for 1 in each column
theRow = 0;
for (ii=0; ii<8; ii++) {
theRow |= gChessBoard[ii];
}
if (theRow == 0x01) {
weAreDone = TRUE;
}
return weAreDone;
}
RotateQueens()
{
int ii;
int keepGoing;
int theRow;
ii = 0;
keepGoing = TRUE;
while (keepGoing && (ii < 8)) {
theRow = gChessBoard[ii] & 0x0ff;
theRow = (theRow >> 1) & 0x0ff;
if (theRow != 0) {
gChessBoard[ii] = theRow;
keepGoing = FALSE;
} else {
gChessBoard[ii] = 0x080;
}
ii++;
}
}
PrintChessBoard()
{
int ii;
int jj;
int theRow;
char textString[32];
printf("\nLoop= %d\n", gLoopCounter);
printf("Solution count= %d\n", gValidCount);
printf("+----------------+\n");
for (ii=0; ii<8; ii++) {
theRow = gChessBoard[ii];
printf("|");
for (jj=0; jj<8; jj++) {
if (theRow & 0x080) {
printf("Q ");
} else {
printf(". ");
}
theRow = theRow << 1;
}
printf("|\n");
}
printf("+----------------+\n");
}
main()
{
int ii;
printf("\nEight Queens brute force");
printf("\n************************\n");
//* put the 8 queens on the board, 1 in each row
for (ii=0; ii<8; ii++) {
gChessBoard[ii] = 0x080;
}
PrintChessBoard();
gLoopCounter = 0;
gValidCount = 0;
while (1) {
gLoopCounter++;
if (CheckCurrentBoard()) {
gValidCount++;
PrintChessBoard();
} else if ((gLoopCounter % 1000) == 0) {
//PrintChessBoard();
}
RotateQueens();
if (CheckForDone()) {
//int elapsedSeconds;
//int elapsedMinutes;
//int elapsedHours;
//elapsedSeconds = millis() / 1000;
//elapsedMinutes = elapsedSeconds / 60;
//elapsedHours = elapsedMinutes / 60;
printf("----------------------------------\n");
printf("All done\n");
PrintChessBoard();
printf("----------------------------------\n");
//Serial.print("total seconds=");
//Serial.println(elapsedSeconds);
//Serial.print("hours=");
//Serial.println(elapsedHours);
//Serial.print("minutes=");
//Serial.println(elapsedMinutes % 60);
//Serial.print("seconds=");
//Serial.println(elapsedSeconds % 60);
return (1);
}
}
}

129
share/smallc/rain.c Normal file
View File

@@ -0,0 +1,129 @@
/*
* Example of using termcap library for SmallC.
* 11/3/1980 EPS/CITHEP
*
* Copyright (c) 1980 Regents of the University of California.
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
*/
#include <stdio.h>
#include <signal.h>
#define CO 80 /* number of columns */
#define LI 24 /* number of lines */
#define CL "\33[H\33[J" /* clear the screen */
#define CM "\33[%u;%uH" /* move the cursor to row, column */
#define BC "\b" /* move cursor left */
#define DN "\33[B" /* move cursor down */
#define ND " " /* move cursor right */
int xpos[5], ypos[5];
char outbuf[BUFSIZ];
moveto(col, row)
int col, row;
{
printf(CM, row, col);
}
onsig(n)
int n;
{
moveto(0, LI - 1);
fflush(stdout);
_exit(0);
}
main()
{
int x, y, j;
setbuf(stdout, outbuf);
for (j = SIGHUP; j <= SIGTERM; j++)
if (signal(j, SIG_IGN) != SIG_IGN)
signal(j, onsig);
fputs(CL, stdout);
fflush(stdout);
for (j = 5; --j >= 0; ) {
xpos[j] = 2 + rand() % (CO - 4);
ypos[j] = 2 + rand() % (LI - 4);
}
for (j = 0; ; ) {
x = 2 + rand() % (CO - 4);
y = 2 + rand() % (LI - 4);
moveto(x, y);
putchar('.');
moveto(xpos[j], ypos[j]);
putchar('o');
if (j == 0)
j = 4;
else
--j;
moveto(xpos[j], ypos[j]);
putchar('O');
if (j == 0)
j = 4;
else
--j;
moveto(xpos[j], ypos[j]-1);
putchar('-');
fputs(DN, stdout);
fputs(BC, stdout);
fputs(BC, stdout);
fputs("|.|", stdout);
fputs(DN, stdout);
fputs(BC, stdout);
fputs(BC, stdout);
putchar('-');
if (j == 0)
j = 4;
else
--j;
moveto(xpos[j], ypos[j]-2);
putchar('-');
fputs(DN, stdout);
fputs(BC, stdout);
fputs(BC, stdout);
fputs("/ \\", stdout);
moveto(xpos[j]-2, ypos[j]);
fputs("| O |", stdout);
moveto(xpos[j]-1, ypos[j]+1);
fputs("\\ /", stdout);
fputs(DN, stdout);
fputs(BC, stdout);
fputs(BC, stdout);
putchar('-');
if (j == 0)
j = 4;
else
--j;
moveto(xpos[j], ypos[j]-2);
putchar(' ');
fputs(DN, stdout);
fputs(BC, stdout);
fputs(BC, stdout);
putchar(' ');
fputs(ND, stdout);
putchar(' ');
moveto(xpos[j]-2, ypos[j]);
putchar(' ');
fputs(ND, stdout);
putchar(' ');
fputs(ND, stdout);
putchar(' ');
moveto(xpos[j]-1, ypos[j]+1);
putchar(' ');
fputs(ND, stdout);
putchar(' ');
fputs(DN, stdout);
fputs(BC, stdout);
fputs(BC, stdout);
putchar(' ');
xpos[j] = x;
ypos[j] = y;
fflush(stdout);
usleep(100000);
}
}

219
share/smallc/test1.c Normal file
View File

@@ -0,0 +1,219 @@
int ga[5];
main()
{
int a, b, c, d;
int arr[5];
int *pi;
char arrc[5];
char *pic;
int s1, s2;
int z;
int t;
int *pip;
int *picp;
int e1, e2;
ga[0] = 10;
ga[1] = 20;
ga[2] = 30;
ga[3] = 40;
ga[4] = 50;
a = 21;
b = 31;
c = 71;
d = 82;
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
pi = &arr[0];
arrc[0] = 13;
arrc[1] = 23;
arrc[2] = 33;
arrc[3] = 43;
arrc[4] = 53;
pic = &arrc[0];
printf(" 21 + 31 = %d (52)\n", a + b);
printf(" 21 - 31 = %d (-10)\n", a - b);
printf(" 21 & 71 = %d (5)\n", a & c);
printf(" 21 | 82 = %d (87)\n", a | d);
printf(" 21 ^ 82 = %d (71)\n", a ^ d);
printf(" 21 * 82 = %d (1722)\n", a * d);
printf(" 82 % 21 = %d (19)\n", d % a);
printf(" 82 / 21 = %d (3)\n", d / a);
printf(" *pi = %d (10)\n", *pi);
printf(" *pi + 1 = %d (11)\n", *pi + 1);
printf(" *(pi + 1) = %d (20)\n", *(pi + 1));
printf("&arr[3] - &arr[0] = %d (3)\n", &arr[3] - &arr[0]);
printf(" arr[3]-arr[0] = %d (30)\n", arr[3] - arr[0]);
printf(" arr[3]+arr[0] = %d (50)\n", arr[3] + arr[0]);
printf(" &ga[3] - &ga[0] = %d (3)\n", &ga[3] - &ga[0]);
printf(" ga[3]-ga[0] = %d (30)\n", ga[3] - ga[0]);
printf(" ga[3]+ga[0] = %d (50)\n", ga[3] + ga[0]);
printf("\n");
printf(" *pic = %d (13)\n", *pic);
printf(" *pic + 1 = %d (14)\n", *pic+1);
printf(" *(pic + 1) = %d (23)\n", *(pic+1));
printf("&arrc[3] - &arrc[0] = %d (3)\n", &arrc[3]-&arrc[0]);
printf("\n");
s1 = 3;
s2 = -200;
printf(" 82 << 3 = %d (656)\n", d << s1);
printf(" 82 >> 3 = %d (10)\n", d >> s1);
printf("-200 >> 3 = %d (-25)\n", s2 >> s1);
printf("-200 << 3 = %d (-1600)\n", s2 << s1);
printf("\n");
printf("-s1 = %d (-3)\n", -s1);
printf("-s2 = %d (200)\n", -s2);
printf("\n");
printf("~82 = %d (-83)\n", ~d);
printf("\n");
z = 0;
printf("!82 = %d (0)\n", !d);
printf(" !0 = %d (1)\n", !z);
printf("\n");
printf(" 0 && 0 = %d (0)\n", z && z);
printf(" 0 && 21 = %d (0)\n", z && a);
printf(" 3 && 21 = %d (1)\n", s1 && a);
printf("21 && 3 = %d (1)\n", a && s1);
printf("\n");
printf(" 0 || 0 = %d (0)\n", z || z);
printf(" 0 || 21 = %d (1)\n", z || a);
printf(" 3 || 21 = %d (1)\n", s1 || a);
printf("21 || 3 = %d (1)\n", a || s1);
printf("\n");
pi = 4;
printf("pi++ = %d (4)\n", pi++);
printf(" pi = %d (8)\n", pi);
printf("++pi = %d (12)\n", ++pi);
printf("pi-- = %d (12)\n", pi--);
printf(" pi = %d (8)\n", pi);
printf("--pi = %d (4)\n", --pi);
printf("\n");
pic = 4;
printf("pic++ = %d (4)\n", pic++);
printf(" pic = %d (5)\n", pic);
printf("++pic = %d (6)\n", ++pic);
printf("pic-- = %d (6)\n", pic--);
printf(" pic = %d (5)\n", pic);
printf("--pic = %d (4)\n", --pic);
printf("\n");
t = 4;
printf("t++ = %d (4)\n", t++);
printf(" t = %d (5)\n", t);
printf("++t = %d (6)\n", ++t);
printf("t-- = %d (6)\n", t--);
printf(" t = %d (5)\n", t);
printf("--t = %d (4)\n", --t);
printf("\n");
t = 4;
printf(" t==4 = %d (1)\n", t == 4);
printf(" t==3 = %d (0)\n", t == 3);
printf(" t==5 = %d (0)\n", t == 5);
t = -4;
printf("t==-4 = %d (1)\n", t == -4);
printf("t==-3 = %d (0)\n", t == -3);
printf("t==-5 = %d (0)\n", t == -5);
printf(" t==4 = %d (0)\n", t == 4);
printf(" t==3 = %d (0)\n", t == 3);
printf(" t==5 = %d (0)\n", t == 5);
printf("\n");
t = 4;
printf(" t!=4 = %d (0)\n", t != 4);
printf(" t!=3 = %d (1)\n", t != 3);
printf(" t!=5 = %d (1)\n", t != 5);
t = -4;
printf("t!=-4 = %d (0)\n", t != -4);
printf("t!=-3 = %d (1)\n", t != -3);
printf("t!=-5 = %d (1)\n", t != -5);
printf(" t!=4 = %d (1)\n", t != 4);
printf(" t!=3 = %d (1)\n", t != 3);
printf(" t!=5 = %d (1)\n", t != 5);
printf("\n");
t = 4;
printf(" t<4 = %d (0)\n", t < 4);
printf(" t<3 = %d (0)\n", t < 3);
printf(" t<5 = %d (1)\n", t < 5);
printf("t<-1 = %d (0)\n", t < -1);
printf("\n");
printf(" t<=4 = %d (1)\n", t <= 4);
printf(" t<=3 = %d (0)\n", t <= 3);
printf(" t<=5 = %d (1)\n", t <= 5);
printf("t<=-1 = %d (0)\n", t <= -1);
printf("\n");
t = 4;
printf(" t>4 = %d (0)\n", t > 4);
printf(" t>3 = %d (1)\n", t > 3);
printf(" t>5 = %d (0)\n", t > 5);
printf("t>-1 = %d (1)\n", t > -1);
printf("\n");
printf(" t>=4 = %d (1)\n", t >= 4);
printf(" t>=3 = %d (1)\n", t >= 3);
printf(" t>=5 = %d (0)\n", t >= 5);
printf("t>=-1 = %d (1)\n", t >= -1);
printf("\n");
pi = -100;
printf(" pi<4 = %d (0)\n", pi < 4);
printf(" pi<3 = %d (0)\n", pi < 3);
printf("pi<-100 = %d (0)\n", pi < -100);
printf(" pi<-1 = %d (1)\n", pi < -1);
printf("\n");
printf(" pi<=4 = %d (0)\n", pi <= 4);
printf(" pi<=3 = %d (0)\n", pi <= 3);
printf("pi<=-100 = %d (1)\n", pi <= -100);
printf(" pi<=-1 = %d (1)\n", pi <= -1);
printf("\n");
pi = -100;
printf(" pi>4 = %d (1)\n", pi > 4);
printf(" pi>3 = %d (1)\n", pi > 3);
printf("pi>-100 = %d (0)\n", pi > -100);
printf(" pi>-1 = %d (0)\n", pi > -1);
printf("\n");
printf(" pi>=4 = %d (1)\n", pi >= 4);
printf(" pi>=3 = %d (1)\n", pi >= 3);
printf("pi>=-100 = %d (1)\n", pi >= -100);
printf(" pi>=-1 = %d (0)\n", pi >= -1);
printf("\n");
pi = &arr[0];
pip = &arr[3];
printf(" *pip - *pi: %d 30\n", *pip - *pi);
printf(" pip - pi: %d 3\n", pip - pi);
printf(" *pip: %d 40\n", *pip);
printf(" *(pip - 3): %d 10\n", *(pip - 3));
printf(" *&arr[3]: %d 40\n", *&arr[3]);
printf("*(&arr[3] - 3): %d 10\n", *(&arr[3]-3));
}
printt (t, str)
int t;
char *str;
{
printf("bool test on value %d %s\n", t, str);
}

127
share/smallc/test2.c Normal file
View File

@@ -0,0 +1,127 @@
#include <fcntl.h>
int aaa;
int bbb;
int ccc;
char gc;
char gbuffer[3];
int gibuffer[4];
extern errno;
main()
{
char b;
int la;
unsigned int u1, u2;
int s1, s2;
unsigned char uc1, uc2;
char sc1, sc2;
int fd;
char buffer[6];
int ibuffer[7];
printf(" sizeof(uc1): %d 1\n", sizeof(uc1));
printf(" sizeof(sc1): %d 1\n", sizeof(sc1));
printf(" sizeof(u1): %d 4\n", sizeof(u1));
printf(" sizeof(s1): %d 4\n", sizeof(s1));
printf(" sizeof(aaa): %d 4\n", sizeof(aaa));
printf(" sizeof(bbb): %d 4\n", sizeof(bbb));
printf(" sizeof(gc): %d 1\n", sizeof(gc));
printf(" sizeof(buffer): %d 6\n", sizeof(buffer));
printf(" sizeof(ibuffer): %d 28\n", sizeof(ibuffer));
printf(" sizeof(char): %d 1\n", sizeof(char));
printf(" sizeof(int): %d 4\n", sizeof(int));
printf(" sizeof(gbuffer): %d 3\n", sizeof(gbuffer));
printf(" sizeof(gibuffer): %d 16\n", sizeof(gibuffer));
// sizeof(ibuffer[0]) is not supported, so the following can be used...
printf("sizeof(ibuffer)/sizeof(int): %d 7\n", sizeof(ibuffer)/sizeof(int));
aaa = 1;
bbb = 2;
la = 4;
printf("%d 1\n", aaa);
printf("%d 2\n", bbb);
printf("%d 4\n", la);
uc1 = 0x80;
sc1 = 0x80;
s1 = uc1;
s2 = sc1;
printf("unsigned char (0x80) -> int: %d 128\n", s1);
printf(" signed char (0x80) -> int: %d -128\n", s2);
u1 = uc1;
u2 = sc1;
printf("unsigned char (0x80) -> unsigned: %d 128\n", u1);
printf(" signed char (0x80) -> unsigned: %d -128\n", u2);
la = errno;
printf("errno: %d 0\n", la);
write(1, "abcd ", 5);
la = errno;
printf("errno after good write call: %d 0\n", la);
write(10, "abcde", 5);
la = errno;
printf("errno after bad write call: %d 9\n", la);
write(1, "abcd ", 5);
la = errno;
printf("good write after failed should not overwrite errno: %d 9\n", la);
errno = 0;
write(1, "abcd ", 5);
la = errno;
printf("good write after errno set to zero: %d 0\n", la);
la = write(1, "abcd ", 5);
printf("write() return: %d 5\n", la);
la = write(10, "abcd ", 5);
printf("write(bad fd) return: %d -1\n", la);
fd = open("/a.txt", O_WRONLY | O_CREAT, 0666);
if (fd != -1) {
printf("open success\n");
la = write(fd, "abcd\n", 5);
if (la == 5) printf("write success\n"); else printf("write failed\n");
la = close(fd);
if (la != -1) printf("close success\n"); else printf("close failed\n");
} else {
printf("open failed\n");
}
buffer[0] = 0;
buffer[1] = 0;
buffer[2] = 0;
buffer[3] = 0;
buffer[4] = 0;
buffer[5] = 0;
fd = open("/a.txt", O_RDONLY, 0666);
if (fd != -1) {
printf("open success\n");
la = read(fd, buffer, 5);
printf(buffer);
if (la == 5) printf("read success\n"); else printf("read failed\n");
la = close(fd);
if (la != -1) printf("close success\n"); else printf("close failed\n");
} else {
printf("open failed\n");
}
if (buffer[0] != 'a') printf("data0 readback from file MISMATCH\n");
if (buffer[1] != 'b') printf("data1 readback from file MISMATCH\n");
if (buffer[2] != 'c') printf("data2 readback from file MISMATCH\n");
if (buffer[3] != 'd') printf("data3 readback from file MISMATCH\n");
if (buffer[4] != '\n') printf("data4 readback from file MISMATCH\n");
if (buffer[0] != 'a' || buffer[1] != 'b' || buffer[2] != 'c' ||
buffer[3] != 'd' || buffer[4] != '\n') {
printf("data readback from file MISMATCH\n");
} else {
printf("data readback from file OK\n");
}
}

49
share/smallc/test3.c Normal file
View File

@@ -0,0 +1,49 @@
main()
{
int t;
t = 0;
if (t) printt(t, "failure"); else printt(t, "success");
t = 1;
if (t) printt(t, "success"); else printt(t, "failure");
t = 8;
if (t) printt(t, "success"); else printt(t, "failure");
t = -2;
if (t) printt(t, "success"); else printt(t, "failure");
printf("\n");
t = 4;
printf("switch test: ");
switch (t) {
case 3:
printf("failure");
break;
case 4:
printf("success");
break;
case 5:
printf("failure");
break;
}
printf("\n");
printf("switch fallthrough test: ");
switch (t) {
case 3:
printf("failure");
break;
case 4:
printf("OKSOFAR: ");
case 5:
printf("success if oksofar printed before this in caps");
break;
}
printf("\n");
}
printt (t, str)
int t;
char *str;
{
printf("bool test on value %d %s\n", t, str);
}

88
share/smallc/webserver.c Normal file
View File

@@ -0,0 +1,88 @@
/*
* Web server, written in SmallC.
*
* A simple web server that shows the value of the analog input pins.
* using an Arduino Wiznet Ethernet shield.
*
* 18 Dec 2009 created by David A. Mellis
* 4 Sep 2010 modified by Tom Igoe
* 16 Apr 2012 ported to RetroBSD by Serge Vakulenko
*/
#include <stdio.h>
#include <fcntl.h>
#include <sys/gpio.h>
#include <wiznet.h>
int client [CLIENT_SIZE];
char buf [80];
main()
{
/* Initialize the Ethernet server library
* with the IP address and port you want to use
* (port 80 is default for HTTP). */
ethernet_init ();
server_init (80);
for (;;) {
/* Listen for incoming clients. */
if (server_available (client)) {
process_request();
}
}
}
process_request()
{
int current_line_is_blank;
int c, fd, pnum, value;
/* An http request ends with a blank line. */
current_line_is_blank = 1;
/* Parse the http request. */
while (client_connected (client)) {
if (client_available (client)) {
c = client_getc (client);
/* If you've gotten to the end of the line (received
* a newline character) and the line is blank, the http
* request has ended, so you can send a reply. */
if (c == '\n' && current_line_is_blank)
break;
if (c == '\n') {
/* You're starting a new line. */
current_line_is_blank = 1;
}
else if (c != '\r') {
/* You've gotten a character on the current line. */
current_line_is_blank = 0;
}
}
}
/* Send a standard http response header. */
client_puts (client, "HTTP/1.1 200 OK\n");
client_puts (client, "Content-Type: text/html\n\n");
/* Output the value of each digital pin. */
fd = open ("/dev/porta", O_RDONLY);
if (fd < 0) {
client_puts (client, "Failed to open /dev/porta\n");
} else {
for (pnum = 0; pnum < 6; pnum++) {
value = ioctl (fd, GPIO_POLL | GPIO_PORT (pnum), 0);
sprintf (buf, "PORT%c = 0x%04x <br />\n", pnum+'A', value);
client_puts (client, buf);
}
close (fd);
}
/* Give the web browser time to receive the data. */
usleep (1000);
/* Close the connection. */
client_stop (client);
}