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