xref: /netbsd-src/sys/arch/x68k/usr.bin/palette/palette.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: palette.c,v 1.7 2007/03/04 06:01:07 christos Exp $	*/
2 /*
3  * pelette - manipulate text colormap for NetBSD/x68k.
4  * author: Masaru Oki
5  *
6  * This software is in the Public Domain.
7  */
8 
9 #include <sys/cdefs.h>
10 __RCSID("$NetBSD: palette.c,v 1.7 2007/03/04 06:01:07 christos Exp $");
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <sys/param.h>
15 #include <sys/ioctl.h>
16 #include <sys/mman.h>
17 #include <sys/fcntl.h>
18 
19 #define PALETTE_OFFSET 0x2000 /* physical addr: 0xe82000 */
20 #define PALETTE_SIZE   0x1000 /* at least 1 page */
21 
22 int
23 main(int argc, char *argv[])
24 {
25 	int fd;
26 	u_short *palette;
27 	char *mapaddr;
28 	int r, g, b;
29 	int c = 7;
30 
31 #ifdef DEBUG
32 {
33 	int i;
34 	printf("argc = %d\n", argc);
35 	for (i = 0; i < argc; i++)
36 		printf("argv[%d] = \"%s\"\n", i, argv[i]);
37 }
38 #endif
39 
40 	if ((fd = open("/dev/grf0", O_RDWR, 0)) < 0) {
41 		perror("open");
42 		exit(1);
43 	}
44 
45 	mapaddr = mmap(0, PALETTE_SIZE, PROT_READ | PROT_WRITE,
46 		       MAP_FILE | MAP_SHARED, fd, PALETTE_OFFSET);
47 	if (mapaddr == (void *)-1) {
48 		perror("mmap");
49 		close(fd);
50 		exit(1);
51 	}
52 	close(fd);
53 	palette = (u_short *)(mapaddr + 0x0200);
54 
55 	if (argc == 5) {
56 		c = atoi(argv[--argc]);
57 		if (c > 15) {
58 			printf("Usage: %s [red green blue [code]]\n", argv[0]);
59 			exit(1);
60 		}
61 	}
62 	if (argc != 4)
63 		r = g = b = 31;
64 	else {
65 		r = atoi(argv[1]);
66 		g = atoi(argv[2]);
67 		b = atoi(argv[3]);
68 		if (r > 31 || g > 31 || b > 31) {
69 			printf("Usage: %s [red green blue [code]]\n", argv[0]);
70 			r = g = b = 31;
71 		}
72 	}
73 #ifdef DEBUG
74 	printf("color table offset = %d\n", c);
75 	printf("r = %d, g = %d, b = %d\n", r, g, b);
76 #endif
77 	r <<= 6;
78 	g <<= 11;
79 	b <<= 1;
80 
81 	palette[c] = r | g | b | 1;
82 
83 	exit(0);
84 }
85