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